问题描述
我通过遵循 Linux编程接口来创建带有以下代码段的消息队列.
I am creating a message queue with the following snippet by following 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的方式不正确吗?
Running this snippet I get an error: "permission denied". I wanted to check and see if I had created the queue previously and not destroyed it, so I used ipcs. However, ipcs does not show any active message queues. I have never used the POSIX IPC libraries in my development environment before (Ubuntu 18.04). Is some set up I must do to allow my user process to create a message queue? Am I using the API incorrectly?
推荐答案
来自手册页:
O_RDONLY打开队列仅接收消息.
O_RDONLY Open the queue to receive messages only.
O_WRONLY打开队列以仅发送消息.
O_WRONLY Open the queue to send messages only.
O_RDWR打开队列以发送和接收消息.
O_RDWR Open the queue to both send and receive messages.
您的代码中没有这三个值.确切地说,您可以这样做,但是它是在mode
参数中,而不是在oflag
参数中,其中相应的数字具有完全不同的含义.第三个参数是创建队列时使用的文件系统权限位(就像创建新文件时open()
的第三个参数一样),而不是打开队列的方式.
You have none of those three values in your code. Or rather you do, but it's in the mode
argument, not the oflag
one, where the corresponding number has a completely different meaning. That third argument is the filesystem permission bits used when creating the queue (just like the third argument to open()
when creating a new file), not the mode the queue is opened in.
这篇关于尝试使用POSIX消息队列创建消息队列时,权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!