Message Queue for Linux Process Communication and linux Process Communication queue

Source: Internet
Author: User

Message Queue for Linux Process Communication and linux Process Communication queue

Based on the following ideas, this article first elaborates on the message queue mechanism from a macro perspective, then describes the mechanism using the specific code as an example, and finally tries to think about the potential application of the communication mechanism.

 

Message Queue is a simple and efficient way to transmit data between two unrelated processes. It exists independently of the sending and receiving processes.

 

 

Figure 1 Message Queue Communication Mechanism

First, let's take a look at the working mechanism of message queues from a macro perspective. Message Queues exist independently of processes. to distinguish different message queues, you must mark Message Queues with key values, in this way, two unrelated processes can send and receive messages through the key value agreed in advance through the message queue. For example, process A sends A message to the key message queue, and process B reads the message from the Key message queue. In this process, four functions are involved:

# Include <sys/msg. h> # Message Queue related functions and data structure header file int msgctl (int msqid, int cmd, struct msqid_ds * buf); # Control Message Queue function int msgget (key_t key, int msgflg); # create a message queue. The key value uniquely identifies the Message Queue int msgrcv (int msqid, void * msg_ptr, size_t msg_sz, long int msgtype, int msgflg ); # receive message int msgsnd (int msqid, const void * msg_ptr, size_t msg_sz, int msgflg); # Send message

The following code is used as an example to analyze the above process (for detailed analysis, see the code comment)

# Msg1.c Acceptor

# Include <stdlib. h> # include <stdio. h> # include <string. h> # include <errno. h> # include <unistd. h> # include <sys/msg. h ># header file struct my_msg_st {long int my_msg_type; char some_text [BUFSIZ] ;}; # Message format int main () {int running = 1; int msgid; struct my_msg_st some_data; long int msg_to_receive = 0; msgid = msgget (key_t) 1234,066 6 | IPC_CREAT ); # create a message queue with the identifier key = 1234. Pay attention to the consistency between the sender and acceptor. if (msgid =-1) {fprintf (stderr, "msgget failed with error: % d \ n ", errno); exit (EXIT_FAILURE) ;}# error handling: Message Queue identifier is returned when msgget is called successfully, and-1 while (running) is returned if the call fails) {if (msgrcv (msgid, (void *) & some_data, BUFSIZ, msg_to_receive, 0) =-1) {# receive messages from the message queue, if the request fails to be received, execute the if statement and exit fprintf (stderr, "msgrcv failed with error: % d \ n", errno); exit (EXIT_FAILURE);} printf ("You wrote: % s ", some_data.some_text); if (strncmp (some_data.some_text," end ", 3) = 0) {# if the received text contains" end ", set running to 0, result: Exit while LOOP running = 0;} if (msgctl (msgid, IPC_RMID, 0) =-1) {# delete this message, if the deletion fails, execute the if statement and exit fprintf (stderr, "msgctl (IPC_RMID) failed \ n"); exit (EXIT_FAILURE);} exit (EXIT_SUCCESS );}
# Msg2.c producer

# Include <stdlib. h> # include <stdio. h> # include <string. h> # include <errno. h> # include <unistd. h> # include <sys/msg. h ># define MAX_TEXT 512 struct my_msg_st {long int my_msg_type; char some_text [MAX_TEXT] ;};# message format, consistent with int main () {int running = 1; struct my_msg_st some_data; int msgid; char buffer [BUFSIZ]; msgid = msgget (key_t) 1234,066 6 | IPC_CREAT); # create a message queue with the message identifier key = 1234. If the queue already exists, the identifier of the queue is directly returned to send and receive messages to the queue if (msgid =-1) {fprintf (stderr, "msgget failed with error: % d \ n ", errno); exit (EXIT_FAILURE) ;}# error handling, same as the recipient msg1 while (running) {printf (" Enter some text: "); fgets (buffer, BUFSIZ, stdin); # input text from the console and store it in the buffer some_data.my_msg_type = 1; # type filling. In this example, strcpy (some_data.some_text, buffer); # copy the buffer data to some_text. if (msgsnd (msgid, (void *) & some_data, MAX_TEXT, 0) =-1) {# send a message to the message queue, if sending fails, execute the if statement and exit fprintf (stderr, "msgsnd failed \ n"); exit (EXIT_FAILURE);} if (strncmp (buffer, "end", 3) = 0) {# If "end" is sent, exit while after "end" is sent, and end program running = 0 ;}} exit (EXIT_SUCCESS );}

The following are the simulated results on the console:

$./Msg2

Enter some text:Hello

Enter some text:How are you today?

Enter some text:End

$./Msg1

You wrote:Hello

You wrote:How are you today?

You wrote:End

$

Potential Application of Message Queue

Figure 2 Application of message queue in daemon

2. If there are three graphical interfaces, they correspond to process 1, process 2, and process 3 respectively. All three applications require mouse and keyboard operations. If you add code that captures mouse and keyboard operations to each process, three such codes are required, it is a waste of resources (memory space ). If we separate the captured mouse and keyboard operation code into a separate process, the process sends captured mouse and keyboard operations to a specific message queue, currently, the image activation program can extract mouse and keyboard operations from the message queue and then execute subsequent commands accordingly.In this way, the common parts of different applications can be extracted to simplify application design and design more optimized common processing programs.

 

Sublimation of potential application of Message Queue

 

Some methods of processing program commonalities:

Library: some common functions provide library functions and provide good definitions for external applications. When applications apply these functions, they only need to call corresponding interfaces.

Daemon: puts forward the common parts of an application as a daemon. Through a communication mechanism, the information exchange between the daemon process and the application is realized.

 

References: fourth edition of Linux Programming

 

 

 

 

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.