Can't handle signals after changing process group

Hello guys! I have this little code:

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

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(SIGINT, &sa, NULL);

    setpgid(0, 0);

    printf("[LEADER]: PID: %d, PARENT: %d, PROCESS GROUP: %d\n", getpid(), 
    getppid(), getpgrp());

    while(1){
        printf("test");
        sleep(3);
    }

    return 0;
}

void signal_handler(int signum)
{
    switch (signum)
    {
    case SIGINT:
        printf("INT recived, exiting..\n");
        exit(EXIT_FAILURE);
    }
}


If i set the process group (line 13), signal like CTRL-C, CTRL-Z is not sent to the process.
But if i remove that line signal works perfectly, Do you know why?

P.S if i open another terminal and i manually send the signal, it works perfectly (kill -INT -pid)

EDIT: I noticed that without using make, to run the application, it works too.

Thank you in advice!
Last edited on
https://man7.org/linux/man-pages/man2/setpgid.2.html
Scroll down to the "Notes".

Try printing the result of this, before and after.
https://man7.org/linux/man-pages/man3/tcgetpgrp.3.html

In short, it seems you've managed to detach yourself from the controlling terminal, without the other usual side effect of putting the process into the background.

> P.S if i open another terminal and i manually send the signal, it works perfectly (kill -INT -pid)
Because it doesn't rely on the whole foreground / background / controlling terminal mess.

Topic archived. No new replies allowed.