System V消息队列创建成功,命令ipcs -q
输出:
------ Message Queues --------<
key msqid owner perms used-bytes messages
0x080b2fb4 0 hel 600 0 0
但是接收消息的程序返回:
exit: msgrcv error, No message of desired type
这是我的代码:
/* create key for message queue */
key = ftok("/home/hel/messageQueueSystemV", 8);
if (key == -1) {
printf("exit: ftok error, %s\n", strerror(errno)); // error
exit(0);
}
/* open existed message queue */
mqFd = msgget(key, 0600);
if (mqFd < 0) {
printf("exit: msgget error, %s\n", strerror(errno)); // error
exit(0);
}
/* receive a message */
if (msgrcv(mqFd, &buf, 1 + sizeof(short), 0, IPC_NOWAIT) < 0) { // type is 0
printf("exit: msgrcv error, %s\n", strerror(errno)); // error
exit(0);
}
最佳答案
将评论转移到答案。
我建议:
Maybe there isn't a message waiting? Where did you write the code to send the message to the queue?
响应是:
Firstly, I run a program to create message queue and send a message. Then use command
ipcs -q
to check whether it succeeds. Then I start another process to receive that message.
狡辩开始了:
The output of
ipcs
says there are no messages queued. You toldmsgrcv()
not to wait, so it didn't, so it returned "there are no messages queued". What am I not understanding?
而且,看来,这是正确的诊断。
关于c - 为什么msgrcv返回ENOMSG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38448912/