Search code examples
signalssigprocmask

Delivery of Signal after sigprocmask


I'm under the impression that using sigprocmask() to unblock a signal will cause a pending blocked signal to be delivered to process.

In a specific case where sigprocmask() is used to unblock signals, if there is a pending signal (say SIGUSR1) that is already unblocked, will the signal SIGUSR1 be delivered to the process with a call to sigprocmask() that unblocks a different signal while keeping SIGUSR1 unblocked?


Solution

  • To understand it thoroughly we need to understand how signal are generated and delivered in linux.

    When kernel receive any signal request it sets signal pending flag for the process, provided signal is not blocked. Now before returning to user mode kernel checks whether there are nonblocked pending signals are present for process or not. If yes, then kernel prefers to deliver that signal before returning to user mode. Now coming to your question:

    if there is a pending signal (say SIGUSR1) that is already unblocked,

    I am assuming that signal(SIGUSR1) is first blocked and later unblocked. In this case, when user made an attempt to unblock signal then signal will be delivered before user process resumes in user mode. For ex. if sigprocmask() is used to unblock signal then signal will be delivered even before sigprocmask() returns.

    will the signal SIGUSR1 be delivered to the process with a call to sigprocmask() that unblocks a different signal while keeping SIGUSR1 unblocked?

    If multiple pending signals are unblocked and ready to deliver then kernel picks the signal which has lowest signal number( of course, synchronous signals have higher priorities over asynchronous signal) to deliver first.