Concept
Message Queuing provides a way to send a block of data from one process to another, each of which is considered to have a type , and the data blocks that the recipient process accepts can have different type values.
We can avoid the synchronization and blocking problems of named pipes by sending messages.
Message Queuing differs from a pipeline in that Message Queuing is message -based, and pipelines are byte-stream based, and Message Queuing reads are not necessarily first-in-first-out.
Message Queuing has the same disadvantage as named pipes, where the maximum length of each message is capped (Msgmax), the total number of bytes per message queue is capped (MSGMNB), and the total number of message queues on the system has an upper limit (Msgmni).
IPC Object Data structure
The kernel maintains a data structure (/USR/INCLUDE/LINUX/IPC.H) for each IPC object.
Message Queuing, semaphores, shared memory all have such a common data structure.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/87/0C/wKioL1fSR3zRPNviAAAorjPADBo763.png "title=" struct _ipc_perm.png "alt=" Wkiol1fsr3zrpnviaaaorjpadbo763.png "/>
Message Queue structure
650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M02/87/0E/wKiom1fSSEnTbghQAAEqNTcPpCg480.png "title=" struct _msqid_ds.png "alt=" Wkiom1fssentbghqaaeqntcppcg480.png "/>
The first entry is the IPC struct, followed by the private members of the message queue.
Message Queuing is implemented with a linked list.
Function
Msgget The system call creates a message queue, or gets an existing message queue. Defined as follows:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/87/0E/wKiom1fSSVqBX-DSAAA1I7U8HOo038.png "title=" Msgget.png "alt=" Wkiom1fssvqbx-dsaaa1i7u8hoo038.png "/>
Key : Can be thought of as a port number, or it can be generated by the function Ftok.
MSGFLG :ipc_creat(creates an IPC resource, otherwise opens the operation).
ipc_excl(The new IPC resource is established only if the IPC resource does not exist, otherwise it generates an error). The IPC_EXCL flag itself does not make much sense, but working with ipc_creat can be used to guarantee that the resulting object is new, not an open object.
If ipc_creatis used alone,msgget () either returns an operator for the message queue that already exists, or returns a new descriptor for the message queue.
If the ipc_creat and ipc_excl flags are used together,msgget () returns a new IPC identifier, or 1 if the IPC resource already exists.
The msgsnd system call adds a message to the message queue. Defined as follows:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/87/0F/wKiom1fSWA6Qae1dAAA-fBmSX6c397.png "title=" Msgsnd.png "alt=" Wkiom1fswa6qae1daaa-fbmsx6c397.png "/>
The msqid parameter is the message queue identifier returned by the msgget call.
The msgp parameter points to a message that is ready to be sent, and the message must be defined as the following type
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/87/0C/wKioL1fSTQWzF31gAAA8tJQa-Zs273.png "title=" Msgp.png "alt=" Wkiol1fstqwzf31gaaa8tjqa-zs273.png "/>
Msgsz The parameter is the length of the data portion (mtext) of the message.
MSGFLG : Controls the behavior of msgsnd , usually only supports the ipc_nowait flag, which is a non-blocking way to send messages. By default (MSGFLG is 0), msgsnd will block if the message queue is full when the message is sent. If the ipc_nowait flag is specified, then msgsnd will immediately return-1 and set errno to Eagain.
msgsnd returns 0 on success, returns 1 on failure, and sets errno.
MSGRCV The system call gets the message from the message queue. Defined as follows:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/87/0F/wKiom1fSWErjUIUVAAAffqAvWQ4656.png "title=" Msgrcv.png "alt=" Wkiom1fswerjuiuvaaaffqavwq4656.png "/>
msqid The parameter is the message queue identifier returned by Msgget .
MSGP parameters are used to store received information.
Msgsz The parameter refers to the length of the part of the message data.
Msgtyp The parameter specifies what type of message to receive, or 0, which indicates that all messages in the message queue will be read.
MSGFLG The parameters control the behavior of the MSGRCV, if 0, when there is no message in the message queue, a blocking wait processing mode is taken. ipc_nowait If there is no message in the message queue, MSGRCV immediately returns and sets Errno to Enomsg.
MSGRCV returns 0 on success, returns 1 on failure, and sets errno.
Msgctl a system call controls a property of a message queue. Defined as follows:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/87/0F/wKiom1fSXI_jy9wtAAA6rGTHg4A094.png "title=" Msgctl.png "alt=" Wkiom1fsxi_jy9wtaaa6rgthg4a094.png "/>
msqid The parameter is the shared memory identifier returned by the msgget call.
cmd parameter specifies the command to execute
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/87/0F/wKiom1fSXgagFNsbAALtnRF8Iu8084.png "title=" Msgctl _cmd.png "alt=" Wkiom1fsxgagfnsbaaltnrf8iu8084.png "/>
Msgctl The return value on success depends on the cmd parameter, which returns 1 on failure and sets the errno.
Code
This article is from the "zgw285763054" blog, make sure to keep this source http://zgw285763054.blog.51cto.com/11591804/1851118
Inter-process communication-Message Queuing