In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to analyze Linux message queue programming. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
Message queue, one of the communication mechanisms of Unix, can be understood as a container for storing messages (data). Write the message to the message queue, and then fetch the message from the message queue, generally in first-in-first-out order. It can solve the problems of different reading and writing speed (processing data speed) and system coupling between the two processes, and the messages in the message queue will not disappear even if the process crashes.
The simplest process of using message memory
① ftok function generates key values
The ② msgget function creates a message queue
The ③ msgsnd function sends messages to message queues
The ④ msgrcv function reads messages from the message queue
⑤ msgctl function to delete message queues
A message data should consist of the following structure, for example
Struct mymesg {long int mtype; / / class, message queue can control the reading of the corresponding type of data, so it is not necessarily in the first-in-first-out order. Later in the article, we will continue to introduce char mtext [size _ t]; / / data, where the passed data is stored}; 1.ftok function generates key values
Each message queue is associated with a corresponding key (shared memory and semaphores are also required).
Required header file # include
Function prototype key_t ftok (const char * path, int id)
Path is an existing pathname
Id is a value between 0,255 and represents the project ID. Take it yourself.
Return value: the key value is returned successfully (equivalent to 32-bit int). Error returns-1
For example: key_t key = ftok ("/ tmp", 66)
The 2.msgget function creates a message queue
Required header file # include
Function prototype int msgget (key_t key,int flag)
The key value generated by key for ftok
Flag can be used to control the creation of a message queue for the required actions and permissions.
The value of flag is IPC _ CREAT: if there is no message queue with key value and the permission is not 0, a message queue is created and a message queue ID is returned. If it exists, return the message queuing ID directly.
The value of flag is IPC_CREAT | IPC_EXCL: if there is no message queue with key value and the permission is not 0, a message queue is created and a message queue ID is returned. If it exists, an error is generated.
Return value: message queuing ID; error returned successfully-1
For example: int id = msgget (key,IPC_CREAT | IPC_EXCL | 0666); create a message queue with permissions of 0666 (all users can read and write, specifically query the contents related to linux permissions), and return a shaping message queue ID. If the key value already exists, an error returns-1.
Int id = msgget (key,IPC_CREAT | 0666); create a message queue with permissions of 0666 (all users can read and write, specifically query the contents related to linux permissions), and return a message queue ID. If the key value already exists, a message queue ID is returned directly.
The 3.msgsnd function sends messages to the message queue
Required header file # include
Function prototype int msgsnd (int msgid,const void * ptr,size_t nbytes,int flag)
Msgid: message queuing ID value returned for msgget
Ptr: mymesg pointer to the message structure
Nbytes: the mtext size of the character array in the message structure mymesg, sizeof (mtext)
Flag: value can be 0, IPC_NOWAIT
When 0, when the message queue is full, the msgsnd will block until the message can be written to the message queue or the message queue is deleted.
When IPC_NOWAIT, when the message queue is full, the msgsnd function will not wait and will immediately return EAGAIN with an error.
Return value: 0 for success;-1 for error
For example: msgsnd (id, (void *) & ckxmsg,512,0)
The 4.msgrcv function reads messages from the message queue
Required header file # include
Function prototype ssize_t msgrcv (int msgid,void * ptr,size_t nbytes,long type,int flag)
Msgid: message queuing ID value returned for msgget
Ptr: mymesg pointer to the message structure
Nbytes: the mtext size of the character array in the message structure mymesg, sizeof (mtext)
Type: in the structure mymesg, we define a long int mtype that is used to distinguish the types of messages
Type = = 0 returns the first message in the queue
Type > 0 returns the first message in the queue with message type type
Type
Flag: can be 0, IPC_NOWAIT, IPC_EXCEPT
When 0, blocking receives messages, and the msgrcv function keeps blocking waiting without this type of message
If it is IPC_NOWAIT, if the message that does not return the condition is called to return immediately, the error code is ENOMSG
When IPC_EXCEPT, use with msgtype to return the first message in the queue that is not of type msgtype
Return value: the length of the data part of the message returned successfully; the error returns-1
For example: msgrcv (id, (void *) & ckxmsg,512,1,0)
The 5.msgctl function controls the message queue
The simple operation is to delete the message queue, and you can also obtain and change the status of the message queue.
Required header file # include
Function prototype int msgctl (int msgid, int cmd, struct msqid_ds * buf)
Msgid is the message queue ID returned by the msgget function.
There are three cmd, and the one that usually deletes the message queue is IPC_RMID;IPC_STAT: take the msqid_ds structure of the queue and store it in the structure pointed to by buf; IPC_SET: change the state of the message queue and copy the uid, gid and mode in the msqid_ds structure referred to by buf to the msqid_ds structure of the message queue. (the kernel maintains a structure for each message queue, which is called msqid_ds. Not to mention here, there are some parameters such as the size of the message queue, pid, storage time, and so on.)
Buf is the structure msqid_ds.
Return value: 0 for success;-1 for error
For example: msgctl (id,IPC_RMID,NULL); delete the message queue of id number
Here is a simple program, a service and a client,service write data to the message queue, client reads data from the message queue, deletes the message queue when service is entered into QUIT, and both programs exit.
1.service.c
# include#include#include#include#includestruct mymesg {long int mtype; char mtext;}; int main () {int id = 0; struct mymesg ckxmsg; key_t key = ftok ("/ tmp", 66); id = msgget (key,IPC_CREAT | 0666); if (id = =-1) {printf ("create msg error\ n") Return 0;} while (1) {char msg [512]; memset (msg,0,sizeof (msg)); ckxmsg.mtype = 1; printf ("input message:"); fgets (msg,sizeof (msg), stdin); strcpy (ckxmsg.mtext,msg) If (msgsnd (id, (void *) & ckxmsg,512,0) {printf ("send msg error\ n"); return 0;} if (strncmp (msg, "QUIT", 4) = = 0) break } if (msgctl (id,IPC_RMID,NULL) {printf ("del msg error\ n"); return 0;} return 0;}
2.client.c
# include#include#include#include#includestruct mymesg {long int mtype;char mtext;}; int main () {int id = 0position struct mymesg ckxmsg;key_t key = ftok ("/ tmp", 66); id = msgget (key,0666 | IPC_CREAT); if (id =-1) {printf ("open msg error\ n"); return 0;} while (1) {if (msgrcv (id, (void *) & ckxmsg,512,1,0) {printf ("receive msg error\ n"); return 0) } printf ("data:%s\ n", ckxmsg.mtext); if (strncmp (ckxmsg.mtext, "QUIT", 4) = = 0) break;} return 0;} above is how to analyze Linux message queue programming shared by Xiaobian. If you happen to have similar doubts, please refer to the above analysis for understanding. If you want to know more about it, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.