-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB.c
88 lines (76 loc) · 1.93 KB
/
B.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "func.h"
int msgid;
void final(int signum)
{
msgctl(msgid,IPC_RMID,NULL);
printf("No.%d signal is coming, shm will be deleted!\n",signum);
exit(0);
}
struct msgbuf
{
long mtype;
char mtext[128];
};//消息队列的通信内容都输到这个数组里
int main(int argc,char **argv)
{//process B
args_check(argc,3);
signal(SIGINT,final);
msgid=msgget(1000,IPC_CREAT|0600);//创建消息队列
struct msgbuf buf2;
int fdw=open(argv[1],O_WRONLY);
int fdr=open(argv[2],O_RDONLY);
printf("I am process B\n");
char buf[128];
int ret,ret1;
fd_set readset;
while(1)
{
FD_ZERO(&readset);
FD_SET(STDIN_FILENO,&readset);
FD_SET(fdr,&readset);
select(fdr+1,&readset,NULL,NULL,NULL);
if(FD_ISSET(STDIN_FILENO,&readset))
{
memset(buf,0,sizeof(buf));
read(STDIN_FILENO,buf,sizeof(buf));
write(fdw,buf,strlen(buf)-1);
strcpy(buf2.mtext,buf);
buf2.mtype=1;
ret1=msgsnd(msgid,&buf2,sizeof(buf2.mtext),0);
if(-1==ret1)
{
puts("send msg fail\n");
system("pause");
return -1;
}
/*if(!strncmp(buf2.mtext,"bye",3))
{
buf2.mtype=2;
msgsnd(msgid,&buf2,sizeof(buf2.mtext),0);
break;
}*/
memset(buf2.mtext,0,sizeof(buf2.mtext));
}
if(FD_ISSET(fdr,&readset))
{
memset(buf,0,sizeof(buf));
read(fdr,buf,sizeof(buf));//the contend of f2 read to buf
printf("from A: %s\n",buf);
strcpy(buf2.mtext,buf);
buf2.mtype=1;
ret1=msgsnd(msgid,&buf2,sizeof(buf2.mtext),0);
if(-1==ret1)
{
puts("send msg fail\n");
system("pause");
return -1;
}
memset(buf2.mtext,0,sizeof(buf2.mtext));
}
if(strcmp(buf,"bye")==0)
{
break;
}
}
exit(0);
}