简体   繁体   English

从消息队列接收

[英]Receiving from message queues

I have successfully created the message queue by using the following command: 我已经使用以下命令成功创建了消息队列:

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

Now I want to send the queue to some other process I used, 现在,我想将队列发送到我使用的其他进程,

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

and I try to receive it in different process by: 我尝试通过以下方式以不同的方式接收它:

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

my structure bufHareTurtle is of following type: 我的结构bufHareTurtle是以下类型:

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

My question: The sending was successful and the program(both the processes) is running too but whenever I am sending an unsigned integer for example 2 , I AM ALWAYS GETTING THE RECEIVED VALUE (IN LATTER PROCESS) AS 0 EVRYTIME. 我的问题:发送成功,并且程序(两个进程)也都在运行,但是每当我发送无符号整数(例如2)时,我总是以0 EVRYTIME的形式获取接收的值(在后期处理中)。 Could somebody tell me what is the error in this code or what could be possible error elsewhere. 有人可以告诉我这段代码是什么错误,或者其他地方可能是什么错误。

The problem was there in synchronization. 问题是在那里同步。 The sending to the queue was delayed due to sleep inserted in between. 由于在两者之间插入了睡眠,因此延迟了发送到队列的操作。 I corrected it and the error was gone 我更正了,错误消失了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM