c - 为什么msgrcv返回ENOMSG

标签 c linux

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 told msgrcv() 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/

相关文章:

c - sqlite Visual Studio 2015

c - 接受非 ASCII 字符

c - 如何用 C 编写 FTP 客户端程序?

c# - 带有 SSH.NET 的 Linux : “sorry, you must have a tty to run sudo”

c++ - 如何使用 C/C++ 获取数字锁定状态?

linux - awk 根据字符串字符比较将两个文件合并为 2 列

java - Java 中的位数据类型

c - "multiple definition of ` main”在eclipse(c语言)

linux - 符号查找错误 undefined symbol : mysql_init

c++ - 在 C/C++ (Linux)、LGPL 或 BSD 中确定 CPU 速度/内存是一项要求