Home > Software design >  Understanding signal blocking and signal suspension
Understanding signal blocking and signal suspension

Time:03-20

After reading the documentation I still don't really understand signal blocking. If you have a mask that blocks a given signal, do you need to unblock that signal first to allow the program to intercept it or does signal blocking behave in a different way?

If you use sigsuspend, does your program get suspended until a given signal from the mask you pass as an argument arrives?

Should the signal you wait for when using sigsuspend be unblocked or is it not necessary?

Btw i am using C and the pthread library to write my programs

CodePudding user response:

If you have a mask that blocks a given signal, do you need to unblock that signal first to allow the program to intercept it or does signal blocking behave in a different way?

A signal is never delivered to any thread that has it blocked. If a signal is raised for a process while all threads of that process have it blocked, then it remains pending until it is unblocked for at least one thread, or the process terminates.

If you use sigsuspend, does your program get suspended until a given signal from the mask you pass as an argument arrives?

No. The signal mask you pass to sigsuspend has the same meaning as the one you pass to (for example) sigprocmask(): it specifies a complete set of the signals that should be blocked. This mask must not include any signals you want the thread to be able to receive. Often, it is appropriate to pass sigsuspend() the mask that was in effect prior to a preceding sigprocmask() call, which the latter function will have provided to you if you asked for it.

Should the signal you wait for when using sigsuspend be unblocked or is it not necessary

At all times, you should ensure that any signal you want a thread to be able to receive is unblocked, and conversely, that any signal the thread must not receive is blocked. This is why sigsuspend() gives you a way to specify a different signal mask to be in effect for the duration of the call.

  • Related