1exit(3) Library Functions Manual exit(3)
2
3
4
6 exit - cause normal process termination
7
9 Standard C library (libc, -lc)
10
12 #include <stdlib.h>
13
14 [[noreturn]] void exit(int status);
15
17 The exit() function causes normal process termination and the least
18 significant byte of status (i.e., status & 0xFF) is returned to the
19 parent (see wait(2)).
20
21 All functions registered with atexit(3) and on_exit(3) are called, in
22 the reverse order of their registration. (It is possible for one of
23 these functions to use atexit(3) or on_exit(3) to register an addi‐
24 tional function to be executed during exit processing; the new regis‐
25 tration is added to the front of the list of functions that remain to
26 be called.) If one of these functions does not return (e.g., it calls
27 _exit(2), or kills itself with a signal), then none of the remaining
28 functions is called, and further exit processing (in particular, flush‐
29 ing of stdio(3) streams) is abandoned. If a function has been regis‐
30 tered multiple times using atexit(3) or on_exit(3), then it is called
31 as many times as it was registered.
32
33 All open stdio(3) streams are flushed and closed. Files created by
34 tmpfile(3) are removed.
35
36 The C standard specifies two constants, EXIT_SUCCESS and EXIT_FAILURE,
37 that may be passed to exit() to indicate successful or unsuccessful
38 termination, respectively.
39
41 The exit() function does not return.
42
44 For an explanation of the terms used in this section, see at‐
45 tributes(7).
46
47 ┌────────────────────────────────┬───────────────┬─────────────────────┐
48 │Interface │ Attribute │ Value │
49 ├────────────────────────────────┼───────────────┼─────────────────────┤
50 │exit() │ Thread safety │ MT-Unsafe race:exit │
51 └────────────────────────────────┴───────────────┴─────────────────────┘
52
53 The exit() function uses a global variable that is not protected, so it
54 is not thread-safe.
55
57 C11, POSIX.1-2008.
58
60 C89, POSIX.1-2001, SVr4, 4.3BSD.
61
63 The behavior is undefined if one of the functions registered using
64 atexit(3) and on_exit(3) calls either exit() or longjmp(3). Note that
65 a call to execve(2) removes registrations created using atexit(3) and
66 on_exit(3).
67
68 The use of EXIT_SUCCESS and EXIT_FAILURE is slightly more portable (to
69 non-UNIX environments) than the use of 0 and some nonzero value like 1
70 or -1. In particular, VMS uses a different convention.
71
72 BSD has attempted to standardize exit codes (which some C libraries
73 such as the GNU C library have also adopted); see the file <sysex‐
74 its.h>.
75
76 After exit(), the exit status must be transmitted to the parent
77 process. There are three cases:
78
79 • If the parent has set SA_NOCLDWAIT, or has set the SIGCHLD handler
80 to SIG_IGN, the status is discarded and the child dies immediately.
81
82 • If the parent was waiting on the child, it is notified of the exit
83 status and the child dies immediately.
84
85 • Otherwise, the child becomes a "zombie" process: most of the process
86 resources are recycled, but a slot containing minimal information
87 about the child process (termination status, resource usage statis‐
88 tics) is retained in process table. This allows the parent to sub‐
89 sequently use waitpid(2) (or similar) to learn the termination sta‐
90 tus of the child; at that point the zombie process slot is released.
91
92 If the implementation supports the SIGCHLD signal, this signal is sent
93 to the parent. If the parent has set SA_NOCLDWAIT, it is undefined
94 whether a SIGCHLD signal is sent.
95
96 Signals sent to other processes
97 If the exiting process is a session leader and its controlling terminal
98 is the controlling terminal of the session, then each process in the
99 foreground process group of this controlling terminal is sent a SIGHUP
100 signal, and the terminal is disassociated from this session, allowing
101 it to be acquired by a new controlling process.
102
103 If the exit of the process causes a process group to become orphaned,
104 and if any member of the newly orphaned process group is stopped, then
105 a SIGHUP signal followed by a SIGCONT signal will be sent to each
106 process in this process group. See setpgid(2) for an explanation of
107 orphaned process groups.
108
109 Except in the above cases, where the signalled processes may be chil‐
110 dren of the terminating process, termination of a process does not in
111 general cause a signal to be sent to children of that process. Howev‐
112 er, a process can use the prctl(2) PR_SET_PDEATHSIG operation to ar‐
113 range that it receives a signal if its parent terminates.
114
116 _exit(2), get_robust_list(2), setpgid(2), wait(2), atexit(3), on_ex‐
117 it(3), tmpfile(3)
118
119
120
121Linux man-pages 6.04 2023-03-30 exit(3)