标签:
1 #include <stdio.h> 2 #include <sys/types.h> 3 #include <sys/ipc.h> 4 #include <sys/msg.h> 5 #include <errno.h> 6 #include <string.h> 7 #include <stdlib.h> 8 9 int msg_key = 1234; 10 11 struct MsgNode 12 { 13 long nMsgType; 14 char sBuf[256]; 15 }; 16 17 int main(int argc,char** argv) 18 { 19 int nMsgId = 0; 20 if((argc == 2) && (strcmp(argv[1],"c") == 0)) 21 { 22 nMsgId = msgget(msg_key,IPC_EXCL); 23 if(nMsgId < 0) 24 { 25 nMsgId = msgget(msg_key,0750|IPC_CREAT); 26 if(nMsgId < 0) 27 { 28 perror("msgget"); 29 printf("msgget failed\n"); 30 exit(-1); 31 } 32 } 33 34 for(int i = 0 ;i < 10; ++i) 35 { 36 MsgNode node; 37 node.nMsgType = 1; 38 sprintf(node.sBuf,"This is the %d msg from client.\n",i); 39 40 if(i == 9) 41 { 42 strcpy(node.sBuf,"end\n"); 43 } 44 45 printf("%s",node.sBuf); 46 47 int ret = msgsnd(nMsgId,&node,sizeof(node.sBuf),IPC_NOWAIT); 48 if(ret < 0) 49 { 50 perror("msgsend"); 51 printf("msgsend failed"); 52 exit(-1); 53 } 54 } 55 } 56 else 57 { 58 nMsgId = msgget(msg_key,IPC_EXCL); 59 if(nMsgId < 0) 60 { 61 printf("msgget failed\n"); 62 exit(-1); 63 } 64 65 bool flag = true; 66 while(flag) 67 { 68 MsgNode node; 69 int ret = msgrcv(nMsgId,&node,sizeof(node.sBuf),1,IPC_NOWAIT); 70 if(ret == -1) 71 { 72 perror("msgrecv"); 73 printf("msgrecv failed\n"); 74 exit(-1); 75 } 76 printf("Receive Msg:%s",node.sBuf); 77 //if(strncmp(node.sBuf,"end",3) == 0) 78 //{ 79 // flag = false; 80 //}; 81 } 82 83 int ret = msgctl(nMsgId,IPC_RMID,0); 84 if( ret == -1) 85 { 86 printf("msgctl failed\n"); 87 exit(-1); 88 } 89 } 90 91 return 0; 92 }
需要注意的是
int msgsend(int msgid, const void *msg_ptr, size_t msg_sz, int msgflg);
及
int msgrcv(int msgid, void *msg_ptr, size_t msg_st, long int msgtype, int msgflg);
其中 size_t msg_st并不是消息结构体的大小,而是消息的大小。
如代码中定义了结构
1 struct MsgNode 2 { 3 long nMsgType; 4 char sBuf[256]; 5 };
则 size_t msg_st指的是 sizeof(MsgNode.sBuf)即不包括结构中消息类型的长度。
在接受/发送消息时如果不设置为IPC_NOWAIT则发送时如果消息队列已满则阻塞,接收时如果消息队列为空则阻塞。
标签:
原文地址:http://www.cnblogs.com/boyunzheyue2/p/5640423.html