从消息队列接收

时间:2010-08-09 17:52:45

标签: c operating-system ipc

我已使用以下命令成功创建了消息队列:

msgIdHareTurtle =  msgget(keyHareTurtle, 0644 | IPC_CREAT | O_NONBLOCK);  

现在我想将队列发送到我使用的其他进程,

msgsnd(msgIdHareTurtle, (struct msgbuf *)&bufHareTurtle, sizeof(int), IPC_NOWAIT);  

我尝试通过以下方式在不同的过程中接收它:

msgrcv(msgIdHareTurtle, (struct msgbuf *)&bufHareTurtle, sizeof(int), 0, IPC_NOWAIT);

我的结构bufHareTurtle属于以下类型:

typedef struct smsgbuf{
    long mtype;
    unsigned int position;
} smsgbuf; 

我的问题:发送成功并且程序(两个进程)也在运行,但每当我发送一个无符号整数(例如2)时,我总是获得收到的值(在LATTER过程中)为0 EVRYTIME。有人可以告诉我这段代码中的错误是什么,或者其他地方可能出现的错误。

1 个答案:

答案 0 :(得分:0)

问题在于同步。由于插入其间的睡眠,发送到队列的时间延迟了。我纠正了它,错误消失了

相关问题