Inter-process communication-message transmission (Message Queue)

Source: Internet
Author: User

[Statement: This article is only intended for self-Summary and mutual communication, and may be omitted. Contact Email: Mr_chenping@163.com message queue is the list of messages. We can think of it as a linked list queue. Users can add or read messages from messages. In this way, it has the FIFO feature, but it can implement random query of messages, therefore, MQ has a greater advantage than FIFO. Message Queues exist in the kernel and are identified by queue IDs. Message Queue features: 1. It can be used in kinship processes (threads) and non-kinship processes (threads. 2. data can be accessed sequentially and randomly. 3. The System V Message Queue continues with the kernel. It is deleted only when the kernel restarts or explicitly deletes a message queue. The following two Code demonstrates the function: server. c: Creates a message queue, obtains the input content on the keyboard, and then writes it to the message queue. Client. c: Open the Message Queue created by server. c, read the content in the message queue, and display it on the screen. Server: [cpp] # include <sys/types. h> # include <sys/ipc. h> # include <sys/msg. h> # include <stdio. h> # include <stdlib. h> # include <unistd. h> # include <string. h> # include <string. h ># define BUFFER_SIZE 512 struct message {long msg_type; char msg_text [BUFFER_SIZE] ;}; int main () {int qid; key_t key; struct message msg; /* generate a standard key */if (key = ftok (". ", 512) =-1) {perror (" ftok "); exit (1);}/* Create A Message Queue */if (qid = msgget (key, IPC_CREAT | 0666) =-1) {perror ("msgget"); exit (1);} printf ("Open queue % d \ n", qid ); while (1) {printf ("input some message to the queue:"); if (fgets (msg. msg_text, BUFFER_SIZE, stdin) = NULL) {puts ("no message"); exit (1);} msg. msg_type = getpid ();/* Add a message to the Message Queue */if (msgsnd (qid, & msg, strlen (msg. msg_text), 0) <0) {perror ("message posted"); exit (1);} if (strncmp (msg. msg_text, "quit", 4) = 0) {break ;}} exit (0);} customer service end: [cpp] # include <sys/types. h> # include <sys/ipc. h> # include <sys/msg. h> # include <stdio. h> # include <stdlib. h> # include <unistd. h> # include <string. h> # include <string. h ># define BUFFER_SIZE 512 struct message {long msg_tpye; char msg_text [BUFFER_SIZE] ;}; int main () {int qid; key_t key; struct message msg; /* generate a standard key */if (key = ftok (". ", 512) =-1) {perror (" ftok "); exit (1);} if (qid = msgget (key, IPC_CREAT | 0666 )) <0) {perror ("msgget"); exit (1);} printf ("Open queue % d \ n", qid); do {memset (msg. msg_text, 0, BUFFER_SIZE); if (msgrcv (qid, (void *) & msg, BUFFER_SIZE, 0, 0) <0) {perror ("msgrcv "); exit (1);} printf ("The message form process % d: % s", msg. msg_tpye, msg. msg_text);} while (strncmp (msg. msg_text, "quit", 4); if (msgctl (qid, IPC_RMID, NULL) <0) {perror ("msgctl"); exit (1 );} exit (0 );}

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.