]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/exit.3
All pages: Replace the 4th argument to .TH by "Linux man-pages (unreleased)"
[thirdparty/man-pages.git] / man3 / exit.3
1 .\" Copyright (C) 2001 Andries Brouwer <aeb@cwi.nl>.
2 .\"
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
4 .\"
5 .\" FIXME . There are a lot of other process termination actions that
6 .\" could be listed on this page. See, for example, the list in the
7 .\" POSIX exit(3p) page.
8 .\"
9 .TH EXIT 3 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
10 .SH NAME
11 exit \- cause normal process termination
12 .SH LIBRARY
13 Standard C library
14 .RI ( libc ", " \-lc )
15 .SH SYNOPSIS
16 .nf
17 .B #include <stdlib.h>
18 .PP
19 .BI "noreturn void exit(int " status );
20 .fi
21 .SH DESCRIPTION
22 The
23 .BR exit ()
24 function causes normal process termination and the least significant byte of
25 .I status
26 (i.e., \fIstatus & 0xFF\fP) is returned to the parent (see
27 .BR wait (2)).
28 .PP
29 All functions registered with
30 .BR atexit (3)
31 and
32 .BR on_exit (3)
33 are called, in the reverse order of their registration.
34 (It is possible for one of these functions to use
35 .BR atexit (3)
36 or
37 .BR on_exit (3)
38 to register an additional
39 function to be executed during exit processing;
40 the new registration is added to the front of the list of functions
41 that remain to be called.)
42 If one of these functions does not return
43 (e.g., it calls
44 .BR _exit (2),
45 or kills itself with a signal),
46 then none of the remaining functions is called,
47 and further exit processing (in particular, flushing of
48 .BR stdio (3)
49 streams) is abandoned.
50 If a function has been registered multiple times using
51 .BR atexit (3)
52 or
53 .BR on_exit (3),
54 then it is called as many times as it was registered.
55 .PP
56 All open
57 .BR stdio (3)
58 streams are flushed and closed.
59 Files created by
60 .BR tmpfile (3)
61 are removed.
62 .PP
63 The C standard specifies two constants,
64 \fBEXIT_SUCCESS\fP and \fBEXIT_FAILURE\fP,
65 that may be passed to
66 .BR exit ()
67 to indicate successful or unsuccessful
68 termination, respectively.
69 .SH RETURN VALUE
70 The
71 .BR exit ()
72 function does not return.
73 .SH ATTRIBUTES
74 For an explanation of the terms used in this section, see
75 .BR attributes (7).
76 .ad l
77 .nh
78 .TS
79 allbox;
80 lbx lb lb
81 l l l.
82 Interface Attribute Value
83 T{
84 .BR exit ()
85 T} Thread safety MT-Unsafe race:exit
86 .TE
87 .hy
88 .ad
89 .sp 1
90 .PP
91 The
92 .BR exit ()
93 function uses a global variable that is not protected,
94 so it is not thread-safe.
95 .SH STANDARDS
96 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
97 .SH NOTES
98 The behavior is undefined if one of the functions registered using
99 .BR atexit (3)
100 and
101 .BR on_exit (3)
102 calls either
103 .BR exit ()
104 or
105 .BR longjmp (3).
106 Note that a call to
107 .BR execve (2)
108 removes registrations created using
109 .BR atexit (3)
110 and
111 .BR on_exit (3).
112 .PP
113 The use of
114 .B EXIT_SUCCESS
115 and
116 .B EXIT_FAILURE
117 is slightly more portable
118 (to non-UNIX environments) than the use of 0 and some nonzero value
119 like 1 or \-1.
120 In particular, VMS uses a different convention.
121 .PP
122 BSD has attempted to standardize exit codes
123 (which some C libraries such as the GNU C library have also adopted);
124 see the file
125 .IR <sysexits.h> .
126 .PP
127 After
128 .BR exit (),
129 the exit status must be transmitted to the
130 parent process.
131 There are three cases:
132 .IP \(bu 3
133 If the parent has set
134 .BR SA_NOCLDWAIT ,
135 or has set the
136 .B SIGCHLD
137 handler to
138 .BR SIG_IGN ,
139 the status is discarded and the child dies immediately.
140 .IP \(bu
141 If the parent was waiting on the child,
142 it is notified of the exit status and the child dies immediately.
143 .IP \(bu
144 Otherwise,
145 the child becomes a "zombie" process:
146 most of the process resources are recycled,
147 but a slot containing minimal information about the child process
148 (termination status, resource usage statistics) is retained in process table.
149 This allows the parent to subsequently use
150 .BR waitpid (2)
151 (or similar) to learn the termination status of the child;
152 at that point the zombie process slot is released.
153 .PP
154 If the implementation supports the
155 .B SIGCHLD
156 signal, this signal
157 is sent to the parent.
158 If the parent has set
159 .BR SA_NOCLDWAIT ,
160 it is undefined whether a
161 .B SIGCHLD
162 signal is sent.
163 .\"
164 .SS Signals sent to other processes
165 If the exiting process is a session leader and its controlling terminal
166 is the controlling terminal of the session, then each process in
167 the foreground process group of this controlling terminal
168 is sent a
169 .B SIGHUP
170 signal, and the terminal is disassociated
171 from this session, allowing it to be acquired by a new controlling
172 process.
173 .PP
174 If the exit of the process causes a process group to become orphaned,
175 and if any member of the newly orphaned process group is stopped,
176 then a
177 .B SIGHUP
178 signal followed by a
179 .B SIGCONT
180 signal will be
181 sent to each process in this process group.
182 See
183 .BR setpgid (2)
184 for an explanation of orphaned process groups.
185 .PP
186 Except in the above cases,
187 where the signalled processes may be children of the terminating process,
188 termination of a process does
189 .I not
190 in general cause a signal to be sent to children of that process.
191 However, a process can use the
192 .BR prctl (2)
193 .B PR_SET_PDEATHSIG
194 operation to arrange that it receives a signal if its parent terminates.
195 .SH SEE ALSO
196 .BR _exit (2),
197 .BR get_robust_list (2),
198 .BR setpgid (2),
199 .BR wait (2),
200 .BR atexit (3),
201 .BR on_exit (3),
202 .BR tmpfile (3)