Proper handling of SIGINT/SIGQUIT (2013)
cons.org
cons.org
In UNIX terminal sessions, you usually have a key like C-c (Control-C) to immediately end whatever program you have running in the foreground. This should work even when the program you called has called other programs in turn. Everything should be aborted, giving you your command prompt back, no matter how deep the call stack is.
Basically, it’s trivial. But the existence of interactive applications that use SIGINT and/or SIGQUIT for other purposes than a complete immediate abort make matters complicated, and – as was to expect – left us with several ways to solve the problems. Of course, existing shells and applications follow different ways.
This web page outlines different ways to solve the problem and argues that only one of them can do everything right, although it means that we have to fix some existing software.
(1) The child terminates normally, never having received SIGINT.
(2) The parent receives SIGINT before it reaches the wait() call, or while in the wait() call but before the child termination is picked up (I suppose in the later case wait() might fail and be restarted).
(3) Once the parent sees that the child terminated normally, it does not exit, and SIGINT was effectively ignored.
Then, after waiting on the child, "Look at whether you (the shell) has received SIGINT while the child was in the foreground", and the shell kills itself with SIGINT if so.
It would be great if consistent interruption could be solved once and for all. In many cases, the presence of sub-processes is essentially an "implementation detail" of the program you're trying to interrupt but you just don't know if the Control-C will work or if the shell will just sit there and hang. And many people are not aware of what shells can do either so they try even more extreme things like closing entire terminal windows to cope.
Sorry, I know this is off-topic shop talk, but we do these all the time and this one is unusual in its non-obviousness. I'll try to resist posting more about it.
The idea that a web page has a definite year it was written in is a very blog-centric (or should I say a newspaper-centric) view. Web pages can be more than that.