Introduction to Message Queuing
The implementation and function of each communication mode is different, and the scene is not the same, the message queue is a linked list, it provides a
The struct Msqid_ds *msgque[msgmni] Vector maintains a list of message queues because the maximum number of messages supported by the Linux system is determined by the Msgque array, and each msgid_ds represents a message queue;
1. Structure of a message queue:
The data members in struct Msqid_ds are described below:
{
struct Ipc_perm msg_perm;
struct MSG *msg_first; /* Message Queue header pointer */
struct MSG *msg_last; /* Message Queue tail pointer */
__kernel_time_t Msg_stime; /* The last time the message queue message was inserted */
__kernel_time_t Msg_rtime; /* The last time a message is received deletes the message in the queue */
__kernel_time_t Msg_ctime;
struct Wait_queue *wwait; /* Send message waiting for process queue */
struct Wait_queue *rwait;
unsigned short msg_cbytes;
unsigned short msg_qnum; /* Number of messages in Message Queuing */
unsigned short msg_qbytes;
__kernel_ipc_pid_t Msg_lspid; /* pid*/of the last message sending process
__kernel_ipc_pid_t Msg_lrpid; /* pid*/of the last message sending process
};
2. Use of Message Queuing
(1) Acquisition of message queue key
To use Message Queuing in a program, you must be able to get to Message Queuing key, because the process cannot directly access the kernel message queue structure, so you need a Message Queuing flag to let the process know which message queue is currently operating, and also to guarantee the key value uniqueness of the message queue.
key_t Kkey=ftok (".", "a");
The function maps a message queue key with a pathname
(2) Gets or opens a message queue
Qid=msgget (key_t key,int msgflag);
Key: Message Queue key
-msgflag:
Ipc_private: Creates a message queue that is exclusive to the process, and other processes cannot access the message queue
Ipc_creat: Creates a new message queue if the message queue does not exist, and returns the message queue if the message queue exists
Ipc_creat| IPC_EXCL:: Ensure only new queues are created and return errors if Message Queuing is present
Ipc_noeait: Small queues are not blocked for blocking
function Reason:
1) If key==ipc_private request a piece of memory, create a new message queue
2) in the Msgque vector table to find the key value of the message queue, if there are no results found in two cases:
Msgflag indicates that a new message queue is not created and returns an error
Msgflag means to create a new message queue (ipc_creat) to create a new message queue
3. Sending a message queue
int msgsnd (int msqid,struct msgbuf* msgp,size_t msgsz,int msgflg)
--msqid: QID for Message Queuing
--MSGP: pointer to corresponding message content struct body
--MSGSZ: The size of the message is the size of the message structure that the pointer points to
--MSFLG: Message Flags
0: Ignore the flag bit to send messages to Message Queuing in a blocked manner
4, receive a message from message queue to msgbuf*
int MSGRCV (int msqid,struct msgbuf* msgp,size) T Msgsz,long msgtyp,int MSGFLG)
--msgqid: qid of Message Queuing
--MSGP: is the buffer to which the message queue will be received
--MSGSZ: The size of the message
--msgtyp: The type of message expected to be received
--MSGFLG: Logo
5. Control of Message Queuing
int msgctl (int msqid,int cmd,struct msqid_ds* buf)
--msqid: QID for Message Queuing
--cmd: The action to be performed on the message queue for the function
Ipc_atat: Takes out the msqid_ds result body of the message queue and stores the parameters in the Msqid_ds structure object pointed to by the BUF
Ipc_set: Sets the Msg_perm member in the MSQID_DS data of the message queue, given by the MSQID_DS structure that the buf points to.
Settings for Message Queuing and operations are determined by CMD
IPC---Message Queuing