-
Notifications
You must be signed in to change notification settings - Fork 3
/
app11-sigchld.C
62 lines (48 loc) · 1.02 KB
/
app11-sigchld.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
// test SIGCHLD handling
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
void SigchldHandler (int sig)
{
printf("signal %d\n", sig);
}
void Sigusr1Handler (int sig)
{
printf("Sigusr1Handler - signal %d\n", sig);
}
int main (int argc, char* argv[])
{
printf("main pid: %d\n", getpid());
signal(SIGCHLD, SigchldHandler);
signal(SIGUSR1, Sigusr1Handler);
int counter = 0;
while (true)
{
counter++;
printf("iteration #%d...\n", counter);
kill(getpid(), SIGUSR1);
const int pid = fork();
if (pid == 0)
{
// child process
sleep(1);
exit(0);
}
const int pid2 = fork();
if (pid2 == 0)
{
exit(0);
}
int waitRes;
do
{
int waitStat;
waitRes = waitpid(-1, &waitStat, WNOHANG);
} while (waitRes > 0);
sleep(2);
}
}