The Linux Programming Interface

(nextflipdebug5) #1
Signals: Fundamental Concepts 397

z SIGIO is ignored by default on several UNIX implementations (particularly BSD
derivatives).
z Although not specified by any standards, SIGEMT appears on most UNIX imple-
mentations. However, this signal typically results in termination with a core
dump on other implementations.
z In SUSv1, the default action for SIGURG was specified as process termination,
and this is the default in some older UNIX implementations. SUSv2 adopted
the current specification (ignore).

20.3 Changing Signal Dispositions: signal().........................................................................


UNIX systems provide two ways of changing the disposition of a signal: signal() and
sigaction(). The signal() system call, which is described in this section, was the origi-
nal API for setting the disposition of a signal, and it provides a simpler interface
than sigaction(). On the other hand, sigaction() provides functionality that is not
available with signal(). Furthermore, there are variations in the behavior of signal()
across UNIX implementations (Section 22.7), which mean that it should never be
used for establishing signal handlers in portable programs. Because of these porta-
bility issues, sigaction() is the (strongly) preferred API for establishing a signal handler.
After we explain the use of sigaction() in Section 20.13, we’ll always employ that call
when establishing signal handlers in our example programs.

Although documented in section 2 of the Linux manual pages, signal() is actu-
ally implemented in glibc as a library function layered on top of the sigaction()
system call.

The function prototype for signal() requires some decoding. The first argument,
sig, identifies the signal whose disposition we wish to change. The second argu-
ment, handler, is the address of the function that should be called when this signal
is delivered. This function returns nothing (void) and takes one integer argument.
Thus, a signal handler has the following general form:

void
handler(int sig)
{
/* Code for the handler */
}

We describe the purpose of the sig argument to the handler function in Section 20.4.
The return value of signal() is the previous disposition of the signal. Like the
handler argument, this is a pointer to a function returning nothing and taking one
integer argument. In other words, we could write code such as the following to

#include <signal.h>

void ( *signal(int sig, void (*handler)(int)) ) (int);
Returns previous signal disposition on success, or SIG_ERR on error
Free download pdf