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