linux message queues

原创 2016年06月01日 02:36:56


Overview of Message Queues

A message queue is a software object maintained by the IRIX kernel, logically apart from the address space of any process. When you create a message queue, the queue has a public identifier. (The identifier is a file pathname for POSIX, or an integer for SVR4.) A process uses the identifier to open the queue. When the queue is open, the process can send messages to the queue or receive messages from the queue.

A message queue has an access mode similar to a file access mode, specifying read and write access for its owner, its owner's group, or all users. A process with an effective user ID giving only read access can only receive messages from the queue. A process with an effective user ID lacking access cannot open the queue.

When a process requests a message from a queue and no message is available, the process can be notified immediately with an error code, or it can be suspended until a message is sent.

A message queue has a limit on the amount of data that can be queued. (POSIX limits the number of messages; SVR4 limits the total size of queued messages.) When a process sends a message that would exceed the queue's limit, the process can be notified immediately with an error code, or it can be suspended until there is room in the queue.

Table 6-1. Abstract Operations on a Message Queue

Operation

POSIX Function

SVR4 Function

Gain access to a queue, creating it if it does not exist.

mq_open(3)

msgget(2)

Query attributes of a queue and number of pending messages.

mq_getattr(3)

msgctl(2)

Change attributes of a queue.

mq_setattr(3)

msgctl(2)

Give up access to a queue.

mq_close(3)

n.a.

Remove a queue from the system.

mq_unlink(3), rm(1)

msgctl(2), ipcrm(1)

Send a message to a queue.

mq_send(3)

msgsnd(2)

Receive a message from a queue.

mq_receive(3)

msgrcv(2)

Request asynchronous notification of a message arriving at a queue.

mq_notify(3)

n.a.

Both implementations can be used to communicate between POSIX threads and between IRIX processes in any combination. Besides obvious features of syntax, the principal differences between the two implementations are as follows:

  • POSIX functions are implemented as library functions in the libc library and operate primarily in the user process address space. SVR4 functions are implemented in the kernel, and every operation requires a context switch. This generally results in lower overhead for the POSIX functions.

  • The identity of a POSIX or an SVR4 queue is retained over a reboot. The contents of a POSIX queue might or might not survive a reboot, but you should not depend on either type of queue to retain its state after the last program closes it.

  • POSIX allows you to set a limit on the number of messages and the size of one message. SVR4 allows you to set a limit on the aggregate size of queued messages, but not on their number or their individual sizes.

  • With a POSIX queue, the choice of whether or not operations should block on a full or empty queue is an attribute of the queue descriptor. With SVR4, you specify blocking or nonblocking operation on each send or receive operation.

  • POSIX supports asynchronous notification of a message arrival. SVR4 does not.

  • SVR4 allows a receiver to request a message from a particular priority class, in effect creating sub-queues within a queue. POSIX supports a priority class on each message, but it always returns the first message of the highest priority class.

example code:

server:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <mqueue.h>

#include "common.h"

int main(int argc, char **argv)
{
    mqd_t mq;
    struct mq_attr attr;
    char buffer[MAX_SIZE + 1];
    int must_stop = 0;

    /* initialize the queue attributes */
    attr.mq_flags = 0;
    attr.mq_maxmsg = 10;
    attr.mq_msgsize = MAX_SIZE;
    attr.mq_curmsgs = 0;

    /* create the message queue */
    mq = mq_open(QUEUE_NAME, O_CREAT | O_RDONLY, 0644, &attr);
    CHECK((mqd_t)-1 != mq);

    do {
        ssize_t bytes_read;

        /* receive the message */
        bytes_read = mq_receive(mq, buffer, MAX_SIZE, NULL);
        CHECK(bytes_read >= 0);

        buffer[bytes_read] = '\0';
        if (! strncmp(buffer, MSG_STOP, strlen(MSG_STOP)))
        {
            must_stop = 1;
        }
        else
        {
            printf("Received: %s\n", buffer);
        }
    } while (!must_stop);

    /* cleanup */
    CHECK((mqd_t)-1 != mq_close(mq));
    CHECK((mqd_t)-1 != mq_unlink(QUEUE_NAME));

    return 0;
}

client:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <mqueue.h>

#include "common.h"


int main(int argc, char **argv)
{
    mqd_t mq;
    char buffer[MAX_SIZE];

    /* open the mail queue */
    mq = mq_open(QUEUE_NAME, O_WRONLY);
    CHECK((mqd_t)-1 != mq);


    printf("Send to server (enter \"exit\" to stop it):\n");

    do {
        printf("> ");
        fflush(stdout);

        memset(buffer, 0, MAX_SIZE);
        fgets(buffer, MAX_SIZE, stdin);

        /* send the message */
        CHECK(0 <= mq_send(mq, buffer, MAX_SIZE, 0));

    } while (strncmp(buffer, MSG_STOP, strlen(MSG_STOP)));

    /* cleanup */
    CHECK((mqd_t)-1 != mq_close(mq));

    return 0;
}

common header:

#ifndef COMMON_H_
#define COMMON_H_

#define QUEUE_NAME  "/test_queue"
#define MAX_SIZE    1024
#define MSG_STOP    "exit"

#define CHECK(x) \
    do { \
        if (!(x)) { \
            fprintf(stderr, "%s:%d: ", __func__, __LINE__); \
            perror(#x); \
            exit(-1); \
        } \
    } while (0) \


#endif /* #ifndef COMMON_H_ */

References

1. http://csweb.cs.wfu.edu/~torgerse/Kokua/More_SGI/007-2478-008/sgi_html/ch06.html

Linux POSIX Message Queue 使用体会

最近正在Linux上使用POSIX Message Queue(以下简称MQ)在进程间通讯,对目前我这系统发行版和编译器来讲,MQ用起来有一点体会,是教程是没有说明的,或者我看的不够仔细,没有发现 ...
  • huyaoyu
  • huyaoyu
  • 2015年06月08日 20:07
  • 1463

Linux进程通信之POSIX消息队列

消息队列是Linux IPC中很常用的一种通信方式,它通常用来在不同进程间发送特定格式的消息数据。 消息队列和之前讨论过的16.1管道和FIFO有很大的区别,主要有以下两点: l 一个进程向消息队列写...
  • anonymalias
  • anonymalias
  • 2013年08月07日 08:53
  • 11299

Linux进程通信之POSIX消息队列

消息队列是Linux IPC中很常用的一种通信方式,它通常用来在不同进程间发送特定格式的消息数据。 消息队列和之前讨论过的16.1管道和FIFO有很大的区别,主要有以下两点: l 一个进程向消息队列写...
  • anonymalias
  • anonymalias
  • 2013年08月07日 08:53
  • 11299

深入理解MessageQueue

Android 中有两个非常重要的知识点,分别是Binder机制和Handler机制。前者用于跨进程通讯,并且通过 ServiceManager 给上层应用提供了大量的服务,而后者用于进程内部通讯,以...
  • kisty_yao
  • kisty_yao
  • 2017年05月05日 09:32
  • 1728

linux下消息队列(Message queue)

1.消息队列 消息队列(英语:Message queue)是一种进程间通信或同一进程的不同线程间的通信方式。 就是一个消息的链表,可以把一条消息看做一个记录,用户可以从中读取读取消息或者向其中...
  • shayueqing
  • shayueqing
  • 2013年09月18日 16:47
  • 2279

Linux 下进程间通信机制(五) 消息队列Message Queues

消息队列函数定义如下: #include int msgctl(int msqid, int cmd, struct msqid_ds *buf); int msgget(key_t ke...
  • zdcsky123
  • zdcsky123
  • 2011年08月25日 09:40
  • 2811

About Messages and Message Queues

目录: Windows Messages Message Types System-Defined Messages Application-Defined Messages Message R...
  • zhuhuangtianzi
  • zhuhuangtianzi
  • 2014年08月27日 08:33
  • 676

Dissecting Message Queues(不同消息中间件Throughput和latency

转载自:http://bravenewgeek.com/dissecting-message-queues/ 有一些结果图片无法显示,可以去原文查看. Continuing my series...
  • taolinke
  • taolinke
  • 2016年01月15日 20:03
  • 1660

Lecture 22: Queues and Message-Passing

1 Message passing with threadsStrategy: Use a synchronized queue for message passing between threads...
  • jitianyu123
  • jitianyu123
  • 2017年04月07日 15:50
  • 100

Optimizing Data Access and Messaging - Idempotency for Windows Azure Message Queues

Idempotency is the mathematical term used to describe a system that produces the same result when a ...
  • riverlau
  • riverlau
  • 2011年12月29日 11:29
  • 289
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:linux message queues
举报原因:
原因补充:

(最多只允许输入30个字)