Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if during a signal handling in UNIX, the same signal gets sent to the program?

Tags:

c

unix

pipe

signals

Any ideas on this? Is there some kind of a signal queue, or does it get dropped?

While we are at this question, is it true that signal handlers should do as minimal work as possible?

I read somewhere that a signal handler should use a pipe and just write one byte to it, indicating what the program should do. Then somewhere else the program periodically checks the pipe, and dispatches based on byte in it. (I might have misunderstood it)

Thanks, Boda Cydo.

like image 546
bodacydo Avatar asked Jun 27 '10 14:06

bodacydo


People also ask

What happens when a signal is sent to a process that is already handling a signal?

24.4. 4 Signals Arriving While a Handler Runs When the handler for a particular signal is invoked, that signal is automatically blocked until the handler returns. That means that if two signals of the same kind arrive close together, the second one will be held until the first has been handled.

What happens if multiple signals of the same type are delivered to a process before it has a chance to process them?

If multiple signals of the same type are delivered to your process before your signal handler has a chance to be invoked at all, the handler may only be invoked once, as if only a single signal had arrived. In effect, the signals merge into one.

What happens when a UNIX process receives a signal?

When a process receives a signal, a default action happens, unless the process has arranged to handle the signal. For the list of signals and the corresponding default actions, see signal(7).

What happens when a process receives a signal while it's waiting?

If a child process receives a signal, a waiting parent will then continue execution leaving an orphan process behind. Hence it is sometimes needed to check the argument set by wait, waitpid or waitid and, in the case that WIFSIGNALED is true, wait for the child process again to deallocate resources.


1 Answers

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.

like image 105
Tyler McHenry Avatar answered Oct 08 '22 16:10

Tyler McHenry