To answer the second part of your question, "is it true that signal handlers should do as minimal work as possible?" the answer is yes, because there is a very minimal set of functions that are "async signal safe" and therefore able to be called from signal handlers. Async signal safety is kind of an enhanced form of re-entrancy. If foo()
is async signal safe, that means that it's safe to call foo()
within a signal handler, even if foo()
was already executing when the signal was raised.
You can get the full list of async signal safe functions by looking that the section 7 man page for signal
(man 7 signal
). Calling any function other than one of these from within a signal handler, directly or indirectly, invokes undefined behavior.
The "write a byte to a pipe" approach is a great way to deal with signals without being restricted to async signal safe functions, especially if your program is already oriented around a select
loop.