I am trying to understand how CTRL+C terminates a child but not a parent process. I see this behavior in some script shells like bash
where you can start some long-running process and then terminate it by entering CTRL-C and the control returns to the shell.
Could you explain how does it work and in particular why isn't the parent (shell) process terminated?
Does the shell have to do some special handling of CTRL+C event and if yes what exactly does it do?
Turned out the way Ctrl-c works is quite simple — it's just a shortcut key for sending the interrupt (terminate) signal SIGINT to the current process running in the foreground. Once the process gets that signal, it's terminating itself and returns the user to the shell prompt.
For killing a child process after a given timeout, we can use the timeout command. It runs the command passed to it and kills it with the SIGTERM signal after the given timeout. In case we want to send a different signal like SIGINT to the process, we can use the –signal flag.
The parent process has to acknowledge the termination of the child process by using the wait system call and what this does is it checks the termination status of the child process.
While in a command line such as MS-DOS, Linux, and Unix, Ctrl + C is used to send a SIGINT signal, which cancels or terminates the currently-running program. For example, if a script or program is frozen or stuck in an infinite loop, pressing Ctrl + C cancels that command and returns you to the command line.
Signals by default are handled by the kernel. Old Unix systems had 15 signals; now they have more. You can check </usr/include/signal.h>
(or kill -l). CTRL+C is the signal with name SIGINT
.
The default action for handling each signal is defined in the kernel too, and usually it terminates the process that received the signal.
All signals (but SIGKILL
) can be handled by program.
And this is what the shell does:
find
, the shell: fork
s itselfYou can trap
signals in your shell script too...
And you can set signal handling for your interactive shell too, try enter this at the top of you ~/.profile
. (Ensure than you're a already logged in and test it with another terminal - you can lock out yourself)
trap 'echo "Dont do this"' 2
Now, every time you press CTRL+C in your shell, it will print a message. Don't forget to remove the line!
If interested, you can check the plain old /bin/sh
signal handling in the source code here.
At the above there were some misinformations in the comments (now deleted), so if someone interested here is a very nice link - how the signal handling works.
First, read the Wikipedia article on the POSIX terminal interface all of the way through.
The SIGINT
signal is generated by the terminal line discipline, and broadcast to all processes in the terminal's foreground process group. Your shell has already created a new process group for the command (or command pipeline) that you ran, and told the terminal that that process group is its (the terminal's) foreground process group. Every concurrent command pipeline has its own process group, and the foreground command pipeline is the one with the process group that the shell has programmed into the terminal as the terminal's foreground process group. Switching "jobs" between foreground and background is (some details aside) a matter of the shell telling the terminal which process group is now the foreground one.
The shell process itself is in yet another process group all of its own and so doesn't receive the signal when one of those process groups is in the foreground. It's that simple.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With