So, I am trying to have a program kill a child process once control-C is called. I am trying to do it with control-c with this part of my code.:
void signalHandler(int sig_num)
{
kill(pidKillTracker, SIGINT);
//signal(SIGINT, signalHandler);
printf("\nChild PID is terminated: %d\n", pidKillTracker);
fflush(stdout);
}
int main(int argc, char ** argv)
{
signal(SIGINT, signalHandler);
//......
Here is the output I show:
:sleep 20
28345^C
Child PID is terminated: 28345
:sleep 20
28383^C^C^C^C^C:
The number before ^C is where I print out the child process ID (I fflush after, so its not that). I do it to prove that the signal is killing the child ID correctly.
As you can see, when I call CTRL-C the first time, it works fine and terminates and prints the child process terminated.
YET, the second time I do it, the process won't terminate no matter how many times I hit CTRL-C.
I can't find anything wrong with this code. Also, I am making sure that pidKillTracker is set to the current child process everytime in the parent foreground, right before waitpid(). It is set equal to what comes out of fork() and is the child process. So it should be calling the current child process ID number every time.
Any ideas as to why this won't work a second time?
I have read that it may be suggested that I use sigaction, yet I really am unclear how to use that with what I am doing, not familiar at all with it, even after reading about it. I fail to understand how to use it.
Could someone point me in the right direction to make CTRL-C work more than once?