Talk C together (98th back: C language instance -- use message queue for inter-process communication 2)
Hello, everyone. In the previous session, we talked about the example of using message queue for inter-process communication. Let's continue with the previous session.Use message queue for interprocess communicationLetter. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!
In the previous session, we introduced the concept of message queue and the usage of related functions. In this session, we use specific examples to illustrate how to use message queue for inter-process communication. Below isProcedure:
1. defines a struct type (the first member must be of the long int type), used as the type of messages in the message queue; 2. use the msgget function to create or obtain a message queue. The key in the Code uses the Random Number 111. 3. add data to the message to be sent (msg_buf.data in the Code); 4. use the msgsnd function to send messages in step 3 to the message queue. use the msgrcv function to obtain a message from the message queue and display the data in the message. 6. to delete a Message Queue using the msgctl function, run IPC_RMID;
The readers will not write code in the body. This program is similar to the example program of shared memory.When running a program, different parameters are used to represent different processes..
Parameter:-w indicates the process in which messages are sent; parameter:-r indicates the process in which messages are received;
In addition, in the program, we only want to demonstrate the usage of message queues without considering inter-process synchronization. Therefore, you must first send a message to the Message Queue before obtaining the message from the message queue. That is, when running the program, use the-w parameter before using the-r parameter. If you do not run the program in this Order, a deadlock will occur.
Below isProgram running resultFor more information, see:
./s -w // Run the program and send a message to the message queue through the parameter w
write data into message.count: 3, data: hello message queue // Display the content of the message sent to the message queue
./s -r // Run the program, read the message from the message queue through the parameter r
the data of message .count: 3, data: hello message queue // Display the content of the message in the received message queue
Let's talk about the example of using message queue for inter-process communication. I want to know what examples will be provided later, and I will try again.