OSDN Git Service

info/GNU_which: Add some explanations.
[linuxjm/jm.git] / manual / LDP_man-pages / original / man3 / exit.3
1 .\" Copyright (C) 2001 Andries Brouwer <aeb@cwi.nl>.
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" FIXME . There are a lot of other process termination actions that
26 .\" could be listed on this page. See, for example, the list in the
27 .\" POSIX exit(3p) page.
28 .\"
29 .TH EXIT 3  2020-02-09 "Linux" "Linux Programmer's Manual"
30 .SH NAME
31 exit \- cause normal process termination
32 .SH SYNOPSIS
33 .nf
34 .B #include <stdlib.h>
35 .PP
36 .BI "void exit(int " status );
37 .fi
38 .SH DESCRIPTION
39 The
40 .BR exit ()
41 function causes normal process termination and the least significant byte of
42 .I status
43 (i.e., \fIstatus & 0xFF\fP) is returned to the parent (see
44 .BR wait (2)).
45 .PP
46 All functions registered with
47 .BR atexit (3)
48 and
49 .BR on_exit (3)
50 are called, in the reverse order of their registration.
51 (It is possible for one of these functions to use
52 .BR atexit (3)
53 or
54 .BR on_exit (3)
55 to register an additional
56 function to be executed during exit processing;
57 the new registration is added to the front of the list of functions
58 that remain to be called.)
59 If one of these functions does not return
60 (e.g., it calls
61 .BR _exit (2),
62 or kills itself with a signal),
63 then none of the remaining functions is called,
64 and further exit processing (in particular, flushing of
65 .BR stdio (3)
66 streams) is abandoned.
67 If a function has been registered multiple times using
68 .BR atexit (3)
69 or
70 .BR on_exit (3),
71 then it is called as many times as it was registered.
72 .PP
73 All open
74 .BR stdio (3)
75 streams are flushed and closed.
76 Files created by
77 .BR tmpfile (3)
78 are removed.
79 .PP
80 The C standard specifies two constants,
81 \fBEXIT_SUCCESS\fP and \fBEXIT_FAILURE\fP,
82 that may be passed to
83 .BR exit ()
84 to indicate successful or unsuccessful
85 termination, respectively.
86 .SH RETURN VALUE
87 The
88 .BR exit ()
89 function does not return.
90 .SH ATTRIBUTES
91 For an explanation of the terms used in this section, see
92 .BR attributes (7).
93 .TS
94 allbox;
95 lb lb lb
96 l l l.
97 Interface       Attribute       Value
98 T{
99 .BR exit ()
100 T}      Thread safety   MT-Unsafe race:exit
101 .TE
102 .PP
103 The
104 .BR exit ()
105 function uses a global variable that is not protected,
106 so it is not thread-safe.
107 .SH CONFORMING TO
108 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
109 .SH NOTES
110 The behavior is undefined if one of the functions registered using
111 .BR atexit (3)
112 and
113 .BR on_exit (3)
114 calls either
115 .BR exit ()
116 or
117 .BR longjmp (3).
118 Note that a call to
119 .BR execve (2)
120 removes registrations created using
121 .BR atexit (3)
122 and
123 .BR on_exit (3).
124 .PP
125 The use of
126 .B EXIT_SUCCESS
127 and
128 .B EXIT_FAILURE
129 is slightly more portable
130 (to non-UNIX environments) than the use of 0 and some nonzero value
131 like 1 or \-1.
132 In particular, VMS uses a different convention.
133 .PP
134 BSD has attempted to standardize exit codes
135 (which some C libraries such as the GNU C library have also adopted);
136 see the file
137 .IR <sysexits.h> .
138 .PP
139 After
140 .BR exit (),
141 the exit status must be transmitted to the
142 parent process.
143 There are three cases:
144 .IP \(bu 3
145 If the parent has set
146 .BR SA_NOCLDWAIT ,
147 or has set the
148 .B SIGCHLD
149 handler to
150 .BR SIG_IGN ,
151 the status is discarded and the child dies immediately.
152 .IP \(bu
153 If the parent was waiting on the child,
154 it is notified of the exit status and the child dies immediately.
155 .IP \(bu
156 Otherwise,
157 the child becomes a "zombie" process:
158 most of the process resources are recycled,
159 but a slot containing minimal information about the child process
160 (termination status, resource usage statistics) is retained in process table.
161 This allows the parent to subsequently use
162 .BR waitpid (2)
163 (or similar) to learn the termination status of the child;
164 at that point the zombie process slot is released.
165 .PP
166 If the implementation supports the
167 .B SIGCHLD
168 signal, this signal
169 is sent to the parent.
170 If the parent has set
171 .BR SA_NOCLDWAIT ,
172 it is undefined whether a
173 .B SIGCHLD
174 signal is sent.
175 .\"
176 .SS Signals sent to other processes
177 If the exiting process is a session leader and its controlling terminal
178 is the controlling terminal of the session, then each process in
179 the foreground process group of this controlling terminal
180 is sent a
181 .B SIGHUP
182 signal, and the terminal is disassociated
183 from this session, allowing it to be acquired by a new controlling
184 process.
185 .PP
186 If the exit of the process causes a process group to become orphaned,
187 and if any member of the newly orphaned process group is stopped,
188 then a
189 .B SIGHUP
190 signal followed by a
191 .B SIGCONT
192 signal will be
193 sent to each process in this process group.
194 See
195 .BR setpgid (2)
196 for an explanation of orphaned process groups.
197 .PP
198 Except in the above cases,
199 where the signalled processes may be children of the terminating process,
200 termination of a process does
201 .I not
202 in general cause a signal to be sent to children of that process.
203 However, a process can use the
204 .BR prctl (2)
205 .B PR_SET_PDEATHSIG
206 operation to arrange that it receives a signal if its parent terminates.
207 .SH SEE ALSO
208 .BR _exit (2),
209 .BR get_robust_list (2),
210 .BR setpgid (2),
211 .BR wait (2),
212 .BR atexit (3),
213 .BR on_exit (3),
214 .BR tmpfile (3)
215 .SH COLOPHON
216 This page is part of release 5.10 of the Linux
217 .I man-pages
218 project.
219 A description of the project,
220 information about reporting bugs,
221 and the latest version of this page,
222 can be found at
223 \%https://www.kernel.org/doc/man\-pages/.