XSI inter-process communication-----Message Queuing

Source: Internet
Author: User
Tags message queue

1. Basic Features

1) Message Queuing is a data link table that is stored and managed by the kernel of the system and referenced by the message queue identity, and the difference between Message Queuing and a well-known pipeline FIFO is that the latter can only have one package at a time, while the former may put a lot of packets, so that it can handle the problem of fast and slow packets.
2) You can create a new message queue through the Msgget function, or get an existing message queue. A message is appended to the back end of the message queue via the MSGSND function (send), and the message is extracted from the front end of the message queue through the MSGRCV (receive) function.
3) Each message unit in the message queue contains the message type and data length in addition to the message data. The existence of message types: The same message queue handles different types of messages, such as processing deposits and withdrawals now, so we can define two types of data instead of defining two message queues
4) The kernel maintains a Message Queuing object in the form of a MSQID_DS structure for each message queue.

Truct msqid_ds {    struct ipc_perm msg_perm;     Permission Information    time_t          msg_stime;    Subsequent send time    time_t          msg_rtime;    Last receive time    time_t          msg_ctime;    Last change time    unsigned long   __msg_cbytes;//The number of bytes in the message queue    msgqnum_t       msg_qnum;     The number of messages in the message queue    msglen_t        msg_qbytes;   The maximum number of bytes The message queue can hold    pid_t           msg_lspid;    Last send process PID    pid_t           msg_lrpid;    Last receive process PID};
struct Ipc_perm {    key_t          __key;//Key value    uid_t          uid;   Valid for Master ID    gid_t          gid;   Valid genus group ID    uid_t          cuid;  Valid Creator ID    gid_t          cgid;  Effectively create group ID    unsigned short mode;  Permission Word    unsigned short __seq;//serial number};

2. Common Functions

Header files: #include <sys/msg.h>

1) create / get message Queue

int Msgget (key_t key, int msgflg);
A. The function creates a message queue with the key parameter for the value, or gets the existing message queue.
B. MSGFLG Value:
0-Gets, does not exist or fails.
Ipc_creat-Creates, does not exist that is created, already exists that is acquired, unless ...
IPC_EXCL-exclusion, which already exists that fails.
C. Successful return message queue identity msqid, failed return-1.

2) send message to message queue

int msgsnd (int msqid, const void* msgp,size_t msgsz, int msgflg);
A. The MSGP parameter points to a block of memory that contains the message type and message data. The first 4 bytes of the memory block must be an integer greater than 0, representing the message type, followed by the message data. The byte length of the message data is represented by the MSGSZ parameter.

Note : The MSGSZ parameter does not contain the number of bytes of the message type (4).

B. If the message queue buffer in the kernel has enough free space (set by the kernel parameter), this function will copy the message into the buffer and return 0 immediately, indicating that the send is successful, or the function will block until the Message Queuing buffer in the kernel has enough free space
C. If the MSGFLG parameter contains ipc_nowait bits, the function does not block when the Message Queuing buffer in the kernel does not have enough free space, but instead returns -1,errno to Eagain.
D. Successful return 0, failure return-1.
3) receive messages from Message Queuing

ssize_t MSGRCV (int msqid, void* msgp, size_t msgsz,long msgtyp, int msgflg);
A. The MSGP parameter points to a block of memory that contains the message type (4 bytes), and the message data.
B. If the received message data byte number is greater than the MSGSZ parameter, that is, the message is too long, and the MSGFLG parameter contains the MSG_NOERROR bit, the message is truncated msgsz bytes are returned and the remainder is discarded.
C. If the MSGFLG parameter does not contain a msg_noerror bit, and the message is too long, no processing is done for the message, and 1 is returned directly.
D. The Msgtyp parameter indicates what type of message is expected to be received: 10 and 20 (note: The Msgtype parameter does not contain the number of bytes of the message type (4). )
= 0-Returns the first message in the message queue.
>0-If the MSGFLG parameter does not contain a msg_except bit, the first message in the message queue of type Msgtyp is returned, and if the MSGFLG parameter contains a msg_except bit, the first message in the message queue that is not of type Msgtyp is returned.
<0-Returns a message in the message queue with a type less than or equal to the absolute value of Msgtyp. If there are more than one, the least type is taken.
E. If there is a message queue that can receive messages, this function moves the message out of the message queue and returns 0 immediately, indicating that the receive succeeds, or the function blocks until there is a message in the message queue to receive.
F. If the MSGFLG parameter contains a ipc_nowait bit, the function does not block when there are no messages in the message queue, but returns -1,errno to Enomsg.
G. The number of bytes that successfully returned the received message data, failure returns-1.
4) Destruction/ControlMessage Queuing
int msgctl (int msqid, int cmd, struct msqid_ds* buf);
A. CMD value:
Ipc_stat-Gets the properties of the message queue, which is output through the BUF parameter.
Ipc_rmid-Deletes Message Queuing immediately. At this point all the msgsnd and MSGRCV function calls that are blocked on the message queue will immediately return the failure errno to EIDRM.
Ipc_set-Sets the properties of the message queue, entered with the BUF parameter, only four properties can be set

B. Successful return 0, failure return-1.


3. Programming model


 #include <stdio.h> #include <string.h> #include <sys/msg.h>int main ( void) {printf ("create message queue ... \ n"), key_t key = Ftok (".", +), if (key = =-1) {perror ("Ftok"); return-1;} int msqid = Msgget (Key, 0644 | Ipc_creat | IPC_EXCL); if (msqid = =-1) {perror ("Msqget"); return-1;} printf ("Send data to Message Queuing (0x%08x/%d) ... \ n", key, Msqid); for (;;) {printf (">"); struct {long Mtype;char mtext[1024];} Msgbuf = {1234, ""};gets (Msgbuf.mtext); strcmp (Msgbuf.mtext, "!")) Break;if (msgsnd (Msqid, &msgbuf, (strlen (Msgbuf.mtext) + 1) *sizeof (msgbuf.mtext[0]), 0) = =-1) {perror ("msgsnd"); return-1;}} printf ("Destroy Message Queuing (0x%08x/%d) ... \ n", key, Msqid), if (Msgctl (Msqid, Ipc_rmid, NULL) = =-1) {perror ("Msgctl"); return-1;} printf ("Done! \ n "); return 0;} 
#include <stdio.h> #include <unistd.h> #include <errno.h> #include <sys/msg.h>int main (void) { printf ("Get message queue ... \ n"); key_t key = Ftok (".", +); if (key = =-1) {perror ("Ftok"); return-1;} int msqid = msgget (key, 0), if (msqid = =-1) {perror ("Msgget"); return-1;} printf ("Receive messages from Message Queuing (0x%08x/%d) ... \ n", key, Msqid); for (;;) {struct {long mtype;char mtext[1024];} Msgbuf = {};ssize_t Msgsz = MSGRCV (Msqid, &msgbuf,sizeof (msgbuf.mtext)-sizeof (msgbuf.mtext[0]), 1234,MSG_NOERROR/ * | ipc_nowait*/); if (Msgsz = =-1) if (errno = = eidrm) {printf ("Message Queuing (0x%08x/%d) has been destroyed! \ n ", key, Msqid); ElseIf (errno = = enomsg) {printf ("No message now, do something else ... \ n"); sleep (1);} else {perror ("MSGRCV"); return-1;} elseprintf ("%04d<%s\n", Msgsz, Msgbuf.mtext);} printf ("Done! \ n "); return 0;}

Command: ipcs-q to view Message Queuing information





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

XSI inter-process communication-----Message Queuing

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.