0

I'm writing a program where I need to catch SIGINT signal so that I can kill child processes without the parent process exiting. I can kill child processes by sending them SIGKILL.

But after a signal is handled the main program resumes from where it left off. I want it to resume from somewhere else. For example say I have a loop and a signal is caught somewhere within loop; after handling the signal I want the program to resume from the beginning of the loop, rather than the middle.

Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
  • I your example when you resume from handling the signal you can use "continue" and initialize your loop condition. i.e. for while (i – Gaston Feb 15 '15 at 15:28

2 Answers2

2

It lies in the nature of asynchronous signals that they can interupt the code at any time.

If there are sections in the code where such an interuption is unwanted then disable signals for those parts.

In all other sections do relevant checks to determine whether an interuption occured and make the code react accordingly.

alk
  • 69,737
  • 10
  • 105
  • 255
-1

It's really bad form to catch SIGINT and that do anything other than terminate. If you want to intercept the signal so you can kill children, then you should do an and exit -- do NOT continuing your program normally.

In your signal handler, set a global flag that indicates SIGINT was received, then from within your loop, test this variable, and execute conditional branch logic.

user590028
  • 11,364
  • 3
  • 40
  • 57