The Linux Programming Interface

(nextflipdebug5) #1

418 Chapter 20


20.14 Waiting for a Signal: pause()......................................................................................


Calling pause() suspends execution of the process until the call is interrupted by a
signal handler (or until an unhandled signal terminates the process).

When a signal is handled, pause() is interrupted and always returns –1 with errno set
to EINTR. (We say more about the EINTR error in Section 21.5.)
An example of the use of pause() is provided in Listing 20-2.
In Sections 22.9, 22.10, and 22.11, we look at various other ways that a pro-
gram can suspend execution while waiting for a signal.

20.15 Summary..................................................................................................................


A signal is a notification that some kind of event has occurred, and may be sent to a
process by the kernel, by another process, or by itself. There is a range of standard
signal types, each of which has a unique number and purpose.
Signal delivery is typically asynchronous, meaning that the point at which the
signal interrupts execution of the process is unpredictable. In some cases (e.g.,
hardware-generated signals), signals are delivered synchronously, meaning that
delivery occurs predictably and reproducibly at a certain point in the execution of a
program.
By default, a signal either is ignored, terminates a process (with or without a
core dump), stops a running process, or restarts a stopped process. The particular
default action depends on the signal type. Alternatively, a program can use signal()
or sigaction() to explicitly ignore a signal or to establish a programmer-defined signal
handler function that is invoked when the signal is delivered. For portability reasons,
establishing a signal handler is best performed using sigaction().
A process (with suitable permissions) can send a signal to another process
using kill(). Sending the null signal (0) is a way of determining if a particular pro-
cess ID is in use.
Each process has a signal mask, which is the set of signals whose delivery is cur-
rently blocked. Signals can be added to and removed from the signal mask using
sigprocmask().
If a signal is received while it is blocked, then it remains pending until it is
unblocked. Standard signals can’t be queued; that is, a signal can be marked as
pending (and thus later delivered) only once. A process can use the sigpending() sys-
tem call to retrieve a signal set (a data structure used to represent multiple different
signals) identifying the signals that it has pending.

#include <unistd.h>

int pause(void);
Always returns –1 with errno set to EINTR
Free download pdf