Signals-I
When a signal is received the signal handler code gets executed.
"kill -l" lists all the available signals on a system.
Here is some sample code that sends a SIGSTOP to itself.
main()
{
pid_t my_pid = getpid();
kill(my_pid, SIGSTOP);
}
Catching Signals - Signal Handlers
signal(SIGINT, print_hello);
With signal fucntion we can define a signal handler for a given signal.In the example above
We have defined print_hello to be the handler for SIGINT(Ctrl-C).
Masking signals:
If we re-enter intoa signal while a signal is being processed ,things can get nasty.
sigprocmask allows us to mask signals in a global context.
This function lets us either block/unblock new signals or completely set a new mask.
To mask any signals while we are in a signal handler,following piece of code could be used.
sigset_t mask_set; /* used to set a signal masking set. */
sigset_t old_set; /* used to store the old mask set. */
/* re-set the signal handler again to catch_int, for next time */
signal(SIGINT, catch_int);
/* mask any further signals while we're inside the handler. */
sigfillset(&mask_set);
sigprocmask(SIG_SETMASK, &mask_set, &old_set);
There is no need to re-set the signal mask later on as the operating system will automatically reset the mask during the return from the signal handler.
