Talk C together (98th back: C language instance -- use message queue for inter-process communication 2)

Source: Internet
Author: User

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.

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.