PreviousNext

Asynchronous Signal Handling

Applications should handle asynchronous signals by having one thread (or possibly a few specific threads) call sigwait( ). The waited-for signals must be blocked before waiting. The recommended procedure is to establish a "signal catcher" thread that calls sigprocmask( ) to establish the per-process mask for asynchronous signals and then calls sigwait( ) to wait for the set of blocked signals. The following code fragment shows an example of a signal catcher thread start routine:

/*
* This is run by the signal catcher thread to handle async signals.
* We don't use sigaction() here because it won't work with
* async signals. Note that signals must be blocked prior to being
* waited for.
*/

void signal_catcher(char *arg)
{
sigset_t signals;
int sig;

sigemptyset(&signals);

/* In this sample, we'll catch only SIGINT... */

sigaddset(&signals, SIGINT);
sigprocmask(SIG_BLOCK, &signals, NULL);*/
while(1)
{
sig = sigwait(&signals);
switch(sig)
{
case SIGINT:

/* SIGINT specific actions here. */
.
.
.
break;
default:
/* Not reached. If we were waiting on other */
/* signals. this would establish a default action */
/* to exit ... */
continue;
}
break;
}
sigprocmask(SIG_UNBLOCK, &signals, NULL);

/* Do termination clean up here. */
.
.
.
exit(1);
}