免费注册 查看新帖 |

Chinaunix

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

mq_open的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-02-13 15:11 |只看该作者 |倒序浏览
我利用mq_open创建队列
程序提示未定义
在makefile中加入 -lposix4
  结果提示无法找到 -lposixe4
请问linux下使用mq_open需要如何设置
谢谢

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2006-02-13 15:22 |只看该作者
2.6内核吗?
记得2.4 POSIX标准支持的不好。

论坛徽章:
0
3 [报告]
发表于 2006-02-13 15:25 |只看该作者
对,2.4的内核对POSIX支持得很不好
所以特意升级到2.6

已解决,不是加-lposix4,而是-lrt

论坛徽章:
0
4 [报告]
发表于 2006-02-13 15:37 |只看该作者

新的问题

源码如下:
#include <stdio.h>
#include <math.h>
#include <mqueue.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

void errorabort ( void )
{
  char *e = strerror(errno);
  
  printf("%s\n", e);       
}


int main( int C, char **v )
{       
        mqd_t mq;
        int flags;
       
        flags = O_CREAT;
               
        if((mq = mq_open( v[1], flags, 0666, NULL)) == (mqd_t)(-1))
        {
                errorabort();               
        }
       
        printf("mq = %d\n", mq);
       
        for(;
        {
          pause();       
        }
       
       
   return (0);       
}


编译通过后,执行
输入参数/home/user/mq.1234,
结果输出Permission denied
改用ROOT执行也不行

请问是什么原因,谢谢

论坛徽章:
0
5 [报告]
发表于 2006-02-13 16:03 |只看该作者
改用 /1.1234就可以了
也就是只能加一个/

关于这个问题在stevens的UNIX网络编程中有介绍
但是感觉不是很详细,他说这种方式是不标准的标准方式(a standard way of being nonstandard)

  有相同遇到此问题的兄弟吗

论坛徽章:
0
6 [报告]
发表于 2007-12-24 23:21 |只看该作者
根据标准:
http://www.opengroup.org/onlinep ... ctions/mq_open.html

  1. If name begins with the slash character, then processes calling mq_open() with the same value of name shall refer to the same message queue object, as long as that name has not been removed. If name does not begin with the slash character, the effect is implementation-defined. The interpretation of slash characters other than the leading slash character in name is implementation-defined. If the name argument is not the name of an existing message queue and creation is not requested, mq_open() shall fail and return an error.
复制代码

所以可移植的方法是name使用类似"/your_mqueue"的形式,

  1. #include <stdio.h>
  2. #include <string.h>

  3. #include <limits.h>
  4. #include <fcntl.h>
  5. #include <sys/stat.h>

  6. #include <mqueue.h>

  7. #define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)

  8. int main(void)
  9. {
  10.         mqd_t mqd;
  11.         int oflag;
  12.         char name[PATH_MAX + 1];

  13.         oflag = O_CREAT | O_RDWR;
  14.         strcpy(name, "/mq_mymtom");
  15.         mqd = mq_open(name, oflag, FILE_MODE, NULL);
  16.         if ((mqd_t)-1 == mqd)
  17.                 perror("mq_open()");
  18.         else
  19.                 printf("mq_open(): success.\n");

  20.         strcpy(name, "mq_mymtom");
  21.         mqd = mq_open(name, oflag, FILE_MODE, NULL);
  22.         if ((mqd_t)-1 == mqd)
  23.                 perror("mq_open()");
  24.         else
  25.                 printf("mq_open(): success.\n");

  26.         return 0;
  27. }
复制代码

评分

参与人数 1可用积分 +9 收起 理由
MMMIX + 9 精品文章

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP