POSIX Message Queuing for Unix IPC (1)

Source: Internet
Author: User

Section reference: http://www.cnblogs.com/Anker/archive/2013/01/04/2843832.html

The persistence of IPC objects: http://book.51cto.com/art/201006/207275.htm

Message Queuing can be thought of as a list of messages that, before a process writes a message to a message queue, does not require another process to wait for the message to arrive on that queue, as opposed to a pipeline and a FIFO. The differences between POSIX Message Queuing and System V Message Queuing are as follows:

1. The read of POSIX Message Queuing always returns the oldest message with the highest priority, and the read of System V Message Queuing can return messages of any given priority.
2. When a message is placed into an empty queue, POSIX Message Queuing allows a signal to be generated or a thread to start, and System V Message Queuing does not provide a similar mechanism.

The POSIX Message Queuing operation functions are as follows:

header files and some definitions:

#include    <mqueue.h>int mqd_t;
/*establish connection between a process and a message queue NAME and return message Queue descriptor or (mqd_t)-1 o  N error.  OFLAG determines the type of access used.    If O_creat is on OFLAG, the third argument is taken as a ' mode_t ', the mode of the created message queue, and the fourth  argument is taken as ' struct mq_attr * ', pointer to message queue attributes.  If The fourth argument is NULL, the default attributes is used. */externmqd_t Mq_open (__constChar*__name,int__oflag, ...) __throw __nonnull ((1));/*removes the association between message Queue descriptor Mqdes and its message queue. */extern intmq_close (mqd_t __mqdes) __throw;/*Remove Message Queue named NAME. */extern intMq_unlink (__constChar*__name) __throw __nonnull (1));


The following function is used to write the program process test.

Program 1 (mqcreate1.c): Creates a message queue whose name is specified as a command-line argument. The procedure is as follows:

#include"unpipc.h"intMain (intargcChar**argv) {    intC, flags; mqd_t MQD; //under Linux is the int typeFlags= O_rdwr |o_creat;  while(c = Getopt (argc, argv,"e")) != -1)    {        Switch(c) { Case 'e': Flags|=o_excl;  Break; }    }    if(Optind! = argc-1) Err_quit ("Usage:mqcreate [-e] <name>"); MQD=Mq_open (Argv[optind], flags, File_mode, NULL);    Mq_close (MQD); Exit (0);}
View Code

Program 2 (MQUNLINK.C): Deletes a message queue. The procedure is as follows:

#include    "unpipc.h"intmain (intChar * *  argv) {    if2)        err_quit ("usage:mqunlink <name >");    Mq_unlink (argv[1]);    Exit (0);}
View Code

Note: The code needs to be compiled in the UNIX Network programming-Volume 2 package.

POSIX Message Queuing is built into the system's virtual file system and can be mounted to the system's file system for viewing;

The Mount command format is as follows:

The Mount command is as follows:

[[email protected] pxmsg]$ mkdir/tmp/-t mqueuenone/tmp/do-1  -T mqueue none/tmp/for

Program Run Result:

[Email protected] pxmsg]$/mqunlink/temp.1234[dell@localhost pxmsg]$ ls-l/tmp/mqueue/0[Dell @localhost pxmsg]$/mqcreate1/temp.1234[dell@localhost pxmsg]$ ls-l/tmp/mqueue/0-rw-r--r-- . 1 Dell Dell 80 August  20:10 temp.1234[Dell@localhost pxmsg]$./mqunlink/temp.1234[Dell@localhost pxmsg]$ ls-l/tmp/mqueue/0[Dell

Description: Why do you want to do this, run a command query under the shell:

Mans 7 Mq_overview

Here you select some of the documentation:

mounting the message queue file system        inch Virtual file system.   (Other       implementations  -  also  provide such a feature, but the details is       likely to differ.)  This file system can is mounted (by the  superuser)       using the following commands:           
    /dev/mqueue           -t mqueue none/dev/mqueueeach message  queueis  Identi fied by a name of the form/somename.  The processes can operate on thesame queue by passing the same name to Mq_open ().

POSIX Message Queuing for Unix IPC (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.