Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 10.5 Interrupted System Calls 329


To prevent applications from having to handle interrupted system calls, 4.2BSD
introduced the automatic restarting of certain interrupted system calls. The system calls
that wereautomatically restarted areioctl,read,readv,write,writev,wait,and
waitpid.Aswe’ve mentioned, the first five of these functions areinterrupted by a
signal only if they areoperating on a slow device;waitandwaitpidarealways
interrupted when a signal is caught. Since this caused a problem for some applications
that didn’t want the operation restarted if it was interrupted, 4.3BSD allowed the
process to disable this feature on a per-signal basis.

POSIX.1 requires an implementation to restart system calls only when theSA_RESTARTflag is
in effect for the interrupting signal. As we will see in Section 10.14, this flag is used with the
sigactionfunction to allow applications to request that interrupted system calls be
restarted.

Historically,when using thesignalfunction to establish a signal handler,implementations
varied with respect to how interrupted system calls werehandled. SystemVnever restarted
system calls by default. BSD, in contrast, restarted them if the calls wereinterrupted by
signals. On FreeBSD 8.0, Linux 3.2.0, and Mac OS X 10.6.8, when signal handlers areinstalled
with thesignalfunction, interrupted system calls will be restarted. The default on Solaris 10,
however, is to return an error (EINTR)instead when system calls areinterrupted by signal
handlers installed with thesignalfunction. By using our own implementation of the
signalfunction (shown in Figure10.18), we avoid having to deal with these differences.

One of the reasons 4.2BSD introduced the automatic restart feature is that
sometimes we don’t know that the input or output device is a slow device. If the
program we write can be used interactively,then it might be reading or writing a slow
device, since terminals fall into this category.If we catch signals in this program, and if
the system doesn’t provide the restart capability,then we have to test every read or
write for the interrupted error return and reissue the read or write.
Figure10.3 summarizes the signal functions and their semantics provided by the
various implementations.

Automatic restart
of interrupted
system calls?

Functions System remains installedSignal handler block signalsAbility to

ISO C, POSIX.1 unspecified unspecified unspecified
V7, SVR2, SVR3 never
SVR4, Solaris never
4.2BSD • •always
4.3BSD, 4.4BSD,
FreeBSD, Linux,

signal

Mac OS X


  • • default


POSIX.1, 4.4BSD,
SVR4, FreeBSD,
Linux, Mac OS X,
Solaris

sigaction • • optional

Figure 10.3 Features provided by various signal implementations
Free download pdf