0

I have a program which forks a child. I am trying to catch following signals: SIGINT, SIGPIPE and SIGTERM.

On Ctrl+c (which generates SIGINT - afaik) I want to make sure I kill the child process before main program terminates which I am doing in my signal handler.

Now my expectation/understanding is that parent process will be automatically be killed on Ctrl+c. But that is not happening.

On Ctrl+c I get the shell prompt back but I can still see my process in ps. So basically my main program is not getting killed.

Is my understanding wrong?

Edit 0: One observation: Before Ctrl+c, in ps main program status says S but after Ctrl+c its I.

Xyz
  • 5,955
  • 5
  • 40
  • 58
hari
  • 9,439
  • 27
  • 76
  • 110
  • 1
    After your signal handler kills the child process, it does call "exit", right? – selbie Aug 07 '11 at 08:36
  • For Ctrl+C, you likely do not need any special handling at all as this SIGINT is sent to the foreground process group of the terminal, which includes your child process unless you take measures to prevent it. – jilles Aug 07 '11 at 14:38
  • @jilles: oh is it? But I do not create create process group per say. Would all child processes be cleaned up without any special work after CTRL+C? – hari Aug 07 '11 at 16:48
  • 1
    Yes, although the shell will not wait for them (so if the program generates output when it gets SIGINT, you may want to wait for it anyway). The same applies to `kill` with `%` notation but not to `kill` with a pid. – jilles Aug 07 '11 at 16:59
  • @jilles: "if the program generates output when it gets SIGINT, you may want to wait for it anyway" - I am not getting this part. What do I want to wait for? For the program to finish generating output? I guess not. Can you please elaborate? – hari Aug 07 '11 at 18:27

1 Answers1

0

It gets killed after the signal handler code finishes running, in your case it won't actually die until the code you placed in the handler code for SIGINT executes (which should close the child process if you wrote it correctly).

It seems that maybe your handler code is doing something incorrectly that is not letting the process exit, please post your handler code and how you setup your handler as it is possible to do a SIGIGN (ignore) on SIGINT so that CTRL+C effectively does nothing to your program.

Make sure you call exit(0) or exit(1) or with an error code to tell the process to terminate if not the process will not exit.

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
  • Thanks Jesus. Actually it goes to signal handler and comes back and continue. – hari Aug 07 '11 at 08:36
  • 2
    Do you call exit(0) on successful child process kill? – Jesus Ramos Aug 07 '11 at 08:45
  • I just did :D and I realized how dumb I am. Thanks @selbie and Jesus. – hari Aug 07 '11 at 08:51
  • No problem, lot of people make this mistake :) – Jesus Ramos Aug 07 '11 at 08:51
  • 2
    Instead of `_exit()` (please not `exit()` as it is not async-signal-safe), it is better to reset the signal to default (`signal` or `sigaction`), unblock it (`sigprocmask`) and resend it to self (`raise`). This indicates to the parent process that you exited because of the signal. A shell may then abort the script or compound command rather than continuing in the assumption that the interrupt was handled internally. – jilles Aug 07 '11 at 17:05