您的位置:首页 > 其它

Posix消息队列的基本操作——创建或打开

2013-06-12 14:25 309 查看
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>           /* For O_* constants */
#include <sys/stat.h>        /* For mode constants */
#include <mqueue.h>

#define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)

struct mq_attr attr;

int main(int argc, char**argv)
{
int c, flags;
mqd_t   mqd;

flags = O_RDWR | O_CREAT;

while((c = getopt(argc, argv,"em:z:")) != -1)
{
switch(c)
{
case 'e':
flags |= O_EXCL;
break;

case 'm':
attr.mq_maxmsg = atol(optarg);
break;

case 'z':
attr.mq_msgsize = atol(optarg);
break;
}
}

if(optind != argc - 1)
{
printf("usage: mqcreate [-e] [ -m maxmsg -z msgsize] <name>");
}

if((attr.mq_maxmsg != 0 && attr.mq_msgsize == 0) || (attr.mq_maxmsg == 0 && attr.mq_msgsize != 0))
{
printf("must specify both -m maxmsg and -z msgsie\n");
}
mqd = mq_open(argv[optind],flags,FILE_MODE,(attr.mq_maxmsg != 0) ? &attr : NULL);

mq_close(mqd);

exit(0);
}


编译的时候注意加参数  -lrt
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: