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

来源:互联网 发布:c语言标准库是什么 编辑:程序博客网 时间:2024/05/16 17:57
#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

原创粉丝点击