【发布时间】:2016-09-10 20:47:16
【问题描述】:
我正在尝试使用 POSIX4 消息队列。所以我使用mq_open 来创建队列,对于我给它的所有选项,我都会填写一个struct mq_attr。
他找不到队列,而我放置了O_CREATE 标志。
这是我的代码(无缩进的行是调试代码):
...
/***
* Queues' names
*/
#define GUI_QUEUE "/guiQ"
...
struct mq_attr attrAct; /* Queue parameters */
/***
* Message queue to send action
*/
attrAct.mq_maxmsg=1;
attrAct.mq_msgsize=sizeof(gui_action);
attrAct.mq_flags=0;
attrAct.mq_curmsgs=0;
printf("serveur first sizeof(gui_action) : %lu\tmsgsize : %lu\n", sizeof(gui_action), attrAct.mq_msgsize);
if ((guiQue=mq_open(GUI_QUEUE, O_CREAT | O_NONBLOCK | O_WRONLY
, S_IWUSR | S_IRUSR , &attrAct))!=0) {
perror("mq_open");
exit(EXIT_FAILURE);
}
if (mq_getattr(guiQue, &attrAct)!=0) {
perror("mq_getattr");
}
else {
printf("serveur second sizeof(gui_action) : %lu\tmsgsize : %lu\n", sizeof(gui_action), attrAct.mq_msgsize);
}
struct mq_attr new;
new=attrAct;
new.mq_msgsize=sizeof(gui_action);
printf("serveur third sizeof(gui_action) : %lu\tmsgsize : %lu\n", sizeof(gui_action), new.mq_msgsize);
if (mq_setattr(guiQue, &new, &attrAct)!=0) perror("mq_setattr");
if (mq_getattr(guiQue, &attrAct)!=0) {
perror("mq_getattr");
}
else {
printf("serveur fourth sizeof(gui_action) : %lu\tmsgsize : %lu\n", sizeof(gui_action), attrAct.mq_msgsize);
}
...
这是输出:
serveur first sizeof(gui_action) : 16 msgsize : 16
mq_open: No such file or directory
我做错了什么?
【问题讨论】:
-
您试图在文件系统的根目录上打开一个队列。该代码没有这样做的权限。
-
是的,但是如果我输入
"guiQ/"或"guiQ",就会出现无效参数错误。那么,如何在用户空间中打开一个 mq 呢? (即使使用“sudo”,它也不起作用) -
查看编译后的可执行代码所在目录,是否有
guiQ,如果没有,需要提供更多调试信息,errno是什么?在根目录中使用它,即/不是拥有它的正确位置。 -
@t0mm13b 如果使用 Linux,您需要根据 mq_overview 手册页以“/”开头的队列命名。
-
我的 dev 目录中没有
guiQ,但/dev/mqueue中有一个,并且我是所有者并且有读写权限。使用 sudo,我有同样的错误。