![](https://img-blog.csdnimg.cn/direct/03d41509c0d743aba6acaa15fe804645.png)
A进程
1 #include <head.h>//所用头文件全部放在head.h文件中
2 //消息包结构体
3 struct msgbuf
4 {
5 long mtype;
6 char mtext[128];
7 };
8
9 void *send(void *arg)
10 {
11 int msqid=*(int*)arg;
12
13 struct msgbuf snd;
14 snd.mtype=1;
15 while(1)
16 {
17 printf("A说>>>");
18 fgets(snd.mtext,sizeof(snd.mtext),stdin);
19 snd.mtext[strlen(snd.mtext)-1]='\0';
20
21 //以阻塞的方式发送
22 if(msgsnd(msqid,&snd,sizeof(snd.mtext),0)<0)
23 {
24 perror("msgsnd");
25 break;
26 }
27 if(strcmp(snd.mtext, "quit") == 0)
28 exit(1);
29 }
30 pthread_exit(NULL);
31 }
32
33 void *rcv(void *arg)
34 {
35 int msqid=*(int*)arg;
36 //消息包
37 struct msgbuf rcv;
38 ssize_t res=0;
39
40 while(1)
41 {
42 res=msgrcv(msqid,&rcv,sizeof(rcv.mtext),2,0);
43 if(res<0)
44 {
45 perror("msgcrv");
46
47 msgctl(msqid,IPC_RMID,NULL);
48 break;
49 }
50 if(strcmp(rcv.mtext, "quit") == 0)
51 {
52 exit(1);
53 }
54
55 printf("A听到的%s\n",rcv.mtext);
56 fflush(stdout);
57 }
58
59 pthread_exit(NULL);
60 }
61
62 int main(int argc, const char *argv[])
63 {
64 //计算key值
65 key_t key = ftok("./",1);
66 if(key < 0)
67 {
68 perror("ftok");
69 return -1;
70 }
71 //创建一个消息队列
72 int msqid = msgget(key,IPC_CREAT|0777);
73 if(msqid < 0)
74 {
75 perror("msgget");
76 return -1;
77 }
78
79 pthread_t tid1, tid2;
80 //创建一个线程,发送消息包
81 if(pthread_create(&tid1, NULL,send,(void*)&msqid) != 0)
82 {
83 perror("pthread_create");
84 return -1;
85 }
86
87 //创建一个线程,接受消息包
88 if(pthread_create(&tid2, NULL,rcv, (void*)&msqid) != 0)
89 {
90 perror("pthread_create");
91 return -1;
92 }
93
94 pthread_join(tid1, NULL);
95 pthread_join(tid2, NULL);
96
97 return 0;
98 }
99
B进程
1 #include <head.h>//所用头文件全部放在head.h文件中
2
3 //消息包结构体
4 struct msgbuf
5 {
6 long mtype;
7 char mtext[128];
8 };
9
10 void *rcv(void *arg)
11 {
12 int msqid=*(int*)arg;
13
14 struct msgbuf rcv;
15 ssize_t res=0;
16 while(1)
17 {
18 res=msgrcv(msqid,&rcv,sizeof(rcv.mtext),1,0);
19 if(res<0)
20 {
21 perror("msgcrv");
22
23 msgctl(msqid, IPC_RMID, NULL);
24 break;
25 }
26 if(strcmp(rcv.mtext, "quit") == 0)
27 {
28 exit(1);
29 }
30 printf("B听到的:%s\n",rcv.mtext);
31 fflush(stdout);
32
33 }
34 pthread_exit(NULL);
35
36 }
37
38 void *send(void *arg)
39 {
40 int msqid=*(int*)arg;
41 struct msgbuf snd;
42 snd.mtype=2;
43
44 while(1)
45 {
46 printf("B说>>>");
47 fgets(snd.mtext,sizeof(snd.mtext),stdin);
48 snd.mtext[strlen(snd.mtext)-1]=0;
49 //以阻塞的方式发送
50 if(msgsnd(msqid,&snd,sizeof(snd.mtext),0)<0)
51 {
52 perror("msgsnd");
53 break;
54 }
55 if(strcmp(snd.mtext, "quit") == 0)
56 exit(1);
57 }
58 pthread_exit(NULL);
59 }
60
61 int main(int argc, const char *argv[])
62 {
63 //计算key值
64 key_t key = ftok("./",1);
65 if(key < 0)
66 {
67 perror("ftok");
68 return -1;
69 }
70
71 //创建一个消息队列
72 int msqid = msgget(key,IPC_CREAT|0777);
73 if(msqid < 0)
74 {
75 perror("msgget");
76 return -1;
77 }
78
79 pthread_t tid1, tid2;
80 //创建一个线程,接受消息包
81 if(pthread_create(&tid2, NULL,rcv, (void*)&msqid) != 0)
82 {
83 perror("pthread_create");
84 return -1;
85 }
86
87 //创建一个线程,发送消息包
88 if(pthread_create(&tid1, NULL,send,(void*)&msqid) != 0)
89 {
90 perror("pthread_create");
91 return -1;
92 }
93
94 pthread_join(tid1, NULL);
95 pthread_join(tid2, NULL);
96
97 return 0;
98 }
99