C Signal Handling
   HOME

TheInfoList



OR:

In the
C Standard Library The C standard library, sometimes referred to as libc, is the standard library for the C (programming language), C programming language, as specified in the ISO C standard.International Organization for Standardization, ISO/International Electrote ...
, signal processing defines how a program handles various
signals A signal is both the process and the result of Signal transmission, transmission of data over some transmission media, media accomplished by embedding some variation. Signals are important in multiple subject fields including signal processin ...
while it executes. A signal can report some exceptional behavior within the program (such as
division by zero In mathematics, division by zero, division (mathematics), division where the divisor (denominator) is 0, zero, is a unique and problematic special case. Using fraction notation, the general example can be written as \tfrac a0, where a is the di ...
), or a signal can report some asynchronous event outside the program (such as someone striking an interactive attention key on a keyboard).


Standard signals

The C standard defines only 6 signals. They are all defined in signal.h header (csignal header in C++): *SIGABRT – "abort", abnormal termination. *SIGFPEfloating point exception. *SIGILL – "illegal", invalid instruction. *SIGINT – "interrupt", interactive attention request sent to the program. *SIGSEGV – " segmentation violation", invalid memory access. *SIGTERM – "terminate", termination request sent to the program. Additional signals may be specified in the signal.h header by the implementation. For example, Unix and
Unix-like A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
operating systems (such as
Linux Linux ( ) is a family of open source Unix-like operating systems based on the Linux kernel, an kernel (operating system), operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically package manager, pac ...
) define more than 15 additional signals; see
Unix signal Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
.


Debugging

*SIGTRAP for debugging purposes. It's platform-dependent and may be used on
Unix Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
-like operating systems.


Handling

A signal can be generated by calling raise() or kill() system calls. raise() sends a signal to the current process, kill() sends a signal to a specific process. A signal handler is a function which is called by the target environment when the corresponding signal occurs. The target environment suspends execution of the program until the signal handler returns or calls longjmp(). Signal handlers can be set with signal() or sigaction(). The behavior of signal() has been changed multiple times across history and its use is discouraged.https://man7.org/linux/man-pages/man2/signal.2.html Signal(2) manpage It is only portable when used to set a signal's disposition to SIG_DFL or SIG_IGN. Signal handlers can be specified for all but two signals (
SIGKILL Signals are standardized messages sent to a running program to trigger specific behavior, such as quitting or error handling. They are a limited form of inter-process communication (IPC), typically used in Unix, Unix-like, and other POSIX-comp ...
and SIGSTOP cannot be caught, blocked or ignored). 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().


Functions


Example usage

#include #include #include volatile sig_atomic_t status = 0; static void catch_function(int signo) int main(void)


See also

*
Unix signal Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...


References

{{Reflist C standard library