-
Notifications
You must be signed in to change notification settings - Fork 2
/
3_mkfifo.c
92 lines (84 loc) · 2.04 KB
/
3_mkfifo.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
89
90
//read.c
#include "func.h"
// read 1.pipe读 2.pipe写
int main(int argc,char** argv)
{
int fdr,fdw;
fdr=open(argv[1],O_RDONLY);
fdw=open(argv[2],O_WRONLY);
printf("fdr=%d,fdw=%d\n",fdr,fdw);
char buf[20]={0};
int ret;
fd_set rdset; //操作描述符集合
struct timeval t;
t.tv_usec=0;
while(1){
FD_ZERO(&rdset);
FD_SET(0,&rdset); //增加fd stdin
FD_SET(fdr,&rdset);
t.tv_sec=3;
ret=select(fdr+1,&rdset,NULL,NULL,&t);
if(ret>0){
if(FD_ISSET(0,&rdset)){
memset(buf,0,sizeof(buf));
ret=read(0,buf,sizeof(buf)); //stdin保存到buf
if(ret>0){
write(fdw,buf,strlen(buf)-1); //write buf to管道2写端
}else {
printf("bye\n");break;}
}
if(FD_ISSET(fdr,&rdset)){
memset(buf,0,sizeof(buf));
ret=read(fdr,buf,sizeof(buf)); //read管道1保存到buf
if(ret>0) printf("B:%s\n",buf);
else {printf("bye\n"); break;}
}
}
else printf("no fd can read");
}
close(fdr);
close(fdw);
return 0;
}
//write.c
#include "func.h"
// write 1.pipe写 2.pipe读
int main(int argc,char** argv)
{
int fdr,fdw;
fdw=open(argv[1],O_WRONLY); //1.pipe
fdr=open(argv[2],O_RDONLY);
printf("fdr=%d,fdw=%d\n",fdr,fdw);
char buf[20]={0};
int ret;
fd_set rdset; //操作描述符集合
struct timeval t;
t.tv_usec=0;
while(1){
FD_ZERO(&rdset);
FD_SET(0,&rdset); //增加fd stdin
FD_SET(fdr,&rdset);
t.tv_sec=3;
ret=select(fdr+1,&rdset,NULL,NULL,NULL);
if(ret>0){
if(FD_ISSET(0,&rdset)){
memset(buf,0,sizeof(buf));
ret=read(0,buf,sizeof(buf)); //stdin保存到buf
if(ret>0){
write(fdw,buf,strlen(buf)-1); //write buf to管道2写端
}else {
printf("bye\n");break;}
}
if(FD_ISSET(fdr,&rdset)){
memset(buf,0,sizeof(buf));
ret=read(fdr,buf,sizeof(buf)); //read管道1保存到buf
if(ret>0) printf("A:%s\n",buf);
else {printf("bye\n"); break;}
}
}
else printf("no fd can read");
}
close(fdr);
close(fdw);
return 0;
}