Message Queuing for interprocess communication

Source: Internet
Author: User

What is Message Queuing?

Message Queuing provides a way to send a block of data from one process to another.

#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys /msg.h> #include <errno.h> #define MAX_TEXT 512struct msg_st{long int msg_type;char text[max_text];}; int main () {int running = 1;struct Msg_st data;char buffer[bufsiz];int msgid = -1;//Establish message Queue MsgId = Msgget ((key_t) 1234, 0666 | Ipc_creat); if (MsgId = =-1) {fprintf (stderr, "Msgget failed with Error:%d\n", errno); exit (exit_failure);} Write a message to the message queue until you write Endwhile (running) {//Input data printf ("Enter some text:"), fgets (buffer, Bufsiz, stdin);d ata.msg_type = 1;    Note 2strcpy (data.text, buffer);//Send data to queue if (Msgsnd (MsgId, (void*) &data, max_text, 0) = =-1) {fprintf (stderr, " Msgsnd failed\n "); exit (exit_failure);} Input end End Input if (strncmp (buffer, "End", 3) = = 0) running = 0;sleep (1);} Exit (exit_success);}

#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys /msg.h> #include <errno.h> #define MAX_TEXT 512struct msg_st{long int msg_type;char text[max_text];}; int main () {int running = 1;struct Msg_st data;char buffer[bufsiz];int msgid = -1;//Establish message Queue MsgId = Msgget ((key_t) 1234, 0666 | Ipc_creat); if (MsgId = =-1) {fprintf (stderr, "Msgget failed with Error:%d\n", errno); exit (exit_failure);} Write a message to the message queue until you write Endwhile (running) {//Input data printf ("Enter some text:"), fgets (buffer, Bufsiz, stdin);d ata.msg_type = 1;    Note 2strcpy (data.text, buffer);//Send data to queue if (Msgsnd (MsgId, (void*) &data, max_text, 0) = =-1) {fprintf (stderr, " Msgsnd failed\n "); exit (exit_failure);} Input end End Input if (strncmp (buffer, "End", 3) = = 0) running = 0;sleep (1);} Exit (exit_success);}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Message Queuing for interprocess communication

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.