Linux Application Development (10): Message Queue

Source: Internet
Author: User

I. chicken ribs

1. Signal communication: limited amount of information that can be transferred. For pipeline communication, only non-formatted byte streams can be transmitted. This will undoubtedly cause inconvenience to application development.

2. Message Queue (also called Message Queue), customer service has these shortcomings.

A message queue is a linked list of messages. A message can be viewed as a record with a specific format.

The write process can add messages to the queue according to one rule. The read process can read messages from the message queue. Note: like the pipeline, after a read process reads a message from the message queue, the message is deleted from the message queue.

3. Message Queue classification:

(1) POSIX (Portable Operating System Interface) Message Queue

(2) The System V (System V, this V read five) message queue is currently used in many

4. Features of message queues in System V

(1) Continuity: The System V message queue is continuous with the kernel. The message queue will be deleted only when the kernel is restarted or manually deleted.

(2) key value: Because the kernel of the message queue is persistent, each message queue must correspond to a unique key value within the system range. Therefore, when obtaining a Message Queue descriptor, the key value of the Message Queue must be provided.

Ii. Message Queue Communication

1. Key value:

(1) introduce the header file:

# Include <sys/types. h>, # include <sys/IPC. h>


(2) key_t ftok (char * pathname, char proj); file to key

How to use:

I. Return the key value corresponding to the file name

= Pathname: File Name

= Proj: Project name, not 0

2. Create/open (the same function is used to create and open the msgget function), Message Queue


(1) int msgget (key_t key, int msgflg); message get, get the message queue Descriptor

Parameters:

= Key: key value, obtained by ftok

= Msgflg: flag, which can be

-- Ipc_creat: If the queue corresponding to the key value does not exist, use this ID to create a new message queue.

-- Ipc_excl, used together with ipc_creat, indicates that if the message queue to be created already exists, an error is returned.

-- Ipc_nowait: When the read/write message queue requirements cannot be met, it is not blocked.

Return Value:

Message Queue description corresponding to the key value Key

(2) The msgget function creates a message queue in the following two cases:

I. If there is no message queue corresponding to the key-value key, and msgflg contains the ipc_creat flag

II. The key parameter is ipc_private.

3. Send a message: int msgsnd (INT msqid, struct msgbuf * msgp, int msgsz, int msgflg );

How to use:

(1) parameters:


= Msqid: ID of the opened message queue

= Msgp, message sending struct type

= Msgsz, message Data Length

= Msgflg, send flag, and 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.

(2) Message format. Note that the Message Type struct not necessarily defined by you is also called msgbuf. Here is an example. In actual application, you can specify the message name; in addition, the following two members of the message struct type are required:

Struct msgbuf {

Long mtype; // indicates the message type, which can be specified by yourself, as long as the sending type is the same as the accepted type. In addition, mtype can be used to indicate the name of a member of the Message Type. The system only requires that the first member in the message struct must be an integer variable.

Char data [X]; // The first address of the message data. The data name is arbitrary. How to combine the EG message is composed of two Arrays: data1 [150] And data2 [100]

}

(3) Description:

I. A Message Queue can store multiple types of messages, as long as you pay attention to the same message type when sending and receiving messages.

II,

4. Receive message: int msgrcv (INT msqid, struct msgbuf * msgp, int msgsz, long msgtyp, int msgflg );

How to Use: read a message of the msgtyp type from the Message Queue represented by msqid. The message is stored in the msgbuf structure executed by msgp. After a message is successfully read, the message in the queue will be deleted.

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.