interprocess communication (IPC) ———— Message Queuing

Source: Internet
Author: User

I. Message Queuing

One of the most basic ways of interprocess communication mentioned earlier is the pipeline, and now let's talk about another way of communicating between processes- Message Queuing . Message Queuing is a way of sending chunks of data from one process to another, each with its type, and the data blocks received by the receiver can be of different types, so that we can send messages to avoid synchronization and blocking problems with Named pipes.

Message Queuing is different from pipeline, pipeline is based on byte stream, and Message Queuing is message-based, and message Queuing is not necessarily read in first-out, Message Queuing is implemented with a linked list , but the same is that they have the same shortcomings, that is, there is a fixed size, The size of each message is capped (Msgmax), and the size of each queue is capped (MSGMNB), and the number of queues in the system is also capped (Msgmni).

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7E/E9/wKiom1cMmV7CSqhHAAAIBv4cl1A285.png "title=" QQ picture 20160412145431.png "alt=" Wkiom1cmmv7csqhhaaaibv4cl1a285.png "/>


Two. IPC Object data structure

The kernel maintains a data structure (/USR/INCLUDE/LINUX/IPC.H) for each IPC object:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7E/E6/wKioL1cMm2bxl2OeAAAR24y1Jc4230.png "title=" QQ picture 20160412150009.png "alt=" Wkiol1cmm2bxl2oeaaar24y1jc4230.png "/>


Three. Message Queue structure

(usr/include/linux/msg.h)

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7E/E6/wKioL1cMm-ezo2vXAAAo8yHd9dc295.png "title=" QQ picture 20160412150235.png "alt=" Wkiol1cmm-ezo2vxaaao8yhd9dc295.png "/>

You can see that the first of the structure of the message queue is the IPC structure, which is owned by each IPC object, and the rest is the individual member.


Four. functions on Message Queuing

    1. Create a new message queue or get an existing message queue

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/EA/wKioL1cM39LSPqO9AAAJnePTI4Q719.png "title=" create. png "alt=" Wkiol1cm39lspqo9aaajnepti4q719.png "/>

In a function parameter, key can be considered a port number, generated using the function Ftok

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7E/ED/wKiom1cM32_zDJw5AAAJxL3UDng727.png "title=" Ftok.png "alt=" Wkiom1cm32_zdjw5aaajxl3udng727.png "/>

The function Ftok converts an already existing pathname and an integer identifier to a key_t value, where the proj_id is used only if its low eight bits.


The second MSGFLG in the Msgget function parameter is the flag of Message Queuing,ipc_creat and ipc_excl, where when ipc_creat is used alone, the existing operator is returned if the shared memory already exists. If it does not exist, it creates a new volume operator for shared memory, and when ipc_creat and ipc_excl are used together, a new one is created when the shared memory does not exist, and an error value of 1 is returned if it exists. That is, IPC_EXCL is not meant to be used alone, and it is only used with ipc_creat to ensure that the resulting object is new rather than open.


2. Send/Receive messages for Message Queuing

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7E/EA/wKioL1cM4dzwlKFaAAAOhHO1Wbc281.png "style=" float: none; "Title=" accepts send. png "alt=" Wkiol1cm4dzwlkfaaaaohho1wbc281.png "/>

In the function parameter,

Msqid represents the identification code of the message queue;

MSGP is a buffer that points to Message Queuing, which is used to temporarily store messages sent and received, which is a user-definable struct, as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7E/EA/wKioL1cM4dyTPmdyAAANcM02m3s600.png "style=" float: none; "title=" struct. png "alt=" Wkiol1cm4dytpmdyaaancm02m3s600.png "/>


Msgsz Indicates the size of the message;

Msgtype refers to the message pattern read from the message queue, that is, which party to read the data, if the value is zero, it means that all messages in the message queue will be read;

The msgflg is used to indicate what action should be taken when there is no data in the queue. If the MSGFLG and constant ipc_nowait are pooled, then if the message queue is full at msgsnd () execution, then MAGSND () will not block, and will immediately return-1, if the MSGRCV () is performed, and if the message queue is empty, do not wait but return-1, and set the error code to enomsg, when the MSGFLG is 0 o'clock, whether msgsnd () execution queue is full or MSGRCV () execution when the queue is empty, all take the blocking wait processing mode.


3. Message Queuing properties

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7E/ED/wKiom1cM5TvAwgt8AAAKr6_tZ-U434.png "title=" Ctl.png "alt=" wkiom1cm5tvawgt8aaakr6_tz-u434.png "/> Function parameter also msqid represents the identifier of the message queue,

cmd represents the following three kinds of CMD operations on Message Queuing for the Msqid identity:

Ipc_stat, which is used to obtain the MSQID_DS data structure corresponding to the message queue and save it to the BUF specified address space;

ipc_set, the command is used to set the properties of the message queue, and the properties to be set are stored in BUF;

Ipc_rmidthat the command removes the message queue from the kernel for the msqid identity.


Here's a chestnut. Use the above function:

First of all, of course, to create two processes, such as one end msg_client and the other end of Msg_server, will be used to encapsulate the function in the same header file comm.h, while in COMM.C implementation:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7E/ED/wKiom1cM6Gzzztj4AAEytpxkXgQ143.png "title=" QQ picture 20160412203137.png "alt=" Wkiom1cm6gzzztj4aaeytpxkxgq143.png "/> In order to distinguish whether Message Queuing is newly created or returned, it can be divided into two functions to get the message queue. With Creat_msg (), because Ipc_creat and ipc_excl are used together, you can ensure that the returned message queue is newly created, and that using the get_msg () function is not necessarily the case;

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/ED/wKiom1cM6q6RXG54AAF4SjvxfzY235.png "title=" QQ picture 20160412204138.png "alt=" Wkiom1cm6q6rxg54aaf4sjvxfzy235.png "/>

In two processes, the get_msg () function is used to get the message queue, one to receive the message and the other to send the message, so that the communication between the processes is completed back and forth.



Finish

This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1763161

interprocess communication (IPC) ———— 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.