What happens when a signal is received already in the signal handler?

I have a parent process spanning multiple child processes. I want to know when any child process completes the registration of the SIGCHLD signal handler.

The question is, what happens if another SIGCHLD (or any other signal) is received while the parent process is already in the signal handler?

I can think of the following results:

  • Signal is ignored
  • The signal is queued and will be processed as soon as the current handler returns
  • The current handler is interrupted, as is the main program

Which one is correct?

+4
source share
1 answer

In your specific example (the same signal is received), the signal is delivered after the signal handler has finished processing (so that point number 2 is correct). Please note, however, that you may “lose” signals.

The reason for this is that although the signal is inside its handler, it is blocked. Blocked signals are set on hold, but not in the queue. The term “waiting” means that the operating system remembers that there is a signal waiting to be delivered at the next opportunity, and “not queued” means that it does this by setting the flag somewhere, but not keeping an accurate record of how it appeared a lot of signals.

That way, you can get 2 or 3 (or 10) more SIGCHLD while in your handler, but you only see one (so in some cases the number 1 mark may also be correct).

Please note that several flags that you can pass to sigaction can influence the default behavior, for example, SA_NODEFER (prevents a blocking signal) and SA_NOCLDWAIT (may not generate a signal at all on some systems).

Now, of course, if you receive a different type of signal, there is no guarantee that it will not interrupt your handler. For this reason, one prefers not to use safe functions without a signal.

+3
source

Source: https://habr.com/ru/post/1498932/


All Articles