- 论坛徽章:
- 0
|
最近在学Linux的消息队列,在使用msgget函数后正常情况下返回值应该是“消息队列标识符”,但不知为何一直返回的是0,代码如下,请大虾们赐教!谢谢!
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
key_t unique_key;
int msgid;
int status;
char str1[] = {"test message:hello!"};
char str2[] = {"test message:goodbye!"};
struct msgbuf
{
long msgtype;
char msgtext[1024];
}sndmsg,rcvmsg;
if ( msgid=msgget(IPC_PRIVATE, 0666) == -1 ) //错误在此,修改为if ( (msgid=msgget(IPC_PRIVATE, 0666)) == -1 )
{
printf("msgget error!\n");
exit(254);
}
printf("msgid: %d\n", msgid);
sndmsg.msgtype = 111;
sprintf(sndmsg.msgtext,str1);
if ( msgsnd(msgid, (struct msgbuf *)&sndmsg, sizeof(str1)+1, 0) == -1 )
{
printf("msgsnd error!\n");
exit(254);
}
sndmsg.msgtype = 222;
sprintf(sndmsg.msgtext,str2);
if ( msgsnd(msgid, (struct msgbuf *)&sndmsg, sizeof(str2)+1, 0) == -1 )
{
printf("msgsnd error!\n");
exit(254);
}
if ( status=msgrcv(msgid, (struct msgbuf *)&rcvmsg, 80, 222, IPC_NOWAIT) == -1 )
{
printf("msgrcv error!\n");
exit(254);
}
printf("The received message: %s.\n", rcvmsg.msgtext);
if (msgctl(msgid, IPC_RMID, 0) == -1)
{
printf("msgctl error!\n");
exit(254);
}
exit(0);
} |
[ 本帖最后由 wangpeng168 于 2009-11-27 16:38 编辑 ] |
|