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

What are the functions related to message queuing in Linux programming

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "what are the functions related to Linux programming message queue". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Message queuing introduction message queue is essentially a linked list in kernel space, and each node of the linked list is a message. Each message has its own message type, which is represented by an integer and must be greater than 0. Each type of message is maintained by the corresponding linked list:

Where the number 1 represents a message of type 1, and the numbers 2, 3, and 4 are similar. Color blocks represent message data, which are hung on a linked list of the corresponding type.

It is worth noting that we just said that there are no messages of message type 0. In fact, the linked list of message type 0 records the order in which all messages are queued, where the red arrow indicates the order in which messages are added.

2. Functions related to message queuing / / create and obtain ipc kernel objects int msgget (key_t key, int flags); / send messages to message queues int msgsnd (int msqid, const void * msgp, size_t msgsz, int msgflg); / / get message ssize_t msgrcv from message queues (int msqid, void * msgp, size_t msgsz, long msgtyp, int msgflg) / / View, set, delete ipc kernel objects (same as shmctl) int msgctl (int msqid, int cmd, struct msqid_ds * buf); 3, message data format whether you send or receive messages, the format of messages must follow the specification. In a nutshell, it usually looks like this:

Struct Msg {long type; / / message type. This is necessary, and the value must be > 0, this value is used by the system / / message body, the number of bytes depends on you / /.}; so, as long as you make sure that the first 4 bytes (long under 32-bit linux) is an integer. For example:

Struct Msg {long type; char name [20]; int age;} msg; struct Msg {long type; int start; int end;} msg; shows that it doesn't matter what data type the body part is, because the message queue passes binary data and doesn't have to be text.

4. Msgsnd function msgsnd function is used to send data to the message queue. If the function is interrupted by a signal, errno is set to EINTR.

Int msgsnd (int msqid, const void * msgp, size_t msgsz, int msgflg); parameter msqid:ipc kernel object id parameter msgp: message data address parameter msgsz: size of message body (excluding message type) parameter msgflg: optional this value is 0: if there is not enough message queue space, msgsnd will block. IPC_NOWAIT: return directly. If there is not enough space, errno will be set to EAGIN.

The return value: 0 indicates success,-1 fails and errno is set.

5. Msgrcv function msgrcv function takes the message from the message queue and deletes it from the message queue.

Ssize_t msgrcv (int msqid, void * msgp, size_t msgsz, long msgtyp, int msgflg); parameter msqid:ipc kernel object id parameter msgp: used to receive message data address parameter msgsz: size of message body (excluding message type) parameter msgtyp: specify which type of message to get

Msgtyp = 0: get the first message in the message queue msgtyp > 0: get the first message of type msgtyp, unless msgflg is specified as MSG_EXCEPT, which means getting the first message other than the msgtyp type. Msgtyp 0. Indicates that the message MSG_NOERROR with non-msgtyp type is obtained: if the message data body content is greater than msgsz, the message data is truncated to msgsz

6. The example programs msg_send and msg_recv are used to send and receive data to the message queue, respectively.

The msg_sendmsg_send program defines a structure Msg, and the message body part is the structure Person. The program sent 10 messages to the message queue. Msg_send.c

# include # include typedef struct {char name [20]; int age;} Person; typedef struct {long type; Person person;} Msg; int main (int argc, char * argv) {int id = msgget (0x8888, IPC_CREAT | 0664) Msg msg [10] = {{1, {"Luffy", 17}}, {1, {"Zoro", 19}}, {2, {"Nami", 18}}, {2, {"Usopo", 17}}, {1, {"Sanji", 19}}, {3, {"Chopper", 15}}, {4 {"Robin", 28}}, {4, {"Franky", 34}}, {5, {"Brook", 88}}, {6, {"Sunny", 2} After the first run of the int i; for (I = 0; I program msg_send), the message queue in the kernel looks something like this: the 6.2 msg_recvmsg_recv program receives a parameter indicating which type of message to receive. For example,. / msg_recv 4 means to receive a message of type 4 and print it on the screen. # include # include typedef struct {char name [20]; int age;} Person; typedef struct {long type; Person person;} Msg; void printMsg (Msg * msg) {printf ("{type =% ld, name =% s, age =% d}\ n", msg- > type, msg- > person.name, msg- > person.age) } int main (int argc, char * argv []) {if (argc\ n ", argv [0]); return-1;} / / message type to be obtained long type = atol (argv [1]); / / get ipc kernel object id int id = msgget (0x8888, 0); Msg msg; int res While (1) {/ / receives messages of type type res = msgrcv (id, & msg, sizeof (Person), type, IPC_NOWAIT) in a non-blocking manner; if (res 6.3 compiles [root@localhost] # gcc msg_send.c-o msg_send [root@localhost] # gcc msg_recv.c-o msg_recv6.4 runs first msg_send, then msg_recv. Receive all messages image.png receive message image.pngmsgctl function of type 4 gets and sets the properties of the message queue int msgctl (int msqid, int cmd, struct msqid_ds * buf) Msqid: message queue identifier cmd: control instruction IPC_STAT: get the message queue header data of msgid to buf IPC_SET: set the properties of the message queue, the properties to be set need to be stored in buf first, and the properties that can be set include: msg_perm.uid, msg_perm.gid, msg_perm.mode and msg_qbytes buf: message queue management structure. Return value: success: 0 error:-1. The cause of the error is stored in error EACCESS: parameter cmd is IPC_STAT, but does not have permission to read the message queue EFAULT: parameter buf points to invalid memory address EIDRM: message queue with identifier msqid has been deleted EINVAL: invalid parameter cmd or msqid EPERM: parameter cmd is IPC_SET or IPC_RMID, but does not have sufficient permissions to execute instance # include # include struct msgbuf {long mtype Char mtext [];}; int main (int argc, char * * argv) {int msqid; struct msqid_ds info; struct msgbuf buf; struct msgbuf buf1; int flag; int sendlength, recvlength; msqid = msgget (IPC_PRIVATE, 0666); if (msqid what are the functions related to Linux programming message queue? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report