Linux inter-process communication (System V) --- Message Queue

Source: Internet
Author: User

Linux inter-process communication (System V) --- Message Queue
Message Queue IPC Principle

A message queue is a chained queue of a message, such as a model of a message queue. The entire message queue has two types of data structures.

1. msqid_ds Message Queue Data Structure: Describes the attributes of the entire message queue, it mainly includes the permissions, owner, and two important pointers of the entire message queue (pointing to the first message and the last message of the Message Queue respectively ). 2. msg Message Queue data structure: the main body of the entire message queue. A message queue has several messages, the basic members of each message data structure include the message type, Message Size, message content pointer, and location of the next message data structure.

Message queues can also be processed based on types. However, the FIFO principle of message queues only applies to messages of the same type. In Linux, message queues are defined as follows (values may vary with Linux versions ):

1. By default, the entire system allows a maximum of 16 message queues. 2. Each message queue is up to 16384 bytes. 3. Each message in the message queue is up to 8192 bytes.

These contents are defined in/usr/include/linux/msg. h:

#define MSGMNI    16   /* <= IPCMNI */     /* max # of msg queue identifiers */#define MSGMAX  8192   /* <= INT_MAX */   /* max size of message (bytes) */#define MSGMNB 16384   /* <= INT_MAX */   /* default max size of a message queue */
Basic Attributes of Message Queue

Out of the/usr/include/linux/msg. h file

Message Queue Management

1. Create a message queue:

# Include <sys/types. h> # include <sys/ipc. h> # include <sys/msg. h>/* The first parameter is the key value, which is generally obtained by the ftok () function. The second parameter is the access permission */int msgget (key_t key, int msgflg );

2. Message Queue attribute Control

# Include <sys/types. h> # include <sys/ipc. h> # include <sys/msg. h>/** the first parameter is the message queue ID, which is obtained by msgget * The second parameter is the control command * The third parameter is the data transmission carrier */int msgctl (int msqid, int cmd, struct msqid_ds * buf );

The control command is as follows:

3. Send a message queue

# Include <sys/types. h> # include <sys/ipc. h> # include <sys/msg. h>/** the first parameter is the message queue ID * The second parameter msgp points to the defined buffer * The third parameter is the size of the sent message * The fourth parameter is generally 0, blocking call process */int msgsnd (int msqid, const void * msgp, size_t msgsz, int msgflg );

4. receive messages from the Message Queue

# Include <sys/types. h> # include <sys/ipc. h> # include <sys/msg. h>/** the first parameter is the message queue ID * The second parameter is the temporary message data structure, used to store messages * The third parameter is the size of the received message * The fourth parameter is used to specify the Message Type of the Request * The Fifth parameter is generally set to 0, blocking call process */ssize_t msgrcv (int msqid, void * msgp, size_t msgsz, long msgtyp, int msgflg );

The message data structure should be defined as follows:

/* From/usr/include/linux/msg. h */struct msgbuf {long mtype;/* Message Type */char mtext [1];/* store the message location, which needs to be redefined */};
Message Queue application instance

Create two processes and use the message queue for communication. One process sends messages, and the other process receives messages.

Sender:

# Include <stdio. h> # include <sys/types. h> # include <sys/ipc. h> # include <sys/msg. h> # include <stdlib. h> # include <string. h> struct msgbuf {long mtype; char mtext [50] ;}; int main () {int key, msg_id; static struct msgbuf buf; key = ftok (". ", 1); msg_id = msgget (key, IPC_CREAT | 0600);/* set the Message Type */buf. mtype = 1; while (1) {fgets (buf. mtext, 50, stdin);/* enter quit to exit the process and delete the queue */if (! Strncmp (buf. mtext, "quit", 4) {msgctl (msg_id, IPC_RMID, 0); exit (0) ;}/ * send messages to the queue */msgsnd (msg_id, (void *) & buf, sizeof (buf), 0);} return 0 ;}

Acceptor:

# Include <stdio. h> # include <sys/types. h> # include <sys/ipc. h> # include <sys/msg. h> struct msgbuf {long mtype; char mtext [50] ;}; int main () {int key, msg_id; static struct msgbuf buf; key = ftok (". ", 1); msg_id = msgget (key, IPC_CREAT | 0600);/* set the Message Type */buf. mtype = 1; while (1) {/* receive messages from the Message Queue */msgrcv (msg_id, (void *) & buf, sizeof (buf), buf. mtype, 0); printf ("Recv msg: % s \ n", buf. mtext);} return 0 ;}

Running result:

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.