Reputation: 30765
What I want to do is to only handle signals at some known points in my code. So at those points I will check for a pending signal and execute its handler immediately.
I know I can use sigprocmask
to block a signal and later unblock it, but sill in that way, the signal will be delivered at any point in time after unblocking, which I don't want. I want to execute the signal handler precisely at a specific location in my code. In other words, I want to make signal handling synchronous.
Is there a possible way to achieve what I'm trying to do?
Upvotes: 1
Views: 2574
Reputation: 6877
You can fetch any pending blocked signal using the sigwait
family of syscalls. It's not possible to wait for both signals and other types of events concurrently though, if you do not want to block in sigwait you can first check if there's pending signals using sigpending
.
If you're using an event driven design for your application you'll typically have several fds in a run loop. For that situation you should instead use a signal handler which writes to a pipe
. The signal will be delivered asynchronously but will then appear in your run loop as just another fd that needs processing.
Upvotes: 3
Reputation: 791759
If I understand you correctly you can simply use sigwait
to clear each of the pending signals and then call the your signal handler function directly.
At the points in your code where you want to handle and clear pending signals you can use sigpending
to check if there are signals waiting and then repeatedly call sigwait
to clear the pending signals, manually calling a signal handling function to perform the required action.
Upvotes: 1
Reputation: 93710
Any pending signals will be delivered immediately when you unmask them. Just unmask/remask at the point where you want to let them be delivered. If you are using threads be aware that asynchronous signals (like those you get from kill
) can go to any thread that has them unblocked.
Upvotes: 0