The message queue

Source: Internet
Author: User

To tidy up the message queue tonight, Message Queuing is a linked list of messages that exist inside the kernel, accessed by a message-to-reference identifier, and each message queue has a MSQID_DS structure that holds the current state parameters of the message queue, which is defined as follows:

1 Steuct msqid_ds 2 {3 struct ipc_perm msg_perm; 4 struct msg *msg_first; 5 struct msg *msg_last; 6 ulong msg_ctypes; 7 u Long Msg_qnum; 8 ULONG Msg_qbytes; 9 pid_t msg_lspid;10 pid_t msg_lrpid;11 time_t msg_stime;12 time_t msg_rtime;13 time_t msg_ctime;14}

This structure has definitions for each domain, and here's what the message queue is about:

1, create or open a message queue operation

By invoking system functions, you can create or open a message queue, send a message to a message queue, and get a message from a message queue, starting with the function that opens the message queue

#include <sys/types.h0> #include <sys/ipc.h> #include <sys/msq.h>int message (key_t key,int flag);

This function can either create a new message queue or open a message queue that already exists, depending on the value of key and flag, which returns the reference identifier of the message queue if the function executes successfully, otherwise returns 1, and when a new message queue is created, the corresponding msqid_ The DS structure will also be initialized, which is not specified here, as illustrated in the following example.

Example: The following program creates or opens a message queue with the permission-RW-RW---according to the relevant user input keywords, the code is as follows:

msgget.c

2. Send and receive messages

Message Queuing has both the sending and receiving of messages, and the process enables interprocess communication between the two operations.

The function for sending a message is described below:

#include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <int msqid,const void * ptr,size_t Nbytes,int flag>;//msqid The reference identifier of the message queue. The newly sent message is inserted at the end of the message queue//ptr A pointer to a long positive integer that immediately follows the length of the data//nbytes message that is passed in the message queue and is not included in the long positive integer, in bytes//flag can take 0 or ipc_nowait

The function returned a success of 0, otherwise-1.

The function that accepts the message is described as follows:

#include <sys/yypes.h> #include <sys/ipc.h> #include <sys/msg.h> #include (int msqid,void *ptr,size_t Nbytes,long type,int flag);//msqid The reference identifier of the message queue.


The type parameter is used to specify the message to be accepted in the queue.
Two bits in Flagflag are related to receiving messages

Now you can write a program to communicate with Message Queuing, write two programs, one to receive a send, the code is as follows:

Receive MSGRCVSend msgsnd

The results of the two programs running are as follows:

$./send Magsndenter Some text:helloenter some text:worldenter some text:end$./receive msgrcvyou wrote:helloyou Wrote:worldYou Wrote:end

From this example, it can be seen that the process of communication can be completely unrelated to the two processes, do not need to contract the synchronization of the method.

3, controlling the message process

The MSGCTL function is used to control Message Queuing, which is prototyped as follows:

#include <sys/msg.h> #include (int msqid,int cmd,struct msqid_ds *buf);

The MSGCTL function performs a control operation on the message queue specified by the Msqidl function that requires a parameter cmd, and the parameter msqid is a positive integer that must be returned by the msgget of the message queue ID

Here is an example to summarize the message queue, the code is as follows:

msg.c

The message queue

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.