Section 12.3. Available Signals

   


12.3. Available Signals

Linux has quite a few signals available for processes to use, all of which are summarized in Table 12.1. There are four default actions the kernel can take for a signal: ignore it, stop the process (it is still alive and can be restarted later), terminate the process, or terminate the process and generate a core dump.[13] The following are more detailed descriptions of each signal listed in Table 12.1:

[13] See page 130 for more information on core dumps.

SIGABRT

The abort() function sends this signal to the process that called it, terminating the process with a core file. Under Linux, the C library calls abort() whenever an assertion fails.[14]

SIGALRM

Sent when an alarm set by the alarm() system call has expired. Alarms are the basis of the sleep() function discussed in Chapter 18.

SIGBUS

When a process violates hardware constraints other than those related to memory protections, this signal is sent. This usually occurs on traditional Unix platforms when an un-aligned access occurs, but the Linux kernel fixes unaligned access and continues the process. Memory alignment is discussed further on page 76.

SIGCHLD

This signal is sent to a process when one of that process's children has exited or stopped. This allows the process to avoid zombies by calling one of the wait() functions from the signal handler. If the parent always waits for its children to exit before continuing, this signal can be safely ignored. This signal is different from the SIGCLD signal provided by early releases of System V. SIGCLD is obsolete and should not be used anymore.

SIGCONT

This signal restarts a process that has been stopped. It may also be caught by a process, allowing it to take an action after being restarted. Most editors catch this signal and refresh the terminal when they are restarted. See Chapter 15 for more information on stopping and starting processes.

SIGFPE

This signal is sent when a process causes an arithmetic exception. All floating-point exceptions, such as overflows and underflows, cause this signal, as does integer division by 0.

SIGHUP

When a terminal is disconnected, the session leader for the session associated with the terminal is sent a SIGHUP signal unless that terminal's CLOCAL flag has been set. If a session leader exits, SIGHUP is sent to the process group leader for each process group in the session. Most processes terminate when SIGHUP is received because it indicates the user is no longer present.

 

Many daemon processes interpret SIGHUP as a request to close and reopen log files and reread configuration files.

SIGILL

The process attempted to run an illegal hardware instruction.

SIGINT

This signal is sent to all processes in the foreground process group when the user presses the interrupt character (normally, ^C).

SIGIO

An asynchronous I/O event has occurred. Asynchronous I/O is rarely used and is not documented in this book. We suggest consulting other books for information on using asynchronous I/O [Stevens, 1992].

SIGKILL

This signal is generated only by kill() and allows a user to terminate a process unconditionally.

SIGPIPE

The process has written to a pipe that has no readers.

SIGPROF

The profile timer has expired. This signal is usually used by profilers, which are programs that examine another process's run-time characteristics. Profilers are normally used to optimize a program's execution speed by helping programmers to find execution bottlenecks.[15]

SIGPWR

The system detected an impending loss of power. It is usually sent to init by a daemon that is monitoring the machine's power source, allowing the machine to be cleanly shut down before power failure.

SIGQUIT

This signal is sent to all processes in the foreground process group when the user presses the quit character (usually, ^\).

SIGSEGV

This signal is sent when a process attempts to access memory it is not allowed to access. It is generated when a process tries to read from unmapped memory, execute a page of memory that has not been mapped with execute permissions, or write to memory it does not have write access to.

SIGSTOP

This signal is generated only by kill() and allows a user to unconditionally stop a process. See Chapter 15 for more information on stopping processes.

SIGSYS

When a program attempts a nonexistent system call, the kernel terminates the program with SIGSYS. This should never happen to programs that make system calls through the system's C library.

SIGTERM

This signal is generated only by kill() and allows a user to gracefully terminate a process. A process should exit as soon as possible after receiving this signal.

SIGTRAP

When a process has crossed a breakpoint, this signal is sent to the process. It is usually intercepted by a debugger process that set the breakpoint.

SIGTSTP

This signal is sent to all processes in the foreground process group when the user presses the suspend character (usually, ^Z). See Chapter 15 for more information on job control.

SIGTTIN

This signal is sent to a background process that has tried to read from its controlling terminal. See Chapter 15 for more information on job control.

SIGTTOU

This signal is sent to a background process that has tried to write to its controlling terminal. See Chapter 15 for more information on job control.

SIGURG

This signal is sent when out-of-band data has been received on a socket. Out-of-band data is an advanced networking topic outside the scope of this book; [Stevens, 2004] covers it thoroughly, however.

SIGUSR1

There is no defined use for this signal; processes may use it for whatever purposes they like.

SIGUSR2

There is no defined use for this signal; processes may use it for whatever purposes they like.

SIGVTALRM

Sent when a timer set by setitimer() has expired. For information on using timers, see Chapter 18.

SIGWINCH

When a terminal window has changed size, such as when an xterm is resized, all processes in the foreground process group for that process are sent SIGWINCH. See page 376 for information on finding the current size of the controlling terminal.

SIGXCPU

The process has exceeded its soft CPU limit. This signal is sent once per second until the process exceeds its hard CPU limit; once that happens the process is terminated with SIGKILL. For information on process resource limits, see pages 118-119.

SIGXFSZ

When a program exceeds its file size limit, SIGXFSZ is sent to it, which normally kills the process. If the signal is caught, the system call that would have exceeded the file size limit instead returns the error EFBIG. For information on process resource limits, see pages 118-119.


[14] Assertions are discussed in most introductory C books [Kernighan, 1988].

[15] The gprof utility, included with all Linux distributions, is a simple profiler.

12.3.1. Describing Signals

Occasionally, an application needs a description of a signal to display to the user or put in a log. There are three ways to do this;[16] unfortunately, none of them is standardized.

[16] These methods parallel the ways of getting descriptions for system errors described in Chapter 9.

The oldest method is through sys_siglist, which is an array of strings describing each signal, indexed by the signal number itself. It includes descriptions for all signals except the real-time signals. Using sys_siglist is more portable than the other methods described here. BSD systems provide psignal(), which provides a shortcut for displaying messages. Here is a version of psignal():

 #include <signal.h> #include <stdio.h> void psignal(int signum, const char * msg) {     printf("%s: %s\n", msg, sys_siglist[signum]); } 


Note that it uses the same list of signals as sys_siglist, so real-time signals are excluded.

The GNU C library used by Linux provides one more method, strsignal(). This function is not provided by any standard, so to access the prototype C files need to define _GNU_SOURCE.

 #define _GNU_SOURCE #include <signal.h> char * strsignal(int signum); 


Like sys_siglist, strsignal() provides a description for signal number signum. It uses sys_siglist for most signals and constructs a description for the real-time signals. For example, SIGRTMIN + 5 would be described as "Real-time signal 5." For an example of strsignal() being used, look at lines 639-648 and 717 of ladsh4.c, which appears in Appendix B.


       
    top
     


    Linux Application Development
    Linux Application Development (paperback) (2nd Edition)
    ISBN: 0321563220
    EAN: 2147483647
    Year: 2003
    Pages: 168

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net