1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/stat.h>
4 #include <unistd.h>
5 #include <errno.h>
6 #include <sys/types.h>
7 #include <fcntl.h>
8 #include <pthread.h>
9 int main(int argc, const char *argv[])
10 {
11
12 umask(0);
13 if(mkfifo("./fifo",0664) < 0)
14 {
15 if(errno != 17)
16 {
17 perror("mkfifo");
18 return -1;
19 }
20 printf("mkfifo success\n");
21 }
22 if(mkfifo("./fifo1",0664) < 0)
23 {
24 if(errno != 17)
25 {
26 perror("mkfifo");
27 return -1;
28 }
29 printf("mkfifo success\n");
30 }
31 pid_t cpid = fork();
32 char arr[128] = "";
33 char buf[128] = "";
34 while(1)
35 {
36 if(cpid > 0)
37 {
38 int fd = open("./fifo",O_WRONLY);
39 if(fd < 0)
40 {
41 perror("open");
42 return -1;
43 }
44 printf("open rdonly success,fd=%d\n",fd);
45 while(1)
46 {
47 fgets(buf,sizeof(buf),stdin);
48 buf[strlen(buf)-1] = '\0';
49 if(write(fd,buf,sizeof(buf)) < 0)
50 {
51 perror("write");
52 return -1;
53 }
54 printf("发送成功\n");
55
56 if(strcmp(buf,"quit") == 0)
57 break;
58 }
59 }
60 else if(cpid == 0)
61 {
62 int fd1 = open("./fifo1",O_RDONLY);
63 if(fd1 < 0)
64 {
65 perror("open");
66 return -1;
67 }
68 printf("open rdonly success,fd1=%d\n",fd1);
69 ssize_t res;
70 while(1)
71 {
72 bzero(arr,sizeof(arr));
73 res = read(fd1,arr,sizeof(arr));
74 if(res < 0)
75 {
76 perror("read");
77 return -1;
78 }
79 else if(0 == res)
80 {
81 printf("对方进程结束\n");
82 break;
83 }
84 printf("A说:%s\n",arr);
85 if(strcmp(arr,"quit") == 0)
86 break;
87 }
88 }
89 if(strcmp(arr,"quit") == 0 || strcmp(buf,"quit") == 0)
90 break;
91 }
92 return 0;
93 }
94 #include <stdio.h>
95 #include <string.h>
96 #include <sys/stat.h>
97 #include <unistd.h>
98 #include <errno.h>
99 #include <sys/types.h>
100 #include <fcntl.h>
101 #include <pthread.h>
102 int main(int argc, const char *argv[])
103 {
104
105 umask(0);
106 if(mkfifo("./fifo",0664) < 0)
107 {
108 if(errno != 17)
109 {
110 perror("mkfifo");
111 return -1;
112 }
113 printf("mkfifo success\n");
114 }
115 if(mkfifo("./fifo1",0664) < 0)
116 {
117 if(errno != 17)
118 {
119 perror("mkfifo");
120 return -1;
121 }
122 printf("mkfifo success\n");
123 }
124 pid_t cpid = fork();
125 char arr[128] = "";
126 char buf[128] = "";
127 while(1)
128 {
129 if(cpid == 0)
130 {
131 int fd = open("./fifo1",O_WRONLY);
132 if(fd < 0)
133 {
134 perror("open");
135 return -1;
136 }
137 printf("open rdonly success,fd=%d\n",fd);
138 while(1)
139 {
140 fgets(buf,sizeof(arr),stdin);
141 buf[strlen(arr)-1] = '\0';
142 if(write(fd,arr,sizeof(arr)) < 0)
143 {
144 perror("write");
145 return -1;
146 }
147 printf("发送成功\n");
148
149 if(strcmp(arr,"quit") == 0)
150 break;
151 }
152 }
153 else if(cpid == 0)
154 {
155 ssize_t res;
156 while(1)
157 {
158 int fd1 = open("./fifo",O_RDONLY);
159 if(fd1 < 0)
160 {
161 perror("open");
162 return -1;
163 }
164 printf("open rdonly success,fd1=%d\n",fd1);
165 bzero(buf,sizeof(buf));
166 res = read(fd1,buf,sizeof(buf));
167 if(res < 0)
168 {
169 perror("read");
170 return -1;
171 }
172 else if(0 == res)
173 {
174 printf("对方进程结束\n");
175 break;
176 }
177 printf("B说:%s\n",arr);
178 if(strcmp(buf,"quit") == 0)
179 break;
180 }
181 }
182 if(strcmp(arr,"quit") == 0 || strcmp(buf,"quit") == 0)
183 break;
184 }
185 return 0;
186 }
1 #include <stdio.h>
2 #include <pthread.h>
3 #include <unistd.h>
4 #include <string.h>
5 int flag = 0;
6 pthread_mutex_t mutex;
7 pthread_cond_t cond1,cond2,cond3;
8 void* callBack(void* arg)
9 {
10 while(1)
11 {
12 pthread_mutex_lock(&mutex);
13 if(flag != 0)
14 {
15 pthread_cond_wait(&cond1,&mutex);
16 }
17 printf("A");
18 flag = 1;
19 pthread_cond_signal(&cond2);
20 pthread_mutex_unlock(&mutex);
21 }
22 pthread_exit(NULL);
23 }
24 void *callBack1(void* arg)
25 {
26 char c = 0;
27 while(1)
28 {
29 pthread_mutex_lock(&mutex);
30 if(flag != 1)
31 {
32 pthread_cond_wait(&cond2,&mutex);
33 }
34 printf("B");
35 flag = 2;
36 pthread_cond_signal(&cond3);
37 pthread_mutex_unlock(&mutex);
38
39 }
40 pthread_exit(NULL);
41
42 }
43 void* callBack2(void *arg)
44 {
45 pthread_mutex_lock(&mutex);
46 while(1)
47 {
48 if(flag != 2)
49 {
50 pthread_cond_wait(&cond3,&mutex);
51 }
52 printf("C\n");
53 flag = 0;
54 pthread_cond_signal(&cond1);
55 pthread_mutex_unlock(&mutex);
56 }
57 pthread_exit(NULL);
58 }
59
60 int main(int argc, const char *argv[])
61 {
62 pthread_mutex_init(&mutex,NULL);
63 pthread_cond_init(&cond1,NULL);
64 pthread_cond_init(&cond2,NULL);
65 pthread_cond_init(&cond3,NULL);
66 pthread_t tid1,tid2,tid3;
67 if(pthread_create(&tid1,NULL,callBack,NULL) != 0)
68 {
69 fprintf(stderr,"pthread_creat failed __%d__\n",__LINE__);
70 return -1;
71 }
72 if(pthread_create(&tid2,NULL,callBack1,NULL) != 0)
73 {
74 fprintf(stderr,"pthread_creat failed __%d__\n",__LINE__);
75 return -1;
76 }
77 if(pthread_create(&tid3,NULL,callBack2,NULL) != 0)
78 {
79 fprintf(stderr,"pthread_creat failed __%d__\n",__LINE__);
80 return -1;
81 }
82 pthread_join(tid1,NULL);
83 pthread_join(tid2,NULL);
84 pthread_join(tid3,NULL);
85
86 pthread_mutex_destroy(&mutex);
87 pthread_cond_destroy(&cond1);
88 pthread_cond_destroy(&cond2);
89 pthread_cond_destroy(&cond3);
90
91 return 0;
}