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>
;
#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