1SIGNAL(2) Linux Programmer's Manual SIGNAL(2)
2
3
4
6 signal - ANSI C signal handling
7
9 #include <signal.h>
10
11 typedef void (*sighandler_t)(int);
12
13 sighandler_t signal(int signum, sighandler_t handler);
14
16 The behavior of signal() varies across Unix versions, and has also var‐
17 ied historically across different versions of Linux. Avoid its use:
18 use sigaction(2) instead. See Portability below.
19
20 signal() sets the disposition of the signal signum to handler, which is
21 either SIG_IGN, SIG_DFL, or the address of a programmer-defined func‐
22 tion (a "signal handler").
23
24 If the signal signum is delivered to the process, then one of the fol‐
25 lowing happens:
26
27 * If the disposition is set to SIG_IGN, then the signal is ignored.
28
29 * If the disposition is set to SIG_DFL, then the default action asso‐
30 ciated with the signal (see signal(7)) occurs.
31
32 * If the disposition is set to a function, then first either the dis‐
33 position is reset to SIG_DFL, or the signal is blocked (see Porta‐
34 bility below), and then handler is called with argument signum. If
35 invocation of the handler caused the signal to be blocked, then the
36 signal is unblocked upon return from the handler.
37
38 The signals SIGKILL and SIGSTOP cannot be caught or ignored.
39
41 signal() returns the previous value of the signal handler, or SIG_ERR
42 on error.
43
45 EINVAL signum is invalid.
46
48 C89, C99, POSIX.1-2001.
49
51 The effects of signal() in a multithreaded process are unspecified.
52
53 According to POSIX, the behavior of a process is undefined after it
54 ignores a SIGFPE, SIGILL, or SIGSEGV signal that was not generated by
55 kill(2) or raise(3). Integer division by zero has undefined result.
56 On some architectures it will generate a SIGFPE signal. (Also dividing
57 the most negative integer by -1 may generate SIGFPE.) Ignoring this
58 signal might lead to an endless loop.
59
60 See sigaction(2) for details on what happens when SIGCHLD is set to
61 SIG_IGN.
62
63 See signal(7) for a list of the async-signal-safe functions that can be
64 safely called from inside a signal handler.
65
66 The use of sighandler_t is a GNU extension. Various versions of libc
67 predefine this type; libc4 and libc5 define SignalHandler; glibc
68 defines sig_t and, when _GNU_SOURCE is defined, also sighandler_t.
69 Without use of such a type, the declaration of signal() is the somewhat
70 harder to read:
71
72 void ( *signal(int signum, void (*handler)(int)) ) (int);
73
74 Portability
75 The only portable use of signal() is to set a signal's disposition to
76 SIG_DFL or SIG_IGN. The semantics when using signal() to establish a
77 signal handler vary across systems (and POSIX.1 explicitly permits this
78 variation); do not use it for this purpose.
79
80 POSIX.1 solved the portability mess by specifying sigaction(2), which
81 provides explicit control of the semantics when a signal handler is
82 invoked; use that interface instead of signal().
83
84 In the original Unix systems, when a handler that was established using
85 signal() was invoked by the delivery of a signal, the disposition of
86 the signal would be reset to SIG_DFL, and the system did not block
87 delivery of further instances of the signal. System V also provides
88 these semantics for signal(). This was bad because the signal might be
89 delivered again before the handler had a chance to reestablish itself.
90 Furthermore, rapid deliveries of the same signal could result in recur‐
91 sive invocations of the handler.
92
93 BSD improved on this situation by changing the semantics of signal han‐
94 dling (but, unfortunately, silently changed the semantics when estab‐
95 lishing a handler with signal()). On BSD, when a signal handler is
96 invoked, the signal disposition is not reset, and further instances of
97 the signal are blocked from being delivered while the handler is exe‐
98 cuting.
99
100 The situation on Linux is as follows:
101
102 * The kernel's signal() system call provides System V semantics.
103
104 * By default, in glibc 2 and later, the signal() wrapper function does
105 not invoke the kernel system call. Instead, it calls sigaction(2)
106 using flags that supply BSD semantics. This default behavior is pro‐
107 vided as long as the _BSD_SOURCE feature test macro is defined. By
108 default, _BSD_SOURCE is defined; it is also implicitly defined if one
109 defines _GNU_SOURCE, and can of course be explicitly defined.
110
111 On glibc 2 and later, if the _BSD_SOURCE feature test macro is not
112 defined, then signal() provides System V semantics. (The default
113 implicit definition of _BSD_SOURCE is not provided if one invokes
114 gcc(1) in one of its standard modes (-std=xxx or -ansi) or defines
115 various other feature test macros such as _POSIX_SOURCE,
116 _XOPEN_SOURCE, or _SVID_SOURCE; see feature_test_macros(7).)
117
118 * The signal() function in Linux libc4 and libc5 provide System V
119 semantics. If one on a libc5 system includes <bsd/signal.h> instead
120 of <signal.h>, then signal() provides BSD semantics.
121
123 kill(1), alarm(2), kill(2), killpg(2), pause(2), sigaction(2), sig‐
124 nalfd(2), sigpending(2), sigprocmask(2), sigqueue(2), sigsuspend(2),
125 bsd_signal(3), raise(3), siginterrupt(3), sigsetops(3), sigvec(3),
126 sysv_signal(3), feature_test_macros(7), signal(7)
127
129 This page is part of release 3.25 of the Linux man-pages project. A
130 description of the project, and information about reporting bugs, can
131 be found at http://www.kernel.org/doc/man-pages/.
132
133
134
135Linux 2008-07-11 SIGNAL(2)