Add Book to My BookshelfPurchase This Book Online

Chapter 10 - Signals

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

Signal Concepts
As mentioned earlier, a signal is a software interrupt—an asynchronous notification that something has happened. Signals are delivered to a process by the operating system. They may occur because of something the program did (e.g., an attempt to divide by 0), something a user did (e.g., press the interrupt key on the keyboard), or something another program did (processes may send signals to one another).
A process may indicate the disposition of each signal defined by the operating system. The four possible dispositions for a signal are given below:
 The signal may be ignored. This tells the operating system to immediately discard the signal, without delivering it to the process. The process is never told that a signal was even generated. Ignoring signals is useful when a process simply doesn't want to be bothered with them, or when it wants to continue performing its task regardless of what happens.
 The signal may be blocked, or held. When a signal is blocked, it will not be delivered to the process, much as if it were being ignored. However, rather than simply discarding the signal, the operating system will place it on a queue of pending signals to be delivered to the process. If the process ever unblocks or releases the signal, it will be delivered at that time. Blocking signals is useful in programs that contain “critical sections” that must not be interrupted, but that otherwise wish to process the signals.
 The signal may be caught, or trapped. The process may tell the operating system to call a user-defined function called a signal handler whenever the signal is delivered. When the signal is delivered, the operating system suspends the process' normal execution, and calls the signal handler function. When the handler function returns, the process' execution picks up where it left off. Catching signals is useful any time the programmer wants to deal with unexpected events in a special way. For example, text editors make sure to catch keyboard interrupt signals, so that an inadvertent keystroke doesn't terminate the editor without saving the file.
 Each signal has a default disposition. As mentioned earlier, most signals' default dispositions are to terminate the process, sometimes with an accompanying core dump. Default dispositions are useful when there's nothing special the process needs to do with that signal; they are also useful for resetting the disposition of a signal that was previously being caught or ignored.
Version 7 UNIX provided 15 different signals. As features such as job control, interprocess communication, and networking were added, however, the list grew. In SVR4, 35 different “regular” signals are provided, along with several special-purpose signals used for realtime programming. The signals are described below.
SIGHUP
Hangup. This signal is sent to a process when its controlling terminal disconnects from the system (see Chapter 11, Processes). It is also commonly used to notify daemon processes to reread their configuration files; since daemon processes do not have controlling terminals, they would not normally receive this signal. The default disposition for this signal terminates the process.
SIGINT
Interrupt. This signal is delivered to a process when the user presses the interrupt key (usually CTRL-C ) on the keyboard. The default disposition for this signal terminates the process.
SIGQUIT
Quit. This signal is delivered to a process when the user presses the quit key (usually CTRL-\) on the keyboard. The default disposition for this signal terminates the process and produces a core file.
SIGILL
Illegal instruction. This signal is delivered to a process when it attempts to execute an illegal hardware instruction. The default disposition for this signal terminates the process and produces a core file.
SIGTRAP
Trace/breakpoint trap. The name for this signal is derived from the PDP-11 “trap” instruction. This signal is delivered to a process when it is being traced by a debugger and encounters a breakpoint; this causes the process to stop and the parent process (the debugger) to be notified. If the process is not being traced, the default disposition for this signal terminates the process and produces a core file.
SIGABRT
Abort. This signal is generated by the abort function (see Chapter 16, Miscellaneous Functions). The default disposition for this signal terminates the process and produces a core file.
SIGEMT
Emulator trap. The name for this signal is derived from the PDP-11 “emulator trap” instruction. It is delivered to a process when an implementation-defined hardware fault is detected. The default disposition for this signal terminates the process and produces a core file.
SIGFPE
Arithmetic exception. (FPE stands for Floating Point Exception, but this signal is used for non-floating-point arithmetic exceptions as well.) This signal is delivered to a process when it attempts an illegal arithmetic operation, such as division by 0, floating-point overflow, and so on. The default disposition for this signal terminates the process and produces a core file.
SIGKILL
Kill. This signal is used to terminate a process “with extreme prejudice.” It cannot be caught, blocked, or ignored. The default (only) disposition for this signal terminates the process.
SIGBUS
Bus error. This signal is delivered to a process when an implementation-defined hardware fault is detected. It usually indicates an attempt to use an improperly aligned address or to reference a nonexistent physical memory address. The default disposition for this signal terminates the process and produces a core file.
SIGSEGV
Segmentation violation (or segmentation fault). This signal is delivered to a process when it attempts to access an invalid virtual memory address, or attempts to access memory that it does not have permission to use. The default disposition for this signal terminates the process and produces a core file.
SIGSYS
Bad system call. This signal is delivered to a process when it somehow executes an instruction that the kernel thought was a system call, but the parameter with the instruction does not indicate a valid system call. The default disposition for this signal terminates the process and produces a core file.
SIGPIPE
Broken pipe. This signal is delivered to a process when it attempts to write on a pipe (see Chapter 13, Interprocess Communication) when there is no process on the other end to receive the data. The default disposition for this signal terminates the process.
SIGALRM
Alarm clock. This signal is delivered to a process when an alarm it has scheduled with the alarm or setitimer system calls (see below) goes off. The default disposition for this signal terminates the process.
SIGTERM
Software termination. This signal is used to tell a process to clean up whatever it's doing (close open files, etc.) and exit. It is the default signal sent by the kill command, and is also sent to all processes by the system shutdown procedure. The default disposition for this signal terminates the process.
SIGUSR1
User-defined signal one. This signal may be used for any programmer-defined purpose. The default disposition for this signal terminates the process.
SIGUSR2
User-defined signal two. This signal may be used for any programmer-defined purpose. The default disposition for this signal terminates the process.
SIGCHLD
Child status change. This signal indicates a change in a child process' status (see Chapter 11, Processes). It was introduced in Berkeley UNIX, and is delivered to a process whenever one of its children exits or is stopped or continued due to job control. The parent process can then use one of the wait system calls to determine what happened. The default disposition for this signal is to discard it; it is only delivered to a process if the process is catching it.
Versions of System V prior to SVR3 have a similar signal, SIGCLD. Unfortunately, this signal has very strange semantics, unlike those of any other signal:
 The default disposition of this signal is to discard it; it is only delivered to a process if the process is catching it.
 If the process specifically sets the signal's disposition to ignore, then children of the calling process will not generate zombie processes (see Chapter 11, Processes). Instead, on termination, the exit status of these processes is just discarded. If the parent process issues a call to one of the wait functions, it will block until all its children have terminated, and then wait will return -1 and errno will be set to ECHILD.
 If the process requests that the signal be caught, the operating system immediately checks if there are any child processes to be waited for, and if so, calls the SIGCLD handler. Thus, the signal is in a sense retroactive—processes that exited before its disposition was changed to a signal handler can result in the calling of the signal handler!
In SVR4,SIGCLD and SIGCHLD refer to the same signal. In order to provide backward compatibility with previous versions of System V, if the signal's disposition is set with either signal or sigset, the SIGCLD behavior is used. If its disposition is set with sigaction, the SIGCHLD behavior is used. This is of particular importance when porting programs from Berkeley-based versions of UNIX to SVR4.
SIGPWR
Power fail/restart. On systems connected to uninterruptible power supplies or that have battery backup, this signal can be sent to the init process to start an orderly system shutdown when power is lost or the batteries are about to fail. The default disposition for this signal is to discard it; it is only delivered to a process if the process is catching it.
SIGWINCH
Window size change. This signal is delivered to a process when the number of rows or columns of its controlling terminal are changed, as when a user resizes a window on a workstation. The default disposition for this signal is to discard it; it is only delivered to a process if the process is catching it.
SIGURG
Urgent socket condition. This signal is used to tell a process that an urgent condition (out of band data) exists on a network communications channel (see Chapter 14, Networking With Sockets). The default disposition for this signal is to discard it; it is only delivered to a process if the process is catching it.
SIGPOLL
Pollable event. This signal is delivered to a process when an event occurs on a pollable device. It is used in conjunction with the poll system call. The default disposition for this signal terminates the process.
SIGSTOP
Stop. This signal cannot be caught, blocked, or ignored. The default (only) disposition for this signal stops the process until a continue signal (SIGCONT) is received.
SIGTSTP
Stop. This signal is delivered to a process when the user presses the suspend key (usually CTRL-Z) on the keyboard. The default disposition for this signal stops the process until a continue signal (SIGCONT) is received.
SIGCONT
Continue. This signal can be caught, but it cannot be blocked or ignored. The default disposition for this signal starts the process if it was stopped, but it is otherwise discarded unless the process is catching it.
SIGTTIN
Stop for tty input. This signal is delivered to a process if it tries to read from the terminal while it is in the background. The default disposition for this signal stops the process until a continue signal (SIGCONT) is received.
SIGTTOU
Stop for tty output. This signal is delivered to a process if it tries to write to the terminal while it is in the background, and the terminal has the TOSTOP mode set (see Chapter 12, Terminals). The default disposition for this signal stops the process until a continue signal (SIGCONT) is received.
SIGVTALRM
Virtual timer expiration. This signal is delivered to a process when a virtual timer alarm it has scheduled with the setitimer system call expires. The default disposition for this signal terminates the process.
SIGPROF
Profiling timer expiration. This signal is delivered to a process when a profiling timer alarm it has scheduled with the setitimer system call expires. The default disposition for this signal terminates the process.
SIGXCPU
CPU time limit exceeded. This signal is delivered to a process when it exceeds its CPU time limit (see Chapter 9, System Configuration and Resource Limits). The default disposition for this signal terminates the process and produces a core file.
SIGXFSZ
File size limit exceeded. This signal is delivered to a process when it exceeds its maximum file size limit (see Chapter 9, System Configuration and Resource Limits). The default disposition for this signal terminates the process and produces a core file.
All versions of UNIX provide the first 15 signals in the preceding list. Most modern versions of UNIX also provide the job control signals, and many provide the timer-related signals as well. The other signals are less common, and may or may not be present in other versions. Other versions may also offer signals that do not appear in the list.

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback