开发者社区> 问答> 正文

为什么没有创建味精队列

正在学习消息队列,编写了代码以创建消息队列

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <stdlib.h>
#include <errno.h>

int main()
{
    key_t key;
    int msgid;

    key = ftok("proj", 64);
    if (key == -1) {
        perror("ftok failed");
        exit(1);
    }
    printf("key:%x\n", key);
    //IPC_CREAT: creating message queue if not exists
    msgid = msgget(key, IPC_CREAT);
    if (msgid == -1) {
        perror("msgget failed");
        printf("errno:%d\n", errno);
        if (errno == ENOENT)
            printf("No message queue exists for key and msgflg did not specify IPC_CREAT\n");
        exit(2);
    }
    printf("msgid:%x\n", msgid);


    return 0;
}

运行命令未显示输出:ipcs -q

panther2@ubuntu:~/c_codes/msg_queue$ ipcs -q

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages

你们能告诉我我是否犯错了

展开
收起
祖安文状元 2020-01-05 18:56:32 294 0
1 条回答
写回答
取消 提交回答
  • 如我所见,您的代码没有错,但是即使在我的系统上,该行为也确实很奇怪。

    当mssget返回0时,一切正常(应该返回一个非负数,0为),并且可以使用队列。

    我for(;;);在您的编末尾添加了一个,然后重新启动。ipcs现在显示:

    0x4025077b 0克鲁德0 0 0

    之后,我ipcrm -q 0并再次启动程序,我每次运行一个新的ID。现在,我删除了无尽的循环,所有所有操作仍然有效,每次运行时,我都会得到一个具有不同编号的消息队列,在下一次运行之前,我总是必须销毁该消息队列。

    真是奇怪!

    我发现了很多有关该主题的报告,例如:https : //www.unix.com/programming/248572-msgget-2-returns-0-workaround-fix.html http://forums.codeguru.com/showthread。 php?403036-奇怪的问题在使用msgget%28%29-在Linux中

    如果您找到有效的解决方案,请及时通知我们!

    由于我的系统现在每次运行时都会生成一个id> 0的新消息队列,因此我不再能够重现此行为。我不想再次重启;)

    2020-01-05 18:56:40
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载