forked from vonzhou/CSAPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
procmask2.c
50 lines (41 loc) · 957 Bytes
/
procmask2.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
#include "csapp.h"
void initjobs()
{
}
void addjob(int pid)
{
}
void deletejob(int pid)
{
}
/* $begin procmask2 */
void handler(int sig)
{
pid_t pid;
while ((pid = waitpid(-1, NULL, 0)) > 0) /* Reap a zombie child */
deletejob(pid); /* Delete the child from the job list */
if (errno != ECHILD)
unix_error("waitpid error");
}
int main(int argc, char **argv)
{
int pid;
sigset_t mask;
Signal(SIGCHLD, handler);
initjobs(); /* Initialize the job list */
while (1) {
Sigemptyset(&mask);
Sigaddset(&mask, SIGCHLD);
Sigprocmask(SIG_BLOCK, &mask, NULL); /* Block SIGCHLD */
/* Child process */
if ((pid = Fork()) == 0) {
Sigprocmask(SIG_UNBLOCK, &mask, NULL); /* Unblock SIGCHLD */
Execve("/bin/date", argv, NULL);
}
/* Parent process */
addjob(pid); /* Add the child to the job list */
Sigprocmask(SIG_UNBLOCK, &mask, NULL); /* Unblock SIGCHLD */
}
exit(0);
}
/* $end procmask2 */