使用消息队列完成两个进程之间相互通信:
进程a:
1 #include <head.h>
2 //定义一个发送消息的结构体类型
3 struct msgbuf
4 {
5 long mtype;//消息类型
6 char mtext[1024];//消息正文
7
8 };
9 //定义一个宏计算正文大小
10 #define SIZE (sizeof(struct msgbuf)-sizeof(long))
11 /***************************************/
12 //分支线程
13 void *task(void*arg)
14 {
15 struct msgbuf msg;
16 //创建key值
17 key_t key=ftok("/",'k');
18 if (key==-1)
19 {
20 perror("ftok key error");
21 return NULL;
22 }
23 printf("key=%#x\n",key);
24 //通过key值来创建消息队列
25 int msgid=msgget(key,IPC_CREAT|0664);
26 if (msgid==-1)
27 {
28 perror("msgget创建消息队列 error");
29 return NULL;
30 }
31 printf("msgid=%d\n",msgid);
32 //将消息从消息队列中读出
33 while (1)
34 {
35 //读取消息队列中的消息
36 msgrcv(msgid,&msg,SIZE,10,0);
37 printf("收到消息:%s\n",msg.mtext);
38 if (strcmp(msg.mtext,"quit")==0)
39 {
40 break;
41 }
42
43 }
44
45
46
47 //退出分支线程
48 pthread_exit(NULL);
49 }
50 /*********************************************/
51 int main(int argc, const char *argv[])
52 {
53 //创建线程号
54 pthread_t tid=-1;
55 //定义一个结构体变量
56 struct msgbuf buf;
57 if (pthread_create(&tid,NULL,task,NULL)!=0)
58 {
59 printf("create tid error");
60 return -1;
61 }
62 //主线程
63 //创建key值
64 key_t key=ftok("/",'k');
65 if (key==-1)
66 {
67 perror("ftok key error");
68 return -1;
69 }
70 printf("key=%#x\n",key);
71 //通过key值来创建消息队列
72 int msgid=msgget(key,IPC_CREAT|0664);
73 if (msgid==-1)
74 {
75 perror("msgget创建消息队列 error");
76 return -1;
77 }
78 printf("msgid=%d\n",msgid);
79 //将消息存入消息队列中
80 while (1)
81 {
82 buf.mtype=15;
83 printf("请输入正文:\n");
84 scanf("%s",buf.mtext);
85 //消息放入消息队列
86 msgsnd(msgid,&buf,SIZE,0);
87 if (strcmp(buf.mtext,"quit")==0)
88 {
89 break;
90 }
91
92 }
93 //删除消息队列
94 if(msgctl(msgid,IPC_RMID,NULL)==-1)
95 {
96 perror("IPC_RMID ERROR");
97 return -1;
98
99 }
100 //回收分支线程
101 pthread_join(tid,NULL);
102
103
104 return 0;
105 }
~
~
进程b
1 #include <head.h>
2 //定义一个发送消息的结构体类型
3 struct msgbuf
4 {
5 long mtype;//消息类型
6 char mtext[1024];//消息正文
7
8 };
9 //定义一个宏计算正文大小
10 #define SIZE (sizeof(struct msgbuf)-sizeof(long))
11 //分支线程
12 void *task(void*arg)
13 {
14 struct msgbuf buf;
15 //创建key值
16 key_t key=ftok("/",'k');
17 if (key==-1)
18 {
19 perror("ftok key error");
20 return NULL;
21 }
22 printf("key=%#x\n",key);
23 //通过key值来创建消息队列
24 int msgid=msgget(key,IPC_CREAT|0664);
25 if (msgid==-1)
26 {
27 perror("msgget创建消息队列 error");
28 return NULL;
29 }
30 printf("msgid=%d\n",msgid);
31 //将消息从消息队列中读出
32 while (1)
33 {
34 //读取消息队列中的消息
35 msgrcv(msgid,&buf,SIZE,15,0);
36 printf("收到消息:%s\n",buf.mtext);
37 if (strcmp(buf.mtext,"quit")==0)
38 {
39 break;
40 }
41
42 }
43
44
45
46 //退出分支线程
47 pthread_exit(NULL);
48 }
49 int main(int argc, const char *argv[])
50 {
51 //创建线程号
52 pthread_t tid=-1;
53 //定义一个结构体变量
54 struct msgbuf buf;
55 if (pthread_create(&tid,NULL,task,&buf)!=0)
56 {
57 printf("create tid error");
58 return -1;
59 }
60 //主线程
61 //创建key值
62 key_t key=ftok("/",'k');
63 if (key==-1)
64 {
65 perror("ftok key error");
66 return -1;
67 }
68 printf("key=%#x\n",key);
69 //通过key值来创建消息队列
70 int msgid=msgget(key,IPC_CREAT|0664);
71 if (msgid==-1)
72 {
73 perror("msgget创建消息队列 error");
74 return -1;
75 }
76 printf("msgid=%d\n",msgid);
77 //将消息存入消息队列中
78 while (1)
79 {
80 buf.mtype=10;
81 printf("请输入正文:\n");
82 scanf("%s",buf.mtext);
83 //消息放入消息队列
84 msgsnd(msgid,&buf,SIZE,0);
85 if (strcmp(buf.mtext,"quit")==0)
86 {
87 break;
88 }
89
90 }
91 //删除消息队列
92 if(msgctl(msgid,IPC_RMID,NULL)==-1)
93 {
94 perror("IPC_RMID ERROR");
95 return -1;
96
97 }
98 //回收分支线程
99 pthread_join(tid,NULL);
100
101
102 return 0;
103 }
~
~