Linux IPC mechanism: Message Queuing example

Source: Internet
Author: User
Tags message queue strcmp

Program msg1.c to receive messages

<span style= "FONT-SIZE:12PX;" > #include <stdio.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <    Stdlib.h> #include <sys/msg.h>struct my_msg_st {long int my_msg_type; Char some_text[bufsiz];};    int main () {int running = 1;    int msgid;    struct My_msg_st some_data;    long int msg_to_receive = 0; Establish Message Queuing MsgId = Msgget ((key_t) 1234, 0666 |    Ipc_creat);        if (-1 = = MsgId) {fprintf (stderr, "Msgget failed with error:%d\n", errno);    Exit (Exit_failure);        }//Get the message from the message queue until you meet end while (running) {memset (Some_data.some_text, ' n ', bufsiz); if (-1 = = MSGRCV (MsgId, (void *) &some_data, Bufsiz, msg_to_receive, 0)) {fprintf (stderr, "MAGRCV faile            D with Error%d\n ", errno);        Exit (Exit_failure);        } printf ("You wrote:%s", some_data.some_text); Some_data.some_text[strlen (Some_data.some_text)-1] = ' + ';  Remove line break to find end flag string exactly      if (0 = = strcmp (Some_data.some_text, "End")) running = 0;            }//Delete Message Queuing if (-1 = = Msgctl (msgid, Ipc_rmid, 0)) {fprintf (stderr, "Msgctl (ipc_rmid) failed\n");    Exit (Exit_failure); } exit (exit_success);} </span>

Program msg2.c for sending messages:

<span style= "FONT-SIZE:12PX;" > #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include    <errno.h> #include <sys/msg.h> #define Max_text 512struct my_msg_st {long int my_msg_type; Char some_text[max_text];};    int main () {int running = 1;    struct My_msg_st some_data;    int msgid;    Char Buffer[bufsiz]; MsgId = Msgget ((key_t) 1234, 0666 |    Ipc_creat);        if (-1 = = MsgId) {fprintf (stderr, "Msgget failed with error%d\n", errno);    Exit (Exit_failure);        }//Send message while (running) {memset (buffer, ' + ', bufsiz);        printf ("Enter some text:");        Fgets (buffer, bufsiz, stdin);        Some_data.my_msg_type = 1;        strcpy (some_data.some_text, buffer); if (-1 = = Msgsnd (MsgId, (void *) &some_data, Max_text, 0)) {fprintf (stderr, "msgsnd failed with error%            D\n ", errno);        Exit (Exit_failure); } buffer[strlen (buffer)-1] = ' N ';    if (0 = = strcmp (buffer, "end")) running = 0; } exit (exit_success);} </span>

Output Result:

[Email protected] document]$./MSG2
Enter some Text:hey man
Enter some text:how is you
Enter some text:endsss
Enter some Text:end
[Email protected] document]$./MSG1
You Wrote:hey Mans
You wrote:how is
You wrote:endsss
You wrote:end

The advantage of Message Queuing over named Pipes is that it exists independently of the send and receive processes, eliminating some of the difficulties that can arise when synchronizing named Pipes for opening and closing.


Linux IPC mechanism: Message Queuing example

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.