Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The use of message queuing in Linux

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "the use of message queues in Linux". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the use of message queues in Linux".

1 the concept and use process of message queue

1) the concept of message queue

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

Processes can add (write) messages to the queue according to specific rules; other processes can read messages from the message queue.

2) Application scenarios of message queuing

Message queuing itself is the content of IPC communication, so it is mainly used for interprocess communication.

Messages are read and written, so the messages sent can be used for notification signals of actions, or they can receive data and do other processing.

2 structures and functions related to message queues

0) structures related to message queues

Each queue has a msqid_ds structure associated with it, as follows.

Struct msqid_ds {access to the message queue and other information time_t msg_stime; / / the last time the queue accepted the message time_t msg_rtime; / / the last time the message was fetched from the queue time_t msg_ctime / / when the queue was last changed unsigned long _ _ msg_cbytes; / / the number of bytes of memory occupied by messages in the queue msgqnum_t msg_qnum; / / the current number of messages in the queue msglen_t msg_qbytes; / / the maximum number of bytes of memory occupied by the queue pid_t msg_lspid / / pid msgsnd pid_t msg_lrpid; of the process that last sent the message to the queue / / pid of the process that last fetched the message from the queue; struct ipc_perm {key_t key; ushort uid; / / user id, valid user ID and valid group id (euid and egid) ushort gid; ushort cuid / / the creator's euid and egid ushort cgid; ushort mode; / / access modes see pattern flag ushort seq; / / IPC object usage frequency information}

1) msgget function

The msgget function is used to create or open a message queue.

① function prototype.

Int msgget (key_t key,int msgflg)

② header file.

Include

③ parameter.

Key: key value.

Msgflg: open the flag. IPC_CREAT: indicates a newly created message queue.

④ returns a value.

Success: returns the id of the message queue.

Failed:-1.

2) msgsnd function

The msgsnd function is used to send messages, that is, to write messages to the message queue.

① function prototype.

Int msgsnd (int msgid,const void * msgp,size_t msgsz,int msgflg)

② header file.

Include

③ parameter.

Msgid: the id of the message queue.

Msgp: points to the message to be sent.

Msgsz: length of the message.

Msgflg: flag bit.

④ returns a value.

Success: 0.

Failed:-1.

3) msgrcv function

The msgrcv function is used to read message queues, that is, to receive messages from message queues.

① function prototype.

Int msgsnd (int msqid, const void * msgp, size_t msgsz, intmsgflg) ssize_t msgrcv (int msqid, void * msgp, size_t msgsz, long msgtyp,intmsgflg)

② header file.

# include # include # include

③ parameter.

Msqid: the id of the message queue.

Msgp: stores messages.

Msgsz: the maximum length of the message you want to get.

Msgtyp: the type of message, which is divided into the following three situations:

When msgtyp = 0: ignore the type, fetch the first message in the queue directly.

When msgtyp > 0: fetch the first message in the message queue whose type is equal to msgtyp.

When msgtyp < 0: take a message whose type is less than or equal to the absolute value of msgtyp, if there are multiple messages

To meet this condition, take the one with the smallest type.

④ returns a value.

Success: the data length of the message actually received.

Failed:-1.

4) msgctl function

The msgctl function is used to manipulate message queues, such as deleting message queues, and so on.

① function prototype.

Int msgctl (int msqid,int cmd,struct msqid_ds * buf)

② header file.

# include # include # include

③ parameter.

Msqid: the id of the message queue.

Cmd: the operation command for the message queue, which specifies the command to be executed on the queue specified by msqid.

IPC_STAT: take the msqidds structure of this queue and store it in the structure pointed to by buf.

IPCSET: copy the fields msg_perm.uid, msg_perm.gid, msg_perm.mode, and msg_qbytes from the structure pointed to by buf into the msqid_ds structure associated with this queue.

This command can only be executed by the following two processes:

One is that its effective user ID is equal to msg_perm.cuid or msg perm.uid.

The other is a process with superuser privileges. Only superusers can increase the value of msg_qbytes.

IPC_RMID: removes the message queue and all data still in the queue from the system. This deletion takes effect immediately.

Other processes that are still using this message queue will get an EIDRM error the next time they try to operate on this queue.

This command can only be executed by the following two processes:

One is that its effective user ID is equal to msg_perm.cuid or msg_perm.uid.

The other is a process with superuser privileges. These three commands (IPC_STAT, IPC_SET, and IPC_RMID) can also be used for semaphores and shared storage.

Buf: get the msqid_ds structure in the kernel, usually not.

④ returns a value.

Success: 0.

Failed:-1.

3 instance code

Here are two processes to demonstrate the use of message queues.

The example code is as follows, and the instructions are all in the code comments, picture.

SendQueue.c .

# include # include / / message structure struct msg {long msgtype; / / message type char msgtext [1024]; / / message length}; void main (int argc, char * argv []) {int msgid; char str [256]; struct msg msgst; key_t key = ftok ("/ tmp", 600) / / create message queue msgid = msgget (key,0666 | IPC_CREAT); / / Keyboard input message while (1) {/ / get message data printf ("\ nPlease enter a message to send,input 'end' to quit!\ n\ n"); scanf ("% s", str) Strcpy (msgst.msgtext,str); if (strncmp (str, "end", 3) = = 0) {printf ("\ n"); break;} / / send message msgsnd (msgid,&msgst,sizeof (struct msg), 0) } / / output message queue msgctl (msgid,IPC_RMID,0);}

ReceiveQueue.c .

# include # include / / message structure struct msg {long msgtype; char msgtext [1024];}; int main (int argc, char * argv []) {int RunFlag = 1; / / Loop flag int msgid =-1; / / message id long msgtp = 0 / / message type struct msg msgst; / / message structure variable key_t key = ftok ("/ tmp", 600); / / create a key value msgid = msgget (key, 0666 | IPC_CREAT) / / establish message queuing if (msgid = =-1) {exit (1) / / abnormal exit} while (RunFlag) / / get a message from the queue until an end message is encountered {if (msgrcv (msgid,&msgst,sizeof (struct msg), msgtp, 0) =-1) {exit (1) / / abnormal exit} printf ("\ nThe message received is:% s\ n\ n", msgst.msgtext); if (strncmp (msgst.msgtext, "end", 3) = = 0) / / end of end {RunFlag = 0 / / set exit loop flag} if (msgctl (msgid, IPC_RMID, 0) = =-1) / / Delete message queue {exit (1); / / abnormal exit} exit (0) / / exit normally}

Compile the program, first run the receiving program, and then run the sending program, enter the message to be sent, and exit and enter end.

The running results of the two ① terminals are as follows:

The running result of a single terminal of ② is as follows:

Thank you for your reading. the above is the content of "the use of message queues in Linux". After the study of this article, I believe you have a deeper understanding of the use of message queues in Linux, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report