[Switch] IPC-Message Queue

Source: Internet
Author: User

I. Concepts

A message queue is a linked list of messages. A process with write permission on a message queue can add new messages to it according to certain rules. A process with read permission on the Message Queue can read messages from the message queue. Message Queues continue with the kernel. The following three concepts are introduced:

1. Continuous with process: IPC always exists until the last process that opens the IPC object closes the object, such as an MPS queue and a named MPs queue.

2; as the kernel continues: IPC continues until the kernel reboots or displays the deleted object. Such as message queue, semaphore, and shared memory

3; as the file system continues: IPC keeps displaying and deleting this object

System V message queue is widely used.

Ii. Message Queue Information

 

The structure msqid_ds is used to set or return message queue information, which exists in the user space;

Struct msqid_ds
{
Struct ipc_perm msg_perm;/* structure describing operation permission */
_ Time_t msg_stime;/* time of last msgsnd command */
_ Time_t msg_rtime;/* time of last msgrcv command */
_ Time_t msg_ctime;/* time of last change */
Unsigned long int _ msg_cbytes;/* current number of bytes on queue */
Msgqnum_t msg_qnum;/* Number of messages currently on queue */
Msglen_t msg_qbytes;/* max number of bytes allowed on queue */
_ Pid_t msg_lspid;/* PID of last msgsnd ()*/
_ Pid_t msg_lrpid;/* PID of last msgrcv ()*/
Unsigned long int _ unused4;
Unsigned long int _ unused5;
};

3. Open and create a message queue

The kernel persistence of Message Queue requires that each Message Queue corresponds to a unique key value within the system range. Therefore, you must obtain a Message Queue description, you only need to provide the key value of the message queue. Msgget is used to create a message queue or open an existing queue.

Int msgget (key_t key, int msgflag );

Create a new message queue or open an existing message queue.

If the message queue is successful, it is the message queue description. If an error occurs, it is-1.

Key: it is a key value obtained by ftok or a constant directly;

Msgflg: indicates the signs or results of ipc_creat, ipc_excl (), and ipc_nowait.

For example, msgflg = ipc_creat | ipc_excl | 0666 indicates that a queue with no key value is created; otherwise, a queue is opened. 0666 indicates that the queue has the same permissions as a common file. The three octal digits represent the read and write permissions of users in the same group and other users.

In either of the following cases, this call creates a new message queue:

1. If no message queue corresponds to the key, and msgflg contains the ipc_creat flag;

2. The key parameter is ipc_private;

Note that when creating a new queue, the system automatically initializes the following members of the struct msqid_ds structure.

The ipc_perm structure is initialized as we mentioned previously. In this structure, the mode member is set according to the corresponding permission bit in the flag.

Msg_qnum, msg_lspid, msg_lrpid, msg_stime, and msg_rtime are all set to 0.

Msg_ctime is set to the current time. Msg_qbytes is set as the system limit value.

4. Obtain and modify Message Queue attributes and delete message queues

  Int msgctl (INT msqid, int cmd, struct msqid_ds * BUF );

Perform multiple operations on message queues

If 0 is returned successfully,-1 is returned if an error occurs.

Msqid: Message Queue ID,

CMD: The operation to be executed is equivalent to the semaphore and shared storage operation.

Buf: the struct msqid_ds structure of this queue, a temporary variable of the msqid_ds struct type. Used to store read message queue attributes or message queue attributes to be modified

The system calls the CMD operation on the message queue identified by msqid. There are three kinds of CMD operations: ipc_stat (get queue status), ipc_set (set queue attributes), and ipc_rmid (delete Message Queue ).

Ipc_stat: this command is used to obtain Message Queue information. The returned information is stored in the msqid_da structure pointed to by the Buf;

Ipc_set: this command is used to set the attributes of a message queue. The attributes to be set are stored in the msqid_ds structure pointed to by the Buf. The attributes include msg_perm.uid, msg_perm.gid, msg_perm.mode, and msg_qbytes, it also affects msg_ctime members.

Ipc_rmid: Delete the Message Queue identified by msqid_ds.

 

5. send and receive messages in a message queue

Int msgrcv (INT msqid, struct msgbuf * msgp, size_t msgsz, long msgtyp, int msgflg );

Read a message of the msgtyp type from msqid of the message queue, and store the message in the msgbuf structure pointed by msgp. (After successful reading, the message in the queue will be deleted)

0 is returned for success,-1 is returned for failure.

Msqid: Message Queue ID

Msgp: the cache structure of the received message queue,

Msgsz: Message Data Length

Msgtype: Message Type, = 0 read the first data in the queue

Msgflg: Read flag,

Ipc_nowait (if a message that does not meet the conditions is received, return immediately. The error code is enomsg ),

Ipc_0000t (used with msgtype> 0), returns the first message in the queue that is not of the msgtype type,

Msg_noerror: truncates long data.

Appendix:

Msgrcv ()Three conditions for Removing Blocking:

1. The message queue contains messages that meet the conditions (or are used );

2. The message queue represented by msqid is deleted;

3. The process that calls msgrcv () is interrupted by the signal;

 

Int msgsnd (INT msqid, struct msgbuf * msgp, int msgsz, int msgflg );

Send a message to the message queue. The message to be sent is stored in the msgbuf structure pointed to by msgp. The message size is specified by msgze.

0 is returned. Otherwise,-1 is returned.

Msqid: Message Queue ID

Msgp: pointer to message data

Msgsz: size of the sent message

Msgflag: Flag

Appendix: msgp format:

 

Struct msgbuf {

 

Long mtype;/* message type must be> 0 */

 

Char mtext [1];/* message data this is just the first address of an array, not only one character */

 

};

We can regard the msgbuf structure as a template. programmers can design a direct message structure based on their own needs. For example, if an application needs to exchange messages consisting of an integer followed by an 8-byte array,

Then it can define its own structure as follows:

Typedef struct my_msgbuf {

Long mtypel

Int mshort;

Char mchar [my_data];

} Message;

For sending a message, the meaningful msgflg flag is ipc_nowait, indicating whether msgsnd is waiting when the message queue does not have enough space to accommodate the message to be sent. 0 indicates waiting.

There are two conditions that cause msgsnd () wait:

1. The sum of the current message size and the number of bytes in the current Message Queue exceeds the total capacity of the message queue;

2. the number of messages (in bytes) in the current message queue is no less than the total capacity (in bytes) of the Message Queue. At this time, although the number of messages in the message queue is large, however, there is only one byte.

There are three conditions for msgsnd () to remove blocking:

1. The above two conditions are not met, that is, there is space in the message queue to accommodate the message;

2. The message queue represented by msqid is deleted;

3. The process that calls msgsnd () is interrupted by the signal;

When msgsnd is returned successfully, the msqid_ds structure related to the message queue is updated to indicate the ID of the called process (msg_lspid) and the time (msg_stime) for the call ), and indicates that a message is added to the queue.

Vi. Use of 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.