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

#include 
#include 
#include 
#include            /* For O_* constants */
#include         /* For mode constants */
#include 

#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] ");
    }
    
    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

你可能感兴趣的:(多线程技术)