This series of articles written by muge0913, reproduced please note the Source: http://blog.csdn.net/muge0913/article/details/7342907
A message queue is a series of consecutively arranged messages stored in the kernel and accessed by reference identifiers of the message queue. Message Queues are similar to MPs queues. However, the advantage of message queues is that a specific message type is specified for each message. The process that receives the message can request to receive the Next message, you can also request to receive a 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) { printf("msgrcv error\n"); exit(1); } printf("The receved message:%s\n",rcvmsg.msgtext); msgctl(msgid,IPC_RMID,0); exit(0);}