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

来源:互联网 发布:resilio sync linux 编辑:程序博客网 时间:2024/05/16 20:29
#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);}

原创粉丝点击