码迷,mamicode.com
首页 > 其他好文 > 详细

Unix IPC之Posix消息队列(3)

时间:2015-08-13 22:08:48      阅读:351      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
struct mq_attr
{
    long mq_flags;      /* message queue flag : 0, O_NONBLOCK */
    long mq_maxmsg;     /* max number of messages allowed on queue*/
    long mq_msgsize;    /* max size of a message (in bytes)*/
    long mq_curmsgs;    /* number of messages currently on queue */
};

/* Receive the oldest from highest priority messages in message queue
   MQDES.  */
ssize_t mq_receive (mqd_t __mqdes, char *__msg_ptr, size_t __msg_len,
                    unsigned int *__msg_prio);

/* Add message pointed by MSG_PTR to message queue MQDES.  */
int mq_send (mqd_t __mqdes, __const char *__msg_ptr, size_t __msg_len,
             unsigned int __msg_prio);
View Code

 

程序1:向消息队列中发送消息。可指定消息大小及优先级。

技术分享
// mqsend.c

#include    "unpipc.h"

int
main(int argc, char **argv)
{
    mqd_t   mqd;
    void    *ptr;
    size_t  len;
    unsigned int  prio;

    if (argc != 4)
        err_quit("usage: mqsend <name> <#bytes> <priority>");
    len = atoi(argv[2]);
    prio = atoi(argv[3]);

    mqd = Mq_open(argv[1], O_WRONLY);

    ptr = Calloc(len, sizeof(char)); // 使用calloc函数分配一个长度为len的缓冲区,默认被初始化为0
    Mq_send(mqd, ptr, len, prio); // 写入消息队列

    exit(0);
}
View Code


程序2:从消息队列读出消息。

技术分享
// mqreveive.c

#include    "unpipc.h"

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

    flags = O_RDONLY;
    while ( (c = Getopt(argc, argv, "n")) != -1)
    {
        switch (c)
        {
            case n: // 命令行中带-n表示以非阻塞模式
                flags |= O_NONBLOCK;
                break;
        }
    }
    if (optind != argc - 1)
        err_quit("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); // 这里的len长度需要>=消息的最大长度,否则mq_receive会立即返回EMSGSIZE错误
    printf("read %ld bytes, priority = %u\n", (long) n, prio);

    exit(0);
}
View Code


运行结果:

[dell@localhost pxmsg]$ ls -l /tmp/mqueue/
总用量 0
[dell@localhost pxmsg]$ ./mqcreate1 /msgqueue
[dell@localhost pxmsg]$ ls -l /tmp/mqueue/
总用量 0

[dell@localhost pxmsg]$ sudo mount -t mqueue none /tmp/mqueue
[sudo] password for dell: 
[dell@localhost pxmsg]$ ls -l /tmp/mqueue/
总用量 0
-rw-r--r--. 1 dell dell 80 8月  13 20:52 msgqueue

[dell@localhost pxmsg]$ ./mqsend /msgqueue 10 1
[dell@localhost pxmsg]$ ./mqsend /msgqueue 20 2
[dell@localhost pxmsg]$ ./mqsend /msgqueue 30 3
[dell@localhost pxmsg]$ ls -l /tmp/mqueue/
总用量 0
-rw-r--r--. 1 dell dell 80 8月  13 20:55 msgqueue

[dell@localhost pxmsg]$ ./mqreceive /msgqueue
read 30 bytes, priority = 3
[dell@localhost pxmsg]$ ./mqreceive /msgqueue
read 20 bytes, priority = 2
[dell@localhost pxmsg]$ ./mqreceive /msgqueue
read 10 bytes, priority = 1
[dell@localhost pxmsg]$ ./mqreceive /msgqueue
^C

[dell@localhost pxmsg]$ ./mqreceive -n /msgqueue
mq_receive error: Resource temporarily unavailable
[dell@localhost pxmsg]$ 

 

Unix IPC之Posix消息队列(3)

标签:

原文地址:http://www.cnblogs.com/fengkang1008/p/4728444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!