Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 10.21 Job-Control Signals 377


Mac OS X 10.6.8 doesn’t supportsigqueueor real-time signals. On Solaris 10,sigqueueis
in the real-time library,librt.

10.21 Job-Control Signals


Of the signals shown in Figure10.1, POSIX.1 considers six to be job-control signals:
SIGCHLD Child process has stopped or terminated.
SIGCONT Continue process, if stopped.
SIGSTOP Stop signal (can’t be caught or ignored).
SIGTSTP Interactive stop signal.
SIGTTIN Read from controlling terminal by background process group member.
SIGTTOU Write to controlling terminal by a background process group member.

Except for SIGCHLD,most application programs don’t handle these signals:
interactive shells usually do all the work required to handle them. When we type the
suspend character (usually Control-Z), SIGTSTP is sent to all processes in the
foreground process group. When we tell the shell to resume a job in the foreground or
background, the shell sends all the processes in the job theSIGCONTsignal. Similarly,if
SIGTTINorSIGTTOUis delivered to a process, the process is stopped by default, and
the job-control shell recognizes this and notifies us.
An exception is a process that is managing the terminal—thevi( 1 ) editor,for
example. It needs to know when the user wants to suspend it so that it can restorethe
terminal’s state to the way it was whenviwas started. Also, when it resumes in the
foreground, thevieditor needs to set the terminal state back to the way it wants it, and
it needs to redraw the terminal screen. Wesee how a program such asvihandles this
in the example that follows.
Thereare some interactions between the job-control signals. When any of the four
stop signals (SIGTSTP,SIGSTOP,SIGTTIN,orSIGTTOU) is generated for a process,
any pending SIGCONT signal for that process is discarded. Similarly,when the
SIGCONTsignal is generated for a process, any pending stop signals for that same
process arediscarded.
Note that the default action forSIGCONTis to continue the process, if it is stopped;
otherwise, the signal is ignored. Normally, we don’t have to do anything with this
signal. WhenSIGCONT is generated for a process that is stopped, the process is
continued, even if the signal is blocked or ignored.

Example


The program in Figure10.31 demonstrates the normal sequence of code used when a
program handles job control. This program simply copies its standardinput to its
standardoutput, but comments aregiven in the signal handler for typical actions
performed by a program that manages a screen.
Free download pdf