Linux process Communication (ii) IPC Message Queuing

Source: Internet
Author: User
Tags message queue

first, what is Message QueuingMessage Queuing provides a way to send a block of data from one process to another. Each block of data is considered to contain a type, and the receiving process can independently receive data structures that contain different types. We can avoid the synchronization and blocking problems of named pipes by sending messages. But Message Queuing, like named Pipes, has a maximum length limit for each block of data. Linux uses macro Msgmax and MSGMNB to limit the maximum length of a message and the maximum length of a queue Msgmni to limit the total number of message queues.

ii. IPC Object data StructureThe kernel maintains a data structure for each IPC object (/USR/INCLUDE/LINUX/IPC.H) third, message queue structure The kernel maintains a data structure for each message queue (/usr/include/linux/msg.h)

iv. using Message Queuing in LinuxLinux provides a series of function interfaces for Message Queuing that allow us to easily use it for interprocess communication. Its usage is similar to the other two system V pic mechanisms, i.e. semaphores and shared memory.
1. Msgget functionThis function is used to create and access a message queue. Its prototype is:


Key: Can be thought of as a port number, or it can be generated by the function Ftok.


Ipc_creat If the IPC does not exist, create an IPC resource, or open the operation.

IPC_EXCL: The new shared memory is established only if the shared memory does not exist, or an error occurs.

The Ipc_creat,xxxget () function either returns an operator for the shared memory that already exists, or returns an identifier for the newly created shared memory.

If the ipc_creat and IPC_EXCL flags are used together, Xxxget () returns a new IPC identifier if the IPC resource already exists or returns-1.

The ipc_exel flag itself does not make much sense, but working with the IPC_CREAT flag can be used to guarantee that the resulting object is new, rather than opening an existing object.  2. msgsnd functionThis function is used to add messages to the message queue. Its prototype is:
MsgId is the message queue identifier returned by the Msgget function. Msg_ptr is a pointer to prepare to send a message, but the data structure of the message has certain requirements, the pointer msg_ptr the message structure must be a long integer member variable start structure, the receive function will use this member to determine the type of message. So the message structure should be defined like this: [CPP] view plain copy print?
  1. struct  my_message{  
  2.     long   int  message_type;  
  3.     /* the data you wish to transfer*/   
  4. };
MSG_SZ is the length of the message that the MSG_PTR points to, note the length of the message, not the length of the entire struct, that is, the length of the member variable that MSG_SZ does not include the long integer message type. The MSGFLG is used to control what will happen when the current message queue is full or the queue message reaches the system-wide limit. If the call succeeds, a single copy of the message data is placed in the message queue and returns 0, returning -1 on failure.
3. Msgrcv functionThis function is used to get the message from a message queue, and its prototype is MsgId, Msg_ptr, Msg_st functions as function msgsnd functions.
The Msgtype can implement a simple receive priority. If Msgtype is 0, it gets the first message in the queue. If its value is greater than 0, it gets the first information with the same message type. If it is less than 0, it gets the first message with a type equal to or less than the absolute value of Msgtype.

The MSGFLG is used to control what happens when there are no corresponding types of messages in the queue that can be received. when the call succeeds, the function returns the number of bytes placed in the receive buffer, the message is copied to the user-allocated buffer that is pointed to by msg_ptr, and the corresponding message in the message queue is deleted. Returns -1 upon failure.
4. Msgctl functionThis function is used to control Message Queuing, which is similar to the SHMCTL function of shared memory, and its prototype is:
command is the action that will be taken, it can take 3 values,Ipc_stat: Sets the data in the MSGID_DS structure to the current associated value of the message queue, which overwrites the value of Msgid_ds with the current association value of the message queue. Ipc_set: If the process has sufficient permissions, set the current association value for the message queue to the value given in the MSGID_DS structure
ipc_rmid: Deleting Message Queuing
BUF is a pointer to the MSGID_DS structure that points to the structure of the Message Queuing pattern and access rights. The MSGID_DS structure includes at least the following members: [CPP] view plain copy print?
  1. struct  MSGID_DS  
  2. {
  3. uid_t Shm_perm.uid;
  4. uid_t Shm_perm.gid;
  5. mode_t Shm_perm.mode;
  6. };
returns 0 on success and -1 on failure.
v. Using Message Queuing for interprocess communication ( real-life examples are primarily about simulating client-server communication with each other) 1, first to complete the Message queue correlation function (comm.h comm.c)


2. Writing server code (SERVER.C)
3. Writing client code (CLIENT.C)


vi. Comparison of message queues with Named PipesFeatures:1. Data block oriented2, the life cycle does not follow the process, the creation of IPC requires user code display deletion, the system will not be automatically deleted, with the kernel (following the operating system until the system restarts)3, two-way data communication created by the system IPC
compared to Named Pipes, the advantage of Message Queuing is that1. Message Queuing can also exist independently of the send and receive processes, eliminating the difficulties that can arise when synchronizing the opening and closing of named pipes. 2. By sending messages, you can also avoid the synchronization and blocking problems of named pipes, and do not need to provide synchronization methods by the process itself. 3. The receiving program can selectively receive data through the message type, instead of being received only by default, as in a named pipe.

Linux process Communication (ii) IPC Message Queuing

Related Article

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: 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.