linux网络编程-进程间通信——消息队列
作者:互联网
1 #include<stdio.h> 2 #include<string.h> 3 #include<sys/types.h> 4 #include<sys/ipc.h> 5 #include<sys/msg.h> 6 int main() 7 { 8 int ret = -1; 9 key_t key; 10 char *msgpath = "./ipc/"; 11 int msgFlags, msg_id; 12 13 int msg_sflags, msg_rflags; 14 struct msgmbuf{ 15 long int mtype; 16 char mtext[10]; 17 }; 18 19 struct msqid_ds msg_info; 20 struct msgmbuf msg_mbuf; 21 22 key = ftok(msgpath,'b'); 23 if(key!=-1) 24 { 25 printf("ftok 成功!key = %d\n", key); 26 } 27 else 28 { 29 printf("ftok 失败!key = %d\n", key); 30 } 31 32 msgFlags = IPC_CREAT|IPC_EXCL; 33 msg_id = msgget(key,msgFlags|0666); 34 if(-1 != msg_id) 35 { 36 printf("消息建立成功,msgId = %d\n", msg_id); 37 } 38 else 39 { 40 printf("msgget 失败, msgId = %d\n", msg_id); 41 return 0; 42 } 43 44 45 msg_sflags = IPC_NOWAIT; 46 msg_mbuf.mtype = 10; 47 memcpy(msg_mbuf.mtext,"1", sizeof("1")); 48 printf("msgLen = %d\n", sizeof("1")); 49 ret = msgsnd(msg_id, &msg_mbuf, sizeof(msg_mbuf.mtext), msg_sflags); 50 if(-1 == ret) 51 { 52 printf("消息发送失败\n"); 53 } 54 55 msg_rflags = IPC_NOWAIT|MSG_NOERROR; 56 57 ret = msgrcv(msg_id, &msg_mbuf,10,10,msg_rflags); 58 if(-1 == ret) 59 { 60 printf("消息接收失败\n"); 61 } 62 else 63 { 64 printf("消息接收成功\n"); 65 } 66 67 68 printf("--1--\n"); 69 70 ret = msgctl(msg_id, IPC_RMID, NULL); 71 if(-1 == ret) 72 { 73 printf("删除消息失败\n"); 74 return 0; 75 } 76 else 77 { 78 printf("删除消息成功\n"); 79 } 80 81 return 0; 82 }
标签:linux,编程,mbuf,ret,间通信,key,printf,msg,id 来源: https://www.cnblogs.com/zzx2bky/p/15913767.html