how to kill a child process on sending an interrupt with key stroke and then sending a signal to parent that the child process has been killed?
The parent process generally receives a signal whenever a child dies regardless. This is the SIGCHLD signal.
This signal can be blocked by the parent process, but otherwise it's always delivered when a child exits for whatever reason. The parent can tell why the child exited by using one of the wait
family (aka wait
, wait3
, wait4
, or waitpid
) to harvest the return code.
It may be a challenge to get the child process to have focus, so that it gets keyboard events.
You can always use the kill
command to send a signal to a given process. Don't let the name kill mislead you, you can send any signal with kill, kill -SIGNUM pid
, but SIGTERM is the default, which will usually cause a process to exit.
So if you wanted to send a SEGV signal to process 11 you'd do
kill -SEGV 11
You could set up the parent to catch a signal, re-send it to the child via a call to kill(2), and then wait for the child to exit using waitpid.
The parent's signal handler would like something like so:
int status;
kill(child_pid, SIG_TO_SEND);/*send to child*/
waitpid(child_pid, &status);/* wait for child to give up */
If you were serious about keeping the parent up and knew the signal was only for the child you'd probably set a variable in the parent, get out of the signal handler, and then do the work of sending and waiting for the child. In general it's good to get out of signal handlers as quickly as you can.