Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

330 Signals Chapter 10


Be awarethat UNIX systems from other vendors can have values different from those
shown in this figure. For example, sigaction under SunOS 4.1.2 restarts an
interrupted system call by default, unlike the platforms listed in Figure10.3.
In Figure10.18, we provide our own version of the signal function that
automatically tries to restart interrupted system calls (other than for theSIGALRM
signal). In Figure10.19, we provide another function,signal_intr,that tries to never
do the restart.
We talk moreabout interrupted system calls in Section 14.4 with regardtothe
selectandpollfunctions.

10.6 Reentrant Functions


When a signal that is being caught is handled by a process, the normal sequence of
instructions being executed by the process is temporarily interrupted by the signal
handler.The process then continues executing, but the instructions in the signal
handler arenow executed. If the signal handler returns (instead of callingexitor
longjmp,for example), then the normal sequence of instructions that the process was
executing when the signal was caught continues executing. (This is similar to what
happens when a hardwareinterrupt occurs.) But in the signal handler, we can’t tell
wherethe process was executing when the signal was caught. What if the process was
in the middle of allocating additional memory on its heap usingmalloc,and we call
mallocfrom the signal handler? Or,what if the process was in the middle of a call to a
function, such asgetpwnam(Section 6.2), that stores its result in a static location, and
we call the same function from the signal handler? In themallocexample, havoc can
result for the process, sincemallocusually maintains a linked list of all its allocated
areas, and it may have been in the middle of changing this list. In the case of
getpwnam,the information returned to the normal caller can get overwritten with the
information returned to the signal handler.
The Single UNIX Specification specifies the functions that areguaranteed to be safe
to call from within a signal handler.These functions arereentrant and arecalled
async-signal safeby the Single UNIX Specification. Besides being reentrant, they block
any signals during operation if delivery of a signal might cause inconsistencies.
Figure10.4 lists these async-signal safe functions. Most of the functions that arenot
included in Figure10.4 aremissing because (a) they areknown to use static data
structures, (b) they callmallocorfree, or (c) they arepart of the standardI/O library.
Most implementations of the standardI/O library use global data structures in a
nonreentrant way.Note that even though we callprintffrom signal handlers in some
of our examples, it is not guaranteed to produce the expected results, since the signal
handler can interrupt a call toprintffrom our main program.
Be awarethat even if we call a function listed in Figure10.4 from a signal handler,
there is only oneerrnovariable per thread (recall the discussion oferrnoand threads
in Section 1.7), and we might potentially modify its value. Consider a signal handler
that is invoked right aftermainhas seterrno.Ifthe signal handler callsread,for
example, this call can change the value oferrno,wiping out the value that was just
Free download pdf