1.
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <head.h>
5 pthread_t tid1;
6 pthread_t tid2;
7 struct aaa
8 {
9 int fd1;
10 int fd2;
11 };
12 char buf1[128] = "";
13 char buf2[128] = "";
14 void *zxc1(void *p)
15 {
16 int fd2 = *(int *)p;
17 while(1)
18 {
19 bzero(buf1,sizeof(buf1));
20 printf("请输入>>>\n");
21 scanf("%s",buf1);
22 if(write(fd2,buf1,sizeof(buf1)) < 0)
23 {
24 ERR_MSG("write");
25 return NULL;
26 }
27 if(strcmp(buf1,"quit") == 0)
28 {
29 exit(0);
30 }
31 }
32 }
33
34 void *zxc2(void* p)
35 {
36 int fd1 = *(int *)p;
37 while(1)
38 {
39 bzero(buf2,sizeof(buf2));
40 if(read(fd1,buf2,sizeof(buf2)) < 0)
41 {
42 ERR_MSG("read");
43 return NULL;
44 }
45 if(strcmp(buf2,"quit") == 0)
46 {
47 exit(0);
48 }
49 printf("%s\n",buf2);
50 }
51 }
52 int main(int argc, const char *argv[])
53 {
54 if(mkfifo("./fifo1",0664) < 0)
55 {
56 if(errno != 17)
57 {
58 ERR_MSG("fifo");
59 return -1;
60 }
61 }
62 if(mkfifo("./fifo2",0664) < 0)
63 {
64 if(errno != 17)
65 {
66 ERR_MSG("fifo");
67 return -1;
68 }
69 }
70 int fd1 = open("./fifo1",O_RDONLY);
71 if(fd1 < 0)
72 {
73 ERR_MSG("open");
74 return -1;
75 }
76 int fd2 = open("./fifo2",O_WRONLY);
77 if(fd2 < 0)
78 {
79 ERR_MSG("open");
80 return -1;
81 }
82
83 if(pthread_create(&tid1,NULL,zxc1,(void*)&fd2) != 0)
84 {
85 printf("子线程1创建失败\n");
86 return -1;
87 }
88
89 if(pthread_create(&tid2,NULL,zxc2,(void*)&fd1) != 0)
90 {
91 printf("子线程2创建失败\n");
92 return -1;
93 }
94
95 pthread_join(tid2,NULL);
96 pthread_join(tid1,NULL);
97
98 close(fd1);
99 close(fd2);
100 return 0;
101 }
~
~
~
~
~
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <head.h>
5 pthread_t tid1;
6 pthread_t tid2;
7 struct aaa
8 {
9 int fd1;
10 int fd2;
11 };
12 char buf1[128] = "";
13 char buf2[128] = "";
14 void *zxc1(void *p)
15 {
16 int fd1 = *(int *)p;
17 while(1)
18 {
19 bzero(buf1,sizeof(buf1));
20 printf("请输入>>>\n");
21 scanf("%s",buf1);
22 if(write(fd1,buf1,sizeof(buf1)) < 0)
23 {
24 ERR_MSG("write");
25 return NULL;
26 }
27 if(strcmp(buf1,"quit") == 0)
28 {
29 exit(0);
30 }
31 }
32 }
33
34 void *zxc2(void* p)
35 {
36 int fd2 = *(int *)p;
37 while(1)
38 {
39 bzero(buf2,sizeof(buf2));
40 if(read(fd2,buf2,sizeof(buf2)) < 0)
41 {
42 ERR_MSG("read");
43 return NULL;
44 }
45 if(strcmp(buf2,"quit") == 0)
46 {
47 exit(0);
48 }
49 printf("%s\n",buf2);
50 }
51 }
52 int main(int argc, const char *argv[])
53 {
54 if(mkfifo("./fifo1",0664) < 0)
55 {
56 if(errno != 17)
57 {
58 ERR_MSG("fifo");
59 return -1;
60 }
61 }
62 if(mkfifo("./fifo2",0664) < 0)
63 {
64 if(errno != 17)
65 {
66 ERR_MSG("fifo");
67 return -1;
68 }
69 }
70 int fd1 = open("./fifo1",O_WRONLY);
71 if(fd1 < 0)
72 {
73 ERR_MSG("open");
74 return -1;
75 }
76 int fd2 = open("./fifo2",O_RDONLY);
77 if(fd2 < 0)
78 {
79 ERR_MSG("open");
80 return -1;
81 }
82
83 if(pthread_create(&tid1,NULL,zxc1,(void*)&fd1) != 0)
84 {
85 printf("子线程1创建失败\n");
86 return -1;
87 }
88
89 if(pthread_create(&tid2,NULL,zxc2,(void*)&fd2) != 0)
90 {
91 printf("子线程2创建失败\n");
92 return -1;
93 }
94
95 pthread_join(tid2,NULL);
96 pthread_join(tid1,NULL);
97
98 close(fd1);
99 close(fd2);
100 return 0;
101 }
~
2.xmind