-
Notifications
You must be signed in to change notification settings - Fork 0
/
04-pipe.c
48 lines (42 loc) · 937 Bytes
/
04-pipe.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
int p;
int f;
int pipefds[2];
char *pin;
char buffer[5];
p = pipe(pipefds);
if (p == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
f = fork();
if (f == 0) {
close(pipefds[0]);
pin = "1248\0";
write(pipefds[1], pin, 5);
printf("Intentional Delay...\n");
sleep(2);
pin = "8421\0";
for (int i=0; i!=5; ++i) {
sleep(1);
write(pipefds[1], pin, 5);
}
exit(EXIT_SUCCESS);
} else {
int r;
int count = 0;
close(pipefds[1]);
do {
r = read(pipefds[0], buffer, 5);
if (r)
printf("Received PIN %d: %s\n", count++, buffer);
} while (r);
wait(NULL);
close(pipefds[0]);
}
return EXIT_SUCCESS;
}