【讨论】FreeBSD5.4中的msgrcv系统调用是否有问题, 我在Linux下没有问题的,请大侠们看
FreeBSD5.4中:
我用 msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg)时,
当msgtyp是负数时,系统会返回Msg队列中类型值小于 等于abs(msgtye)的第一个消息,
而不是队列中类型值小于 等于abs(msgtye)的消息中类型值最小的消息,系统中的msrcv
man:RETURN:
The msgtyp argument is less than 0. The first message of the lowest
message type that is less than or equal to the absolute value of
msgtyp will be received.
同一个程序,我在Linux下编译运行都正确的,
是不是FB5.4中的msgrcv调用有问题??大家有没有碰到同样的问题。
请大家看看,谢谢!!
我的测试代码如下:
[CODE]//====================
//q.h 头文件
//====================
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define QKEY (key_t)0105
#define QPERM 0660 /*消息队列的权限*/
#define MAXLEN 50 /*最大消息体长度*/
#define MAXPRIOR 10 /*最大消息优先级*/
struct q_entry {
long mtype ;
char mtext[MAXLEN+1] ;
} ; //我的消息定义
//=====================
//server.c 用于接收消息
//=====================
#include "q.h"
int warn ( char* s )
{
fprintf ( stderr , "%s\n" , s ) ;
exit (1) ;
}
//初始化一个消息队列,消息队列不存在时新建一个
int init_q ( void )
{
int mqid ;
if ( (mqid=msgget(QKEY,IPC_CREAT|QPERM))==-1 ) {
perror ( "init_q:msgget" ) ;
}
return mqid ;
}
int receive (void)
{
int mqid ,len ;
int dealmsg ( struct q_entry msg ) ;
struct q_entry msg ;
if ( (mqid=init_q())==-1)
warn("receive:mqid error" ) ;
for ( ;; ){
if ( (len=msgrcv(mqid,&msg,MAXLEN,(-1)*(MAXPRIOR),MSG_NOERROR) )== -1 )
warn ( "receive: msgrcv" ) ;
msg.mtext[len] = '\0' ;
dealmsg ( msg ) ;
}
}
//处理收到的消息例程,
int dealmsg ( struct q_entry msg )
{
printf ( "Received msg : %-30s %d\n",msg.mtext,msg.mtype ) ;
return 0 ;
}
int main ( int argc , char** argv )
{
receive () ;
}
//======================================
//client.h 发送消息,需要 消息 和 消息优先级两个参数
//======================================
#include "q.h"
int warn ( char* s )
{
fprintf ( stderr , "%s\n" , s ) ;
exit (1) ;
}
//初始化一个消息队列,消息队列不存在时新建一个
int init_q ( void )
{
int mqid ;
if ( (mqid=msgget(QKEY,IPC_CREAT|QPERM))==-1 ) {
perror ( "init_q:msgget" ) ;
}
return mqid ;
}
//发送一个消息,priority 将为消息的 类型值
int send ( char *msg , int priority )
{
int mqid , len ;
len = strlen ( msg ) ;
struct q_entry m_ent ;
if ( len > MAXLEN )
warn ( "message to long" ) ;
if ( priority > MAXPRIOR | priority < 1 )
warn ( "priority value illegal" ) ;
if ( (mqid = init_q ())== -1 )
warn ( "mqid error" ) ;
m_ent.mtype = (long)priority ;
memset ( m_ent.mtext,'\0',MAXLEN+1 ) ;
memcpy ( m_ent.mtext,msg,len ) ;
printf ( "in send m_ent.mtext=%s\n",m_ent.mtext ) ;
if ( msgsnd(mqid,&m_ent,len,0)==-1 )
warn("msgsnd") ;
return 0 ;
}
static const char *usage = "usage: send msg #prio\n" ;
int main ( int argc , char** argv )
{
int prio ;
if ( argc != 3 )
warn ( usage ) ;
prio = atoi ( argv[2] ) ;
send ( argv[1],prio ) ;
}
[/CODE]