Info

typedef void _signalfn_t(int);

typedef _signalfn_t _user *_sighandler_t;

The parameter accepts the number of the signal received so that the same handler function can be installed for different signals.11

The signal handler is installed using the sigaction system call, which (in our example) replaces the default handler for sigterm with the user-defined handler function.

Processes can set a global mask to specify which signals are to be blocked while the handler is running. A bit chain is used to indicate that a signal is either blocked (bit value 1) or not blocked (bit value 0). The sample program sets all bit positions to 0 so that all signals sent to the process from the outside can be received while the handler is running.

The last step in the program is to wait for a signal using the sigsuspend system call. The process is placed in the blocked state (see Chapter 2) and sleeps until woken by the arrival of a signal; it is then immediately put to sleep again (by the while loop). The main code need not concern itself with signal handling because this is done automatically by the kernel in conjunction with the handler function. The approach shown is a good example of how to avoid the deprecated practice of busy waiting.12

If the sigterm signal is sent to the process using kill, the process is not terminated as it normally would be; instead, it outputs the number of the received signal (15) and continues to run because, as desired, the signal was forwarded to the user-defined handler routine and not to the default implementation of the kernel.

Continue reading here: Info

Was this article helpful?

0 0