Posix消息队列的基本操作——接收消息

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <mqueue.h>

int main(int argc, char** argv)
{
    int c, flags;
    mqd_t   mqd;
    size_t  prio;
    ssize_t n;
    void*   buff;
    struct mq_attr  attr;

    flags = O_RDONLY;

    while((c = getopt(argc, argv, "n")) != -1)
    {
        switch(c)
        {
            case 'n':
            {
                flags |= O_NONBLOCK;
                break;
            }
        }
    }

    if(optind != argc -1)
    {
        printf("usage:mqreceive [ -n ] <name>");
    }

    mqd = mq_open(argv[optind] , flags);

    mq_getattr(mqd, &attr);

    buff = malloc(attr.mq_msgsize);

    n = mq_receive(mqd, buff, attr.mq_msgsize, &prio);

    printf("read %ld bytes, priority = %u\n", (long)n, prio);

    exit(0);
}

你可能感兴趣的:(Posix消息队列的基本操作——接收消息)