关于Unix第二卷60页的mq_open 函数的参数非法问题
2017-05-26 17:55
351 查看
utili.h文件:
#ifndef _UTILI_H
#define _UTILI_H
#include<stdio.h>
#include<unistd.h>
#include<errno.h>
#include<stdlib.h>
#include<assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<string.h>
#include<mqueue.h>
#include<wait.h>
#define MAXLEN 1024
#define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
#endif
mqcreate.c 文件:
#include"../utili.h"
int main(int argc,char **argv)
{
int c,flags;
mqd_t mqd;
flags = O_RDWR|O_CREAT;
while((c=getopt(argc,argv,"e"))!=-1)
{
switch(c)
{
case 'e':
flags |= O_EXCL;
break;
}
}
if(optind != argc-1)
{
printf("usage:mqcreate [-e] <name>");
exit(0);
}
mqd = mq_open(argv[optind],flags,FILE_MODE,NULL);
if(mqd == -1)
printf("mq_opem Fail. errno = %d\n",errno);
assert(mqd!=-1);
mq_close(mqd);
exit(0);
}
编译的时候: gcc -o mqcreate mqcreate.c -lrt
如果编译出错执行以下两条命令:
# mkdir /dev/mqueue
# mount -t mqueue none /dev/mqueue
原因是:在Linux系统,消息队列是创建在虚拟文件系统上,而虚拟文件需要挂载
编译通过后 执行: ./mqcreate /mq_name
需要注意的是命令行参数的 /mq_name 必须以这种格式: /mq_name
原因是:每一个消息队列是有一个name来定义的,即是以空字符结尾的字符串形如: /somename
这么命名,原因详细参见Unix第二卷的14页 2.2节 IPC名字:这一小节详细阐述了IPC 名字的规则和移植性问题.
如果还有问题请仔细阅读: man 7 mq_overview
#ifndef _UTILI_H
#define _UTILI_H
#include<stdio.h>
#include<unistd.h>
#include<errno.h>
#include<stdlib.h>
#include<assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<string.h>
#include<mqueue.h>
#include<wait.h>
#define MAXLEN 1024
#define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
#endif
mqcreate.c 文件:
#include"../utili.h"
int main(int argc,char **argv)
{
int c,flags;
mqd_t mqd;
flags = O_RDWR|O_CREAT;
while((c=getopt(argc,argv,"e"))!=-1)
{
switch(c)
{
case 'e':
flags |= O_EXCL;
break;
}
}
if(optind != argc-1)
{
printf("usage:mqcreate [-e] <name>");
exit(0);
}
mqd = mq_open(argv[optind],flags,FILE_MODE,NULL);
if(mqd == -1)
printf("mq_opem Fail. errno = %d\n",errno);
assert(mqd!=-1);
mq_close(mqd);
exit(0);
}
编译的时候: gcc -o mqcreate mqcreate.c -lrt
如果编译出错执行以下两条命令:
# mkdir /dev/mqueue
# mount -t mqueue none /dev/mqueue
原因是:在Linux系统,消息队列是创建在虚拟文件系统上,而虚拟文件需要挂载
编译通过后 执行: ./mqcreate /mq_name
需要注意的是命令行参数的 /mq_name 必须以这种格式: /mq_name
原因是:每一个消息队列是有一个name来定义的,即是以空字符结尾的字符串形如: /somename
这么命名,原因详细参见Unix第二卷的14页 2.2节 IPC名字:这一小节详细阐述了IPC 名字的规则和移植性问题.
如果还有问题请仔细阅读: man 7 mq_overview
相关文章推荐
- mq_open()函数的 attr 参数错误问题
- 关于C#函数对象参数传递的问题
- 关于C++当中的函数作为另一个函数的参数之小问题
- 关于数组做函数参数的问题汇总
- 关于 Numpy 数组作为函数参数的一个小问题
- 【C】关于main()函数参数的问题;
- 关于C#函数对象参数传递的问题
- 关于rs.Open sql,conn语句的参数问题
- 关于数组做函数参数的问题汇总
- C++中关于二维数组作为函数参数传递的问题
- 关于二维数组作为参数传递给函数的问题
- 关于C#函数对象参数传递的问题
- 关于函数中参数传递的问题
- 9月25日-关于Sprite非法参数异常的问题
- 关于fsockopen pfsockopen函数安全问题的公告
- 关于ASP.net中的存储过程"为过程或函数指定的参数太多?"的问题
- 关于C#函数对象参数传递的问题
- 多学多得呀,关于函数的可变参数问题
- 关于C++中open打开文件函数的诡异的问题
- C++关于参数为const引用类型的函数调用参数成员函数的一个问题