-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA.c
78 lines (73 loc) · 1.43 KB
/
A.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
#include "func.h"
int shmid;
struct t
{
char buf3[128];
};
struct t *p;
void final(int signum)
{
shmdt(p);
shmctl(shmid,IPC_RMID,NULL);
printf("No.%d signal is coming, shm will be deleted!\n",signum);
exit(0);
}
int main(int argc,char **argv)
{//process A
args_check(argc,3);
signal(SIGINT,final);
shmid=shmget(1234,4096,IPC_CREAT|0600);
p=(struct t *)shmat(shmid,NULL,0);
int fdr=open(argv[1],O_RDONLY);
if(-1==fdr)
{
printf("1.pipe open fail\n");
system("pause");
return -1;
}
int fdw=open(argv[2],O_WRONLY);
if(-1==fdw)
{
printf("2.pipe open fail\n");
system("pause");
return -1;
}
printf("I am process A\n");
char buf[128];
fd_set readset;
int ret;
while(1)
{
FD_ZERO(&readset);
FD_SET(STDIN_FILENO,&readset);
FD_SET(fdr,&readset);
ret=select(fdr+1,&readset,NULL,NULL,NULL);
if(ret<0)
{
printf("select() error");
system("pause");
return -1;
}
if(FD_ISSET(STDIN_FILENO,&readset))
{
memset(buf,0,sizeof(buf));
read(STDIN_FILENO,buf,sizeof(buf));
write(fdw,buf,strlen(buf)-1);
strcpy(p->buf3,buf);
//printf("from A: %s\n",buf);
}
if(FD_ISSET(fdr,&readset))
{
memset(buf,0,sizeof(buf));
read(fdr,buf,sizeof(buf));//the contend of f2 read to buf
printf("from B:%s\n",buf);
strcpy(p->buf3,buf);
//printf("from B: %s\n",buf);
}
if(strcmp(buf,"bye")==0)
{
break;
}
}
exit(0);
}