免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3558 | 回复: 5
打印 上一主题 下一主题

请教关于POSIX消息队列的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-10-11 10:23 |只看该作者 |倒序浏览
想请教各位关于POSIX消息队列的问题,就是我按照<UNIX网络编程>卷2中编写了程序之后, 可以编译成功, 但是运行时问题报错, 说是 参数不正确.
想请教一下在Linux下该如何来使用POSIX消息队列? 谢谢.
如下是代码:

  1. #include<stdio.h>
  2. #include<mqueue.h>
  3. #include<stdlib.h>
  4. #include<unistd.h>
  5. #include<sys/types.h>
  6. int main(int argc, char **argv)
  7. {
  8.         int c, flags;
  9.         mqd_t mqd;
  10.         flags = O_RDWR|O_CREAT;
  11.         while ((c = getopt(argc, argv, "e")) != -1)
  12.         {
  13.                 switch(c)
  14.                 {
  15.                         case 'e':
  16.                                 flags |= O_EXCL;
  17.                                 break;
  18.                 }
  19.         }

  20.         printf("optind = %d, argc = %d\n", optind, argc);
  21.         if (optind != argc - 1)
  22.         {
  23.                 printf("usage: mqcreate [-e]\n");
  24.                 exit(1);
  25.         }

  26.         if ((mqd = mq_open(argv[optind], flags, 0600, NULL)) == -1)
  27.         {
  28.                 printf("mq_open error\n");
  29.                 perror("<errno>");
  30.                 exit(1);
  31.         }
  32.         mq_close(mqd);
  33.         return 0;
  34. }
复制代码

我使用gcc mqcreate1.c -o mqcreate -lrt来进行编译, 在运行时报的错为
./mqcreate test
<errno>: Invalid argument
请教一下该怎样来使用. 谢谢!

论坛徽章:
0
2 [报告]
发表于 2007-10-11 10:32 |只看该作者
不会也要帮顶

论坛徽章:
0
3 [报告]
发表于 2007-10-11 10:40 |只看该作者
According to mq_overview(7) the queue name needs to begin with a /.   
It'll fail the second time, of course, because you've got the O_EXCL
flag set.

论坛徽章:
0
4 [报告]
发表于 2007-10-11 10:51 |只看该作者
原帖由 xds2000 于 2007-10-11 10:40 发表
According to mq_overview(7) the queue name needs to begin with a /.   
It'll fail the second time, of course, because you've got the O_EXCL
flag set.


Thank you !

论坛徽章:
0
5 [报告]
发表于 2008-05-10 11:46 |只看该作者
上面的程序我用下面命令运行:
/a/src/unp2/pxmsg $ ./mqcreate1.out /.abc
/a/src/unp2/pxmsg $ ./mqcreate1.out -e /.abc
mq_open() error: File exists
问题1. 我想知道刚创建的这个文件在什么地方?
/a/src/unp2/pxmsg $ ./mqcreate1.out /tmp/tmp.abc
mq_open() error: Permission denied
问题2. /tmp目录怎么不让写呢?

论坛徽章:
0
6 [报告]
发表于 2012-05-16 09:50 |只看该作者
创建的消息队列的名字错了,应该为 /test,有个斜杠,你试试
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP