In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Linux in how to achieve message queue programming, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
I. concept
A message queue is a linked list of messages. Think of a message as a record with a specific format and a specific priority. Processes with write access to message queues can add new messages according to certain rules; processes with read permissions can read messages away. If you read it, there will be no more. Message queuing persists with the kernel. The message is deleted only when the kernel is restarted or deleted manually. Within the system scope, the message queue corresponds uniquely to the key value.
Second, steps and ideas
1. Get the key value
2. Open and create message queues
3. Send messages
4. Receive messages
Let's take a look at the details:
1. Get the key value
The code is as follows:
Key_t ftok (char * pathname, char proj)
The header file is. Returns the key value corresponding to the file name, and returns-1 if failed. Proj is the name of the project. Write it casually, but not 0.
Fname is the file name you specify (the file name that already exists). Require permission of-t, or execute with root authority, usually set to / tmp or set to "." I don't need this function here, because the key value can be specified by myself, for example: # define KEY_MSG 0x101
2. Open and create message queues
The code is as follows:
Int msgget (key_t key, int msgflg)
The header file is. Key is obtained by ftok.
Msgflg has:
IPC_CREAT creates a new message queue with file permissions of 0666.
IPC_EXCL is used with IPC_CREAT to indicate that an error is returned if the message queue to be created already exists.
IPC_NOWAIT read and write messages are not blocked.
Create a new message queue when there is no message queue corresponding to key and the msgflg contains the IPC_CREAT flag or the parameter of key is IPC_PRIVATE.
3. Send messages
The code is as follows:
Int msgsnd (int msqid, struct msgbuf * msgp, int msgsz, int msgflg)
Sends a message to the message queue. The id,msgp where the msqid is the message queue is the structure for storing messages. Msgsz is the length of the message, which is different from the size of the array. Msgflg is the message flag, usually 0, or it can be IPC_NOWAIT. Error returns-1.
Message format
The code is as follows:
Struct msgbuf {
Long mtype
Char mtext [100]
}
4. Receive messages
The code is as follows:
Int msgrcv (int msqid, struct msgbuf * msgp, int msgsz, long msgtyp, int msgflg)
Reads a message of type msgtyp from the message queue represented by msqid and stores the message in the msgbuf structure specified by msgp. Messages in the queue will be deleted after reading. Size is the size of the data in the structure, do not calculate msgtyp. Error returns-1.
Third, give examples
Create a message queue, the child process sends data, and the parent process receives the data. To store and read data into the queue.
The code is as follows:
# include#include#include#include#include#define max 100
Struct {long mtype; char data [max];}
Int main (int argc,char * argv []) {int pid; if (Argcymp2) {printf ("Usage: msg [Message]\ n"); return-1;} key_t key; if ((key=ftok ("/ tmp",'g'))
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: 294
*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.