-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy path3.c
96 lines (86 loc) · 2.29 KB
/
3.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <signal.h> // kill, sigaction
#include <stdio.h>
#include <stdlib.h> // exit
#include <unistd.h> // getppid, fork, pause, pipe
#include <getopt.h>
#define errExit(msg) \
do { \
perror(msg); \
exit(EXIT_FAILURE); \
} while (0)
// APUE 8.9, 10.16
// https://www.gnu.org/software/libc/manual/html_node/Sigsuspend.html
static sig_atomic_t sigflag = 0;
static void sig_handler() { sigflag = 1; }
static void wait_with_signal() {
printf("wait with signal\n");
struct sigaction act;
act.sa_handler = sig_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
if (sigaction(SIGUSR1, &act, NULL) == -1)
errExit("sigaction");
sigset_t cont_mask, old_mask;
sigemptyset(&cont_mask);
sigaddset(&cont_mask, SIGUSR1);
if (sigprocmask(SIG_BLOCK, &cont_mask, &old_mask) == -1)
errExit("sigprocmask");
pid_t cpid = fork();
if (cpid < 0)
errExit("fork");
else if (cpid == 0) {
printf("hello\n");
kill(getppid(), SIGUSR1);
} else {
while (sigflag == 0)
sigsuspend(&old_mask);
printf("goodbye\n");
}
}
// APUE 15.2
static void wait_with_pipe() {
printf("wait with pipe\n");
int pipefd[2];
if (pipe(pipefd) == -1)
errExit("pipe");
pid_t cpid = fork();
if (cpid < 0)
errExit("fork");
else if (cpid == 0) {
printf("hello\n");
close(pipefd[0]);
write(pipefd[1], "c", 1);
close(pipefd[1]);
} else {
close(pipefd[1]);
char c;
read(pipefd[0], &c, 1); // block
close(pipefd[0]);
printf("goodbye\n");
}
}
_Noreturn static void usage(char *name) {
fprintf(stderr, "Usage: %s [-s|--signal] [-p|--pipe]\n", name);
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[]) {
int opt;
struct option options[] = {
{"signal", no_argument, NULL, 's'},
{"pipe", no_argument, NULL, 'p'}
};
if ((opt = getopt_long(argc, argv, "sp", options, NULL)) != -1) {
switch (opt) {
case 's':
wait_with_signal();
break;
case 'p':
wait_with_pipe();
break;
default:
usage(argv[0]);
}
} else
usage(argv[0]);
return 0;
}