-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex11-01.c
60 lines (55 loc) · 1.58 KB
/
ex11-01.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
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#define MSGSIZE 100
int main() {
pid_t pid1, pid2;
int filedes, p1[2], p2[2];
fd_set initset, newset;
int nread;
char msg[MSGSIZE];
pipe(p1);
pipe(p2);
pid1 = pid2 = 0;
pid1 = fork();
if (pid1 > 0)
pid2 = fork();
if (pid1 > 0 && pid2 > 0) {
printf("[parent] hello!\n");
close(p1[1]);
close(p2[1]);
FD_ZERO(&initset);
FD_SET(p1[0], &initset);
FD_SET(p2[0], &initset);
newset = initset;
while (select(p2[0] + 1, &newset, NULL, NULL, NULL) > 0) {
if (FD_ISSET(p1[0], &newset))
if (read(p1[0], msg, MSGSIZE) > 0) printf("[parent] %s\n", msg);
if (FD_ISSET(p2[0], &newset))
if (read(p2[0], msg, MSGSIZE) > 0)
printf("[parent] %s\n", msg);
newset = initset;
}
} else if (pid1 == 0 && pid2 == 0) {
printf("[fork1] hello!\n");
close(p1[0]);
close(p2[0]);
close(p2[1]);
dup2(p1[1], 1);
execl("ex11-01c", "ex11-01c", (char *) 0);
} else if (pid1 > 0 && pid2 == 0) {
printf("[fork2] hello!\n");
close(p1[0]);
close(p1[1]);
close(p2[0]);
write(p2[1], "from fork2 via pipe", MSGSIZE);
mkfifo("./fifo", 0666);
filedes = open("./fifo", O_RDWR);
nread = read(filedes, msg, MSGSIZE);
printf("%s (%d)\n", msg, nread);
close(filedes);
unlink("./fifo");
} else exit(1);
}