"Application Programming - inter-process communication - Message Queuing Message Queuing"

1. Message Queuing

  Message Queuing: there is provided a method of transmitting a block of data from one process to another. Compared with the FIFO, the message queue has the advantage that it is independent of the transmission and receiving process exists . 

  1. The list structure organization, stored in the kernel.

  2. referenced by the queue identifier.

  3. The data indexed by a specified data type.

 

2.msgget function

#include <SYS / msg.h> int the msgget (key_t key, int the msgflg); 
first parameter key: each IPC object key corresponding to a 
second parameter msgflg: behavior function ( 0666 | the IPC_CREAT represents user has read write permission) 
return value: success: non-negative queue ID; failure: - 1 ;

 

  Role: to create and open a message queue.

  It may be (only view the status of the message queue) by ipcs -q: Check the state of the system IPC

 

3.msgsnd function

#include <SYS / msg.h> int a msgsnd ( int msqid, const void * msg_ptr, size_t msg_sz, int the msgflg); 
first parameter msqid is returned by the function msgget message queue identifier; 
The second parameter is a msg_ptr pointer pointing to send message, the message must be started as just said, as a member variable to a long integer; 
third parameter is the length msg_sz msg_ptr directed message. This length does not include the length of the long message type member variable; 
fourth parameter controls what msgfig current message queue full message or queue limit is reached when the system-wide to occur, if the msgflg IPC_NOWAIT flag is set, the function will immediately return, do not send 
sent message and the return value - . 1 . If IPC_NOWAIT msgflg flag is cleared, the send process will hang waiting queue free space. 
Returns: Success: 0 ; fails when: - 1 ;

 

  Action: add messages to the message queue.

  Constrained structure of the message in two ways. First, it must be less than the upper limit of the system; secondly, it must start with a member variable long integer. Receiving function will use this member variable to determine the type of message. When a message, preferably the message structure is defined as follows:

  struct my_message{

    long int message_type;

  }

  Since receiving the message to use message_type, so you can not ignore it. You must include it when declaring your own data structure, and preferably it is initialized to a known value.

 

4.msgrcv function

#include <SYS / msg.h> int msgrcv ( int msqid, void * msg_ptr, size_t msg_sz, Long int MsgType, int the msgflg); 
first parameter msqid is returned by the function of the message queue identifier msgget; 
second parameter msg_ptr is ready to receive a message pointer pointing, as messages must begin with a member variable long integer msgsnd function as described in the foregoing. 
The third parameter is the length msg_sz msg_ptr directed messages, it does not include the length of the long message type member variable. It does not include the length of the long message type member variable. 
Msgtype fourth parameter is a long integer, it can achieve a simple form of the received priority. Msgtype If the value is 0, the first acquired queue message is available. 
If its value is greater than zero, to obtain a first message with the same message type. If its value is less than zero, the message takes the absolute value of the first message type is equal to or less than msgtype. 
This function looks like a very complicated, but the practical application is very simple. If you want to send a message in order to receive them, put msgtype set to zero. If you want to acquire a particular type of message, put msgtype set 
to the corresponding value type. If you would like to receive less than or equal to n type message, to put msgtype -

  n.
Msgflg fifth parameter for controlling what happens when there is no corresponding type of queue may receive a message when that will occur. If IPC_NOWAIT sign in msgflg is set, the function will return immediately, the return value is - 1 if msgflg. 
IPC_NOWAIT flag is cleared, the process will be suspended to await a corresponding type of message arrives.

  Action: retrieve messages from a message bar UI.

 

5.msgctl function

#include <SYS / msg.h> int the msgctl ( int msqid, int command, struct the msqid_ds * buf); 
The first parameter is returned by msqid msgget message queue identifier; 
The second parameter is the command action to be taken, it can take three values. 
IPC_STAT data set msqid_ds structure for the message queue is currently associated value     
IPC_SET if the process has sufficient rights, put the value of the message associated with the current value of the queue to msqid_ds structure given 
IPC_RMID remove Message Queuing 
Returns: Success: 0 ; fails when: - 1


 

Guess you like

Origin www.cnblogs.com/zhuangquan/p/12299590.html