Orphaned process doesn't receive SIGHUP

Hello!
I have this simple fork code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main(int argc, char const *argv[])
{
    pid_t p = fork();
    if (p == -1)
    {
        printf("error in fork leader");
        exit(EXIT_FAILURE);
    }
    else if (p == 0)
    {
        execlp("./child", "child", NULL);
    }

    sleep(3); //sleep and then it dies.

    return 0;
}


Child:

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

struct sigaction sa;

void signal_handler(int signum);

int main(int argc, char const *argv[])
{
    struct sigaction sa;
    bzero(&sa, sizeof(struct sigaction));
    sa.sa_handler = signal_handler;
    sigaction(SIGCONT, &sa, NULL);
    sigaction(SIGHUP, &sa, NULL);

    while (1)
    {
        printf("test\n");
        sleep(2);
    }

    return 0;
}

void signal_handler(int signum)
{
    switch (signum)
    {
    case SIGHUP:
        kill(getpid(), SIGKILL);
        break;
    }
}


According to the standard POSIX:


If termination of a process causes a process group to become orphaned, and some member is stopped, then all are sent first SIGHUP and then SIGCONT.


In my case, the parent is the process group leader, and the child is a process in the process group. At the time that the parent dies, the child become orphan (beacuse the process group leader dies) but, apparently, the signal is not sent to the child (i want the child to die when it receives the SIGHUP as you can see in the code)

is the signal sent only to stopped processes? If yes, can i still notify the child in other way?
Last edited on
https://linux.die.net/man/2/getppid
Change this line.

 
printf("test, current parent = %d\n", getppid());


When the parent dies, the child is adopted by the shell.
You should see that in a change of what it thinks the parent process is.

It's the shell that is your process group leader.

Topic archived. No new replies allowed.