-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe_sync.c
73 lines (54 loc) · 2.52 KB
/
pipe_sync.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
61
62
63
64
65
66
67
68
69
70
71
72
73
/*************************************************************************\
* Copyright (C) Michael Kerrisk, 2024. *
* *
* This program is free software. You may use, modify, and redistribute it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation, either version 3 or (at your option) any *
* later version. This program is distributed without any warranty. See *
* the file COPYING.gpl-v3 for details. *
\*************************************************************************/
/* Listing 44-3 */
#include "curr_time.h" /* Declaration of currTime() */
#include "tlpi_hdr.h"
int
main(int argc, char *argv[])
{
int pfd[2]; /* Process synchronization pipe */
int j, dummy;
if (argc < 2 || strcmp(argv[1], "--help") == 0)
usageErr("%s sleep-time...\n", argv[0]);
setbuf(stdout, NULL); /* Make stdout unbuffered, since we
terminate child with _exit() */
printf("%s Parent started\n", currTime("%T"));
if (pipe(pfd) == -1)
errExit("pipe");
for (j = 1; j < argc; j++) {
switch (fork()) {
case -1:
errExit("fork %d", j);
case 0: /* Child */
if (close(pfd[0]) == -1) /* Read end is unused */
errExit("close");
/* Child does some work, and lets parent know it's done */
sleep(getInt(argv[j], GN_NONNEG, "sleep-time"));
/* Simulate processing */
printf("%s Child %d (PID=%ld) closing pipe\n",
currTime("%T"), j, (long) getpid());
if (close(pfd[1]) == -1)
errExit("close");
/* Child now carries on to do other things... */
_exit(EXIT_SUCCESS);
default: /* Parent loops to create next child */
break;
}
}
/* Parent comes here; close write end of pipe so we can see EOF */
if (close(pfd[1]) == -1) /* Write end is unused */
errExit("close");
/* Parent may do other work, then synchronizes with children */
if (read(pfd[0], &dummy, 1) != 0)
fatal("parent didn't get EOF");
printf("%s Parent ready to go\n", currTime("%T"));
/* Parent can now carry on to do other things... */
exit(EXIT_SUCCESS);
}