-
Notifications
You must be signed in to change notification settings - Fork 307
/
10.3.c
47 lines (37 loc) · 765 Bytes
/
10.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
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
static void sig_cld(int);
int main(void)
{
pid_t pid;
if (signal(SIGCLD, sig_cld) == SIG_ERR) {
printf("signal error\n");
exit(-1);
}
if ((pid = fork()) < 0) {
printf("fork error\n");
exit(-1);
} else if (0 == pid) {
sleep(2);
_exit(0);
}
pause();
exit(0);
}
static void sig_cld(int signo)
{
pid_t pid;
int status;
printf("SIGCLD received\n");
if (signal(SIGCLD, sig_cld) == SIG_ERR) {
printf("signal error\n");
exit(-1);
}
if ((pid = wait(&status)) < 0) {
printf("wait error\n");
exit(-1);
}
printf("pid = %d\n", pid);
}