Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 10.3 signalFunction 323


10.3 signalFunction


The simplest interface to the signal features of the UNIX System is thesignalfunction.
#include <signal.h>
void (*signal(intsigno,void (*func)(int)))(int);
Returns: previous disposition of signal (see following) if OK,SIG_ERRon error

Thesignalfunction is defined by ISO C, which doesn’t involve multiple processes, process
groups, terminal I/O, and the like. Therefore, its definition of signals is vague enough to be
almost useless for UNIX systems.
Implementations derived from UNIX System V support thesignalfunction, but it provides
the old unreliable-signal semantics. (Wedescribe these older semantics in Section 10.4.) The
signalfunction provides backwardcompatibility for applications that requirethe older
semantics. New applications should not use these unreliable signals.
4.4BSD also provides thesignalfunction, but it is defined in terms of thesigaction
function (which we describe in Section 10.14), so using it under 4.4BSD provides the newer
reliable-signal semantics. Most current systems follow this strategy,but Solaris 10 follows the
System V semantics for thesignalfunction.
Because the semantics ofsignaldiffer among implementations, we must use thesigaction
function instead. We provide an implementation ofsignalthat usessigaction in
Section 10.14. All the examples in this text use thesignalfunction from Figure10.18 to give
us consistent semantics regardless of which particular platform we use.

Thesignoargument is just the name of the signal from Figure10.1. The value of
funcis (a) the constantSIG_IGN,(b) the constantSIG_DFL, or (c) the address of a
function to be called when the signal occurs. If we specifySIG_IGN, we are telling the
system to ignorethe signal. (Remember that we cannot ignorethe two signalsSIGKILL
andSIGSTOP.) When we specifySIG_DFL, we are setting the action associated with
the signal to its default value (see the final column in Figure10.1). When we specify the
address of a function to be called when the signal occurs, we arearranging to ‘‘catch’’
the signal.We call the function either thesignal handleror thesignal-catching function.
The prototype for thesignal function states that the function requires two
arguments and returns a pointer to a function that returns nothing (void). Thesignal
function’s first argument,signo, is an integer.The second argument is a pointer to a
function that takes a single integer argument and returns nothing. The function whose
address is returned as the value ofsignaltakes a single integer argument (the final
(int)). In plain English, this declaration says that the signal handler is passed a single
integer argument (the signal number) and that it returns nothing. When we call
signalto establish the signal handler,the second argument is a pointer to the function.
The return value fromsignalis the pointer to the previous signal handler.
Many systems call the signal handler with additional, implementation-dependent arguments.
We discuss this further in Section 10.14.
The perplexingsignalfunction prototype shown at the beginning of this section
can be made much simpler through the use of the followingtypedef[Plauger 1992]:
typedef void Sigfunc(int);
Free download pdf