summaryrefslogtreecommitdiff
path: root/test/signalbychild.c
blob: 462daf4121a9e1dccae4124ef51beba093b0c44e (plain)
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

#include <signal.h>

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

int volatile g_signal_need_exit = 0;
int saved_ppid = 0;

void signal_notify(int signum)
{
        if (saved_ppid == getpid()) {
                
                printf("parent got signal %d\n", signum);
        }

        g_signal_need_exit = 1;
        
}

static void install_signal(void)
{
        struct sigaction sa;

        sa.sa_handler = signal_notify;

        if (sigaction(SIGINT, &sa, NULL) == -1) {
                perror("sigaction");
        }
        
}

static void enter_eventloop(void)
{
        while(!g_signal_need_exit) {
                
        }
        printf("thread exit\n");

        _exit(0);
        
}

int main(void)
{
        saved_ppid = getpid();
        install_signal();

        for(int i = 0; i < 5; i++) {
                if (fork() == 0) {
                        enter_eventloop();
                        
                        /* raise(SIGUSR1); */
                        
                }
        }

        while(wait(NULL) > 0);
}