tags:

views:

227

answers:

1

According to the documentation:

There is no way to “block” signals temporarily from critical sections (since this is not supported by all Unix flavors).

What stops me using signal.signal(signum,SIG_IGN) to block it, then adding the signal back?

+8  A: 

What stops you is that, if the signal actually arrives while SIG_IGN is in place, then it will be ignored and thrown away. When you add the signal back later, it's too late because it's gone and you'll never get to learn that it happened.

Thus, you will have "ignored" (= thrown away) the signal rather than "blocked" it (= kept it for handling at the end of the critical section). Your confusion here might just arise from not knowing specifically what "blocking" a signal means: it means the OS hangs onto the signal, letting it wait to strike until your critical section is complete.

See (as a great reference for all sorts of questions like this) W. Richard Steven's Advanced Programming in the UNIX Environment. Section 10.8 in the edition I have, "Reliable Signal Terminology and Semantics", is the one I just checked before answering to be sure of my answer.

Update: on my Ubuntu laptop, "man sigprocmask" (if manpages-dev is installed) seems to be the man page to start with for learning about signal blocking. Again, as the Python docs note, this isn't available under all Unixes, so don't expect your old Irix or AIX box to run your Python program if you actually use "sigprocmask". But maybe you're not worried about that. :-)

Brandon Craig Rhodes
So I can't just create a signal handler to record that the signal was received, then check this variable as soon as I leave the critical section?
Casebash
Sure! In that case, (a) you've "handled" the exception, not "blocked" it, so the statement in the Python docs won't apply to what you're doing, and (b) it turns out that none of your code is actually a "critical section" since any of your code, I guess, can survive the momentary interruption of receiving a signal and setting a flag somewhere. In traditional async-speak, a "critical section" is a section that is *really* critical: that can't be interrupted even to set a flag.
Brandon Craig Rhodes
What would be that critical?
Casebash
Typically, the only domains requiring critical sections are things like controller code for video cards, communications gear, or embedded devices. That's why "sigprocmask()" was added to Unix fairly late, and why lots of older Unixes don't support it, because people often use entirely different operating systems if they have worries that would require a critical section. :-)
Brandon Craig Rhodes