2

I have been trying to prevent my parent thread from killing the child proccess if the parent proccess recives a ctrl-C signal. I have run out of ideas.

The parent proccess allready catches SIGINT and so i want the same as now the children dies.

  int main() {
       pid_t pid;

       pid = fork();

       if(pid > 0) {
          /*Parent proccess*/
          [...other code...]
       } else {
          /*Child proccess*/

           signal(SIGINT, SIG_IGN);     //does not work
           sigaction(SIGINT, &action, 0);  //does not work either
           [...other code...]
       }

  }

Ideas?

user1501127
  • 865
  • 1
  • 18
  • 32
  • What is the goal to hold the child alive if the parent process is dead? – Leo Chapiro Dec 16 '15 at 12:05
  • 1
    What signal is the child process getting to kill it? – Andrew Henle Dec 16 '15 at 12:05
  • 1
    Possible duplicate of [Linux daemonize](http://stackoverflow.com/questions/3095566/linux-daemonize) – ecatmur Dec 16 '15 at 12:09
  • There are no threads here, only processes. – ams Dec 16 '15 at 12:14
  • Why does the child gets signalled at all when the parent ends? – alk Dec 16 '15 at 12:18
  • 1
    It usually doesn't, but ctrl+c sends sigint to the whole foreground process-group, not just to the parent process. The OP could try to use setsid() instead of trying to catch the signal. – Ctx Dec 16 '15 at 12:19
  • The parent proccess allready catches SIGINT and so i want the same as now the children dies. – user1501127 Dec 16 '15 at 12:29
  • 1
    do you by chance exec*() after the fork()? This will reset all signal handlers to default – Ctx Dec 16 '15 at 12:31
  • Yes! ahh so thats it then, i do execvp(). But should not the sig* in parent/child code work for the new proccess? – user1501127 Dec 16 '15 at 12:53
  • 1
    File handles, env vars, and process groups are kernel properties. Signal handers are user-space properties. Nothing user-space survives exec. – ams Dec 16 '15 at 13:14

1 Answers1

2

SIGINT is sent to the entire process group. Try using setpgrp(); in the child process to put it into a different process group.

Duane McCully
  • 406
  • 3
  • 6