我正在按照 The Linux Programming Interface 使用以下代码片段创建消息队列.
if((mq_open("/my_message_queue", O_CREAT, O_RDWR, NULL)) == -1) {
perror("mq creation failed");
}
运行此代码片段时出现错误:“权限被拒绝”。我想检查一下我之前是否创建了队列并且没有销毁它,所以我使用了 ipcs。但是,ipcs 不显示任何事件消息队列。我以前从未在我的开发环境(Ubuntu 18.04)中使用过 POSIX IPC 库。我是否必须进行一些设置才能允许我的用户进程创建消息队列?我是否错误地使用了 API?
最佳答案
来自the man page :
The oflag argument specifies flags that control the operation of the call. (Definitions of the flags values can be obtained by including
<fcntl.h>
.) Exactly one of the following must be specified in oflag:O_RDONLY Open the queue to receive messages only.
O_WRONLY Open the queue to send messages only.
O_RDWR Open the queue to both send and receive messages.
您的代码中没有这三个值。或者更确切地说,您这样做,但它在 mode
中参数,而不是 oflag
一,其中对应的数字具有完全不同的含义。第三个参数是创建队列时使用的文件系统权限位(就像创建新文件时 open()
的第三个参数一样),而不是打开队列的模式。
关于c - 尝试使用 POSIX 消息队列创建消息队列时权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58477463/