Signals – Handling Signal While SIGSTOP is Active

signals

What happens if a process gets a signal after it was stopped by SIGSTOP?

I am trying to understand and get a good picture of how this is handled. Unfortunately, all I could find was a short description of the signal but no details of particular use-cases.

As of now, my understanding is that while SIGSTOP is in effect:

  • SIGKILL will kill the process immediately
  • other signals will be gathered up, and on a SIGCONT, the signals on hold will be unleashed on the process, as if all of them were sent at that moment
  • while handling the bunched-up signals after a SIGCONT, sa_mask, sa_flags's SA_RESTART, etc. will be applied, as per normal

E.g., If there is a SIGSTOP -> SIGHUP -> SIGCONT sequence, the handling of SIGHUP will be put on hold until after SIGCONT is received.

I am not sure if that is correct.

I am still in the dark about these cases:

  1. I am still confused about the order of the bunched-up signals.

    E.g.: SIGSTOP -> SIGHUP -> SIGTERM -> SIGCONT: will SIGHUP and SIGTERM be handled in the order they are received, or in an arbitrary order?

  2. What happens if the same signal is received multiple times while SIGSTOP is in effect?

    SIGSTOP -> SIGHUP -> SIGHUP -> SIGCONT

  3. Will SIGSTOP interrupt a running signal handler? If signals were waiting to be handled, are they preserved until after a SIGCONT?


I have found some answers in the answers to these similar questions, but I am still unclear on the above things.

Best Answer

SIGSTOP isn't something that is "in effect". The signal is delivered, and then the process is in a stopped state. That state is what is in effect. If some signals are generated against the process in that state, then they simply become pending. Signals cannot be delivered to a stopped process. When the process runs again, they get processed according to the signal process mask, and their disposition: ignored signals ignored.

I'm not sure about the order being specified. Only real-time signals have reliable queuing properties. For instance POSIX contains wording like "If there are any pending unblocked signals after the call to sigprocmask(), at least one of those signals shall be delivered before the call to sigprocmask() returns." If, say, a SIGINT and SIGTSTP are both pending, it doesn't specify which is delivered first.

Of course there are special behaviors: being stopped by SIGSTOP doesn't prevent being killed by SIGKILL.

Related Question