1 #include <stdio.h>
2 #include <sys/stat.h>
3 #include <sys/types.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <semaphore.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 int main(int argc, const char *argv[])
10 {
11
12 if(mkfifo("./fifoA",0664)<0)
13 {
14 if(errno!=17)
15 {
16 perror("fifoA");
17 return -1;
18 }
19 }
20 printf("mkfifo successs __%d__\n",__LINE__);
21
22
23 if(mkfifo("./fifoB",0664)<0)
24 {
25 if(errno!=17)
26 {
27 perror("fifoB");
28 return -1;
29 }
30 }
31 printf("mkfifo successs __%d__\n",__LINE__);
32 int fd_r=open("./fifoA",O_RDONLY);
33 if(fd_r<0)
34 {
35 perror("open");
36 return -1;
37 }
38 printf("open rdonly successs! __%d__\n",__LINE__);
39
40
41 int fd_w = open("./fifoB",O_WRONLY);
42 if(fd_w<0)
43 {
44 perror("open");
45 return -1;
46 }
47 printf("open wronly success __%d__\n",__LINE__);
48 printf("连接成功\n");
49
50
51 char buf[128]="";
52 ssize_t res=0;
53 int c=-1;
54 while(1)
55 {
56
57 bzero(buf,sizeof(buf));
58
59 res=read(fd_r,buf,sizeof(buf));
60
61 c=strcmp(buf,"quit");
62
63 if(0==c)
64 {
65 break;
66 }
67 if(res<0)
68 {
69 perror("read");
70 return -1;
71 }
72 else if(0==res)
73 {
74 printf("数据读取完毕\n");
75 break;
76 }
77 printf("B:%s\n",buf);
78
79 printf("A: ");
80 fgets(buf,sizeof(buf),stdin);
81 buf[strlen(buf)-1]='\0';
82
83 if((write(fd_w,buf,sizeof(buf)))<0)
84 {
85 perror("write");
86 return -1;
87 }
88
89
90
91 }
92 close(fd_r);
93 close(fd_w);
94
95
96
97
98 return 0;
99 }
~
~
~
~
~
~
~
1 #include <stdio.h>
2 #include <sys/stat.h>
3 #include <sys/types.h>
4 #include <unistd.h>
5 #include <semaphore.h>
6 #include <fcntl.h>
7 #include <errno.h>
8 #include <string.h>
9
10 int main(int argc, const char *argv[])
11 {
12
13 if(mkfifo("./fifoA",0664)<0)
14 {
15 if(errno!=17)
16 {
17 perror("fifoA");
18 return -1;
19 }
20 }
21 printf("mkfifo successs __%d__\n",__LINE__);
22
23
24 if(mkfifo("./fifoB",0664)<0)
25 {
26 if(errno!=17)
27 {
28 perror("fifoB");
29 return -1;
30 }
31 }
32 printf("mkfifo successs __%d__\n",__LINE__);
33
34 int fd_w = open("./fifoA",O_WRONLY);
35 if(fd_w<0)
36 {
37 perror("open");
38 return -1;
39 }
40 printf("open wronly success __%d\n__",__LINE__);
41 int fd_r=open("./fifoB",O_RDONLY);
42 if(fd_r<0)
43 {
44 perror("open");
45 return -1;
46 }
47 printf("open reaadonly successs__%d__\n",__LINE__);
48 printf("连接成功\n");
49
50
51
52 char buf[128]="";
53 ssize_t res=0;
54 int c=-1;
55 while(1)
56 {
57
58 printf("B: ");
59 fgets(buf,sizeof(buf),stdin);
60 buf[strlen(buf)-1]='\0';
61
62 if((write(fd_w,buf,sizeof(buf)))<0)
63 {
64 perror("write");
65 return -1;
66 }
67
68
69 bzero(buf,sizeof(buf));
70
71 res=read(fd_r,buf,sizeof(buf));
72 c=strcmp(buf,"quit");
73 if(0==c)
74 {
75 break;
76 }
77
78 if(res<0)
79 {
80 perror("read");
81 return -1;
82 }
83 if(0==res)
84 {
85 printf("数据读取完毕\n");
86 break;
87 }
88 printf("A: %s\n",buf);
89 }
90
91 close(fd_r);
92 close(fd_w);
93
94
95
96
97 return 0;
98 }
~
~
~
~
~
~
~
~
~
~
~