Linux under C programming: Message Queuing instance

Source: Internet
Author: User
Tags message queue printf sprintf linux

Message Queuing is a series of sequential messages that are stored in the kernel and accessed through the reference identifier of the message queue. Message Queuing is similar to pipelines, but the advantage of using Message Queuing is that a specific message type is specified for each message, the process that receives the message can request to receive the next message, or it can request the next specific type of message.

#include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <stdio.h&gt     
;     
    #include <string.h> int main () {key_t unique_key;     
             
    int msgid;     
    int status;     
    Char str1[]={"Test Message:hello muge0913"};     
             
    Char str2[]={"Test Message:goodbye muge0913"};     
    struct MSGBUF {long msgtype;     
    Char msgtext[1024];     
         
    }sndmsg,rcvmsg;     
    if ((MsgId = Msgget (ipc_private,0666)) ==-1) {printf ("Msgget error!\n");     
    Exit (1);     
    } sndmsg.msgtype = 111;     
         
    sprintf (SNDMSG.MSGTEXT,STR1);     
    if (msgsnd (msgid, struct msgbuf *) &sndmsg,sizeof (str1) +1,0) ==-1) {printf ("msgsnd error!\n");     
    Exit (1);     
    } sndmsg.msgtype = 222;     
    sprintf (SNDMSG.MSGTEXT,STR2); if (msgsnd (MsgId, struct MSGBUF *) &sndmsg,sizeof (str2) +1,0) ==-1) {printf ("msgsnd error\n");     
    Exit (1); } if (status = MSGRCV (MsgId, (struct msgbuf *) &rcvmsg,80,222,ipc_nowait)) ==-1) {p     
    rintf ("Msgrcv error\n");     
    Exit (1);     
    printf ("The receved message:%s\n", rcvmsg.msgtext);     
    Msgctl (msgid,ipc_rmid,0);     
Exit (0); }

View a full set of articles: Http://www.bianceng.cn/Programming/C/201212/34807.htm

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.