【问题标题】:POSIX4 messages queues "mq_open: No such file or directory"POSIX4 消息队列“mq_open:没有这样的文件或目录”
【发布时间】: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,我有同样的错误。

标签: c posix mq


【解决方案1】:

mq_open returns (mqd_t) -1 on failure and a message queue descriptor on success.

您将mq_open(实际上是一个>= 0 的整数)的成功返回误认为是失败,而perror 正在报告某个先前系统调用的errno

【讨论】:

    猜你喜欢
    • 2020-05-31
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 2023-03-09
    • 2015-02-20
    相关资源
    最近更新 更多