Signal.h
Encyclopedia
signal.h is a header file
Header file
Some programming languages use header files. These files allow programmers to separate certain elements of a program's source code into reusable files. Header files commonly contain forward declarations of classes, subroutines, variables, and other identifiers...

 defined in the C Standard Library
C standard library
The C Standard Library is the standard library for the programming language C, as specified in the ANSI C standard.. It was developed at the same time as the C POSIX library, which is basically a superset of it...

 to specify how a program handles signals
Signal (computing)
A signal is a limited form of inter-process communication used in Unix, Unix-like, and other POSIX-compliant operating systems. Essentially it is an asynchronous notification sent to a process in order to notify it of an event that occurred. When a signal is sent to a process, the operating system...

 while it executes. A signal can report some exceptional behavior within the program (such as division by zero
Division by zero
In mathematics, division by zero is division where the divisor is zero. Such a division can be formally expressed as a / 0 where a is the dividend . Whether this expression can be assigned a well-defined value depends upon the mathematical setting...

), or a signal can report some asynchronous event outside the program (such as someone striking an interactive attention key
SIGINT (POSIX)
On POSIX-compliant platforms, SIGINT is the signal sent to a process by its controlling terminal when a user wishes to interrupt the process. In source code, SIGINT is a symbolic constant defined in the header file signal.h...

 on a keyboard
).

A signal can be generated by calling raise (to send a signal to the current process) or kill (to send a signal to any process). Each implementation defines what signals it generates (if any) and under what circumstances it generates them. An implementation can define signals other than the ones listed here. The standard header can define additional macros with names beginning with SIG to specify the values of additional signals. All such values are integer constant expressions >= 0.

A signal handler can be specified for all but two signals (SIGKILL
SIGKILL
On POSIX-compliant platforms, SIGKILL is the signal sent to a process to cause it to terminate immediately. The symbolic constant for SIGKILL is defined in the header file signal.h. Symbolic signal names are used because signal numbers can vary across platforms, however on the vast majority of...

 and SIGSTOP
SIGSTOP
On POSIX-compliant platforms, SIGSTOP is the signal sent to a process to stop it for later resumption. The symbolic constant for SIGSTOP is defined in the header file signal.h. Symbolic signal names are used because signal numbers can vary across platforms....

 cannot be caught, blocked or ignored). A signal handler is a function that the target environment calls when the corresponding signal occurs. The target environment suspends execution of the program until the signal handler returns or calls longjmp. For maximum portability, an asynchronous signal handler should only:
  • make calls (that succeed) to the function signal
  • assign values to objects of type volatile
    Volatile variable
    In computer programming, particularly in the C, C++, C#, and Java programming languages, a variable or object declared with the volatile keyword usually has special properties related to optimization and/or threading...

     sig_atomic_t
  • return control to its caller


If the signal reports an error within the program (and the signal is not asynchronous), the signal handler can terminate by calling abort, exit, or longjmp
Setjmp.h
setjmp.h is a header defined in the C standard library to provide "non-local jumps": control flow that deviates from the usual subroutine call and return sequence...

.

Member functions

  • int raise(int sig). This raises a signal artificially. It causes signal sig to be generated. The sig argument is compatible with the SIG macros.If the call is successful, zero is returned. Otherwise a nonzero value is returned.

Example:
  1. include
  2. include
  3. include


static void catch_function(int signal) {
puts("Interactive attention signal caught.");
}

int main(void) {
if (signal(SIGINT, catch_function) SIG_ERR) {
fputs("An error occurred while setting a signal handler.\n", stderr);
return EXIT_FAILURE;
}
puts("Raising the interactive attention signal.");
if (raise(SIGINT) != 0) {
fputs("Error raising the signal.\n", stderr);
return EXIT_FAILURE;
}
puts("Exiting.");
return 0;
}

  • psignal(int sig, const char *s), outputs to stderr a string representation of a signal number. It is in 4.3BSD, Solaris and Linux
    Linux
    Linux is a Unix-like computer operating system assembled under the model of free and open source software development and distribution. The defining component of any Linux system is the Linux kernel, an operating system kernel first released October 5, 1991 by Linus Torvalds...

    , but is not specified by POSIX
    POSIX
    POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...

     or SUS
    Single UNIX Specification
    The Single UNIX Specification is the collective name of a family of standards for computer operating systems to qualify for the name "Unix"...

    .


On the same systems, string.h contains the non-standard strsignal(int sig) which operates analogously to strerror.
  • void (* signal(int sig, void (*func)(int)) )(int), sets the action taken when the program receives the signal sig. If the value of func is SIG_DFL, default handling for that signal will occur. If the value of func is SIG_IGN, the signal will be ignored. Otherwise func points to a signal handler function to be called when the signal occurs.


The function func may terminate by executing a return statement or by calling the abort, exit or longjmp functions. If func executes a return statement, and the value of sig was SIGFPE or any other implementation-defined value corresponding to a computational exception, the behavior is undefined. Otherwise, the program will resume execution at the point it was interrupted. If the function returns and the return request can be honored, the signal function returns the value of func for the most recent call to signal for the specified signal sig. Otherwise a value of SIG_ERR is returned and a positive value is stored in errno.

If the signal occurs other than as the result of calling the abort or raise function, the behavior is undefined if the signal handler calls any function in the standard library other than the signal function itself (with a first argument of the signal number corresponding to the signal that cause the invocation of the handler) or refers to any object with status storage duration other than by assigning to a static storage duration variable of type volatile sig_atomic_t. Furthermore, if such a call to the signal function results in a SIG_ERR return, the value of errno is indeterminate.
Member macros

  • SIG_DFL - Used to set default signal handling.
  • SIG_IGN - Used to handle a signal by ignoring it.
  • SIG_ERR - A number used for errors.

Member constants
Constant Meaning Systems
SIGHUP
SIGHUP
On POSIX-compliant platforms, SIGHUP is a signal sent to a process when its controlling terminal is closed....

 
Hangup POSIX
SIGINT
SIGINT (POSIX)
On POSIX-compliant platforms, SIGINT is the signal sent to a process by its controlling terminal when a user wishes to interrupt the process. In source code, SIGINT is a symbolic constant defined in the header file signal.h...

 
Interrupt ANSI
SIGQUIT
SIGQUIT
On POSIX-compliant platforms, SIGQUIT is the signal sent to a process by its controlling terminal when the user requests that the process perform a core dump. The symbolic constant for SIGQUIT is defined in the header file signal.h; on the vast majority of systems it is signal #3.SIGQUIT can...

 
Quit POSIX
SIGILL
SIGILL
On POSIX-compliant platforms, SIGILL is the signal sent to a process when it attempts to execute a malformed, unknown, or privileged instruction. The symbolic constant for SIGILL is defined in the signal.h header file...

 
Illegal instruction ANSI
SIGABRT
SIGABRT
On POSIX-compliant platforms, SIGABRT is the signal sent to a process to tell itto abort, i.e., to terminate. In source code, SIGABRT is a symbolic constant defined in the header file signal.h...

 
Abort ANSI
SIGTRAP
SIGTRAP
On POSIX-compliant platforms, SIGTRAP is the signal sent to a process when a condition arises that a debugger has requested to be informed of. In source code, SIGTRAP is a symbolic constant defined in the header file signal.h. Signal names are used instead of bare numbers because signal numbers can...

 
Trace trap POSIX
SIGIOT
SIGABRT
On POSIX-compliant platforms, SIGABRT is the signal sent to a process to tell itto abort, i.e., to terminate. In source code, SIGABRT is a symbolic constant defined in the header file signal.h...

 
IOT trap 4.2 BSD
SIGEMT
SIGEMT
On some Unix-like platforms, SIGEMT is the signal sent to computer programs when an emulator trap occurs. The symbolic constant for SIGEMT is defined in signal.h. Symbolic signal names are used because signal numbers can vary across platforms.-Etymology:...

 
EMT trap 4.2 BSD
SIGINFO
SIGINFO
On some Unix-like platforms, SIGINFO is the signal sent to computer programs when a status request is received from the keyboard. The symbolic constant for SIGINFO is defined in the header file signal.h...

 
Information 4.2 BSD
SIGFPE
SIGFPE
On POSIX compliant platforms, SIGFPE is the signal sent to a process when it performs an erroneous arithmetic operation. The symbolic constant for SIGFPE is defined in the header file signal.h.- Etymology :...

 
Floating-point exception ANSI
SIGKILL
SIGKILL
On POSIX-compliant platforms, SIGKILL is the signal sent to a process to cause it to terminate immediately. The symbolic constant for SIGKILL is defined in the header file signal.h. Symbolic signal names are used because signal numbers can vary across platforms, however on the vast majority of...

 
Kill, unblock-able POSIX
SIGBUS
SIGBUS
On POSIX-compliant platforms, SIGBUS is the signal sent to a process when it causes a bus error. The symbolic constant for SIGBUS is defined in the header file signal.h. Symbolic signal names are used because signal numbers can vary across platforms....

 
Bus error 4.2 BSD
SIGSEGV
SIGSEGV
On POSIX-compliant platforms, SIGSEGV is the signal sent to a process when it makes an invalid memory reference, or segmentation fault. The symbolic constant for SIGSEGV is defined in the header file signal.h...

 
Segmentation violation ANSI
SIGSYS
SIGSYS
On POSIX-compliant platforms, SIGSYS is the signal sent to a process when it passes a bad argument to a system call. The symbolic constant for SIGSYS is defined in the header file signal.h. Symbolic signal names are used because signal numbers can vary across platforms.-Etymology:SIG is a common...

 
Bad argument to system call 4.2 BSD
SIGPIPE
SIGPIPE
On POSIX-compliant platforms, SIGPIPE is the signal sent to a process when it attempts to write to a pipe without a process connected to the other end. The symbolic constant for SIGPIPE is defined in the header file signal.h. Symbolic signal names are used because signal numbers can vary across...

 
Broken pipe POSIX
SIGALRM
SIGALRM
On POSIX-compliant platforms, SIGALRM is the signal sent to a process when a time limit has elapsed. The symbolic constant for SIGALRM is defined in the signal.h header file. Symbolic signal names are used because signal numbers can vary across platforms....

 
Alarm clock POSIX
SIGTERM
SIGTERM
On POSIX-compliant platforms, SIGTERM is the signal sent to a process to request its termination. The symbolic constant for SIGTERM is defined in the header file signal.h. Symbolic signal names are used because signal numbers can vary across platforms, however on the vast majority of systems,...

 
Termination ANSI
SIGUSR1
SIGUSR1
On POSIX-compliant platforms, SIGUSR1 and SIGUSR2 are signals sent to a process to indicate user-defined conditions. The symbolic constants for them are defined in the header file signal.h. Symbolic signal names are used because signal numbers can vary across platforms.-Etymology:SIG is a common...

 
User-defined signal 1 POSIX
SIGUSR2  User-defined signal 2 POSIX
SIGCHLD
SIGCHLD
On POSIX-compliant platforms, SIGCHLD is the signal sent to a process when a child process terminates. The symbolic constant for SIGCHLD is defined in the header file signal.h. Symbolic signal names are used because signal numbers can vary across platforms....

 
Child status has changed POSIX
SIGCLD
SIGCHLD
On POSIX-compliant platforms, SIGCHLD is the signal sent to a process when a child process terminates. The symbolic constant for SIGCHLD is defined in the header file signal.h. Symbolic signal names are used because signal numbers can vary across platforms....

 
Same as SIGCHLD System V
SIGPWR
SIGPWR
On some Unix-like platforms, SIGPWR is the signal sent to computer programs when the system experiences a power failure. The symbolic constant for SIGPWR is defined in the header file signal.h...

 
Power failure restart System V
SIGXCPU
SIGXCPU
On POSIX-compliant platforms, SIGXCPU is the signal that is sent to a process when it has used up the CPU for a duration that exceeds a certain predetermined user-settable value....

 
Exceeded CPU time POSIX
SIGSTOP
SIGSTOP
On POSIX-compliant platforms, SIGSTOP is the signal sent to a process to stop it for later resumption. The symbolic constant for SIGSTOP is defined in the header file signal.h. Symbolic signal names are used because signal numbers can vary across platforms....

 
Pause execution POSIX
SIGCONT
SIGCONT
On POSIX-compliant platforms, SIGCONT is the signal sent to restart a process previously paused by the SIGSTOP or SIGTSTP signal. The symbolic constant for SIGCONT is defined in the header file signal.h. Symbolic signal names are used because signal numbers can vary across platforms.-Etymology:SIG...

Resume execution POSIX

External links
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK