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
74 <sysexits.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
88 statistics) is retained in process table. This allows the parent to
89 subsequently use waitpid(2) (or similar) to learn the termination
90 status of the child; at that point the zombie process slot is
91 released.
92
93 If the implementation supports the SIGCHLD signal, this signal is sent
94 to the parent. If the parent has set SA_NOCLDWAIT, it is undefined
95 whether a SIGCHLD signal is sent.
96
97 Signals sent to other processes
98 If the exiting process is a session leader and its controlling terminal
99 is the controlling terminal of the session, then each process in the
100 foreground process group of this controlling terminal is sent a SIGHUP
101 signal, and the terminal is disassociated from this session, allowing
102 it to be acquired by a new controlling process.
103
104 If the exit of the process causes a process group to become orphaned,
105 and if any member of the newly orphaned process group is stopped, then
106 a SIGHUP signal followed by a SIGCONT signal will be sent to each
107 process in this process group. See setpgid(2) for an explanation of
108 orphaned process groups.
109
110 Except in the above cases, where the signalled processes may be
111 children of the terminating process, termination of a process does not
112 in general cause a signal to be sent to children of that process.
113 However, a process can use the prctl(2) PR_SET_PDEATHSIG operation to
114 arrange that it receives a signal if its parent terminates.
115
117 _exit(2), get_robust_list(2), setpgid(2), wait(2), atexit(3),
118 on_exit(3), tmpfile(3)
119
120
121
122Linux man-pages 6.05 2023-07-20 exit(3)