1EXIT(P) POSIX Programmer's Manual EXIT(P)
2
3
4
6 exit, _Exit, _exit - terminate a process
7
9 #include <stdlib.h>
10
11 void exit(int status);
12 void _Exit(int status);
13
14
15 #include <unistd.h>
16 void _exit(int status);
17
18
20 For exit() and _Exit(): The functionality described on this reference
21 page is aligned with the ISO C standard. Any conflict between the
22 requirements described here and the ISO C standard is unintentional.
23 This volume of IEEE Std 1003.1-2001 defers to the ISO C standard.
24
25 The value of status may be 0, EXIT_SUCCESS, EXIT_FAILURE, or any other
26 value, though only the least significant 8 bits (that is, status &
27 0377) shall be available to a waiting parent process.
28
29 The exit() function shall first call all functions registered by
30 atexit(), in the reverse order of their registration, except that a
31 function is called after any previously registered functions that had
32 already been called at the time it was registered. Each function is
33 called as many times as it was registered. If, during the call to any
34 such function, a call to the longjmp() function is made that would ter‐
35 minate the call to the registered function, the behavior is undefined.
36
37 If a function registered by a call to atexit() fails to return, the
38 remaining registered functions shall not be called and the rest of the
39 exit() processing shall not be completed. If exit() is called more than
40 once, the behavior is undefined.
41
42 The exit() function shall then flush all open streams with unwritten
43 buffered data, close all open streams, and remove all files created by
44 tmpfile(). Finally, control shall be terminated with the consequences
45 described below.
46
47 The _Exit() and _exit() functions shall be functionally equivalent.
48
49 The _Exit() and _exit() functions shall not call functions regis‐
50 tered with atexit() nor any registered signal handlers. Whether open
51 streams are flushed or closed, or temporary files are removed is imple‐
52 mentation-defined. Finally, the calling process is terminated with the
53 consequences described below.
54
55 These functions shall terminate the calling process with the follow‐
56 ing consequences:
57
58 Note: These consequences are all extensions to the ISO C standard and
59 are not further CX shaded. However, XSI extensions are shaded.
60
61
62 * All of the file descriptors, directory streams, conversion
63 descriptors, and message catalog descriptors open in the calling
64 process shall be closed.
65
66 * If the parent process of the calling process is executing a wait()
67 or waitpid(), and has neither set its SA_NOCLDWAIT flag nor set
68 SIGCHLD to SIG_IGN, it shall be notified of the calling process'
69 termination and the low-order eight bits (that is, bits 0377) of
70 status shall be made available to it. If the parent is not waiting,
71 the child's status shall be made available to it when the parent
72 subsequently executes wait() or waitpid().
73
74 The semantics of the waitid() function shall be equivalent to wait().
75
76 * If the parent process of the calling process is not executing a
77 wait() or waitpid(), and has neither set its SA_NOCLDWAIT flag
78 nor set SIGCHLD to SIG_IGN, the calling process shall be trans‐
79 formed into a zombie process. A zombie process is an inactive
80 process and it shall be deleted at some later time when its parent
81 process executes wait() or waitpid().
82
83 The semantics of the waitid() function shall be equivalent to wait().
84
85 * Termination of a process does not directly terminate its children.
86 The sending of a SIGHUP signal as described below indirectly termi‐
87 nates children in some circumstances.
88
89 * Either:
90
91 If the implementation supports the SIGCHLD signal, a SIGCHLD shall be
92 sent to the parent process.
93
94 Or:
95
96 If the parent process has set its SA_NOCLDWAIT flag, or set SIGCHLD to
97 SIG_IGN, the status shall be discarded, and the lifetime of the calling
98 process shall end immediately. If SA_NOCLDWAIT is set, it is implemen‐
99 tation-defined whether a SIGCHLD signal is sent to the parent process.
100
101 * The parent process ID of all of the calling process' existing child
102 processes and zombie processes shall be set to the process ID of an
103 implementation-defined system process. That is, these processes
104 shall be inherited by a special system process.
105
106 * Each attached shared-memory segment is detached and the value of
107 shm_nattch (see shmget()) in the data structure associated with its
108 shared memory ID shall be decremented by 1.
109
110 * For each semaphore for which the calling process has set a semadj
111 value (see semop() ), that value shall be added to the semval of the
112 specified semaphore.
113
114 * If the process is a controlling process, the SIGHUP signal shall be
115 sent to each process in the foreground process group of the control‐
116 ling terminal belonging to the calling process.
117
118 * If the process is a controlling process, the controlling terminal
119 associated with the session shall be disassociated from the session,
120 allowing it to be acquired by a new controlling process.
121
122 * If the exit of the process causes a process group to become
123 orphaned, and if any member of the newly-orphaned process group is
124 stopped, then a SIGHUP signal followed by a SIGCONT signal shall be
125 sent to each process in the newly-orphaned process group.
126
127 * All open named semaphores in the calling process shall be closed as
128 if by appropriate calls to sem_close().
129
130 * Any memory locks established by the process via calls to mlockall()
131 or mlock() shall be removed. If locked pages in the address space of
132 the calling process are also mapped into the address spaces of other
133 processes and are locked by those processes, the locks established
134 by the other processes shall be unaffected by the call by this
135 process to _Exit() or _exit().
136
137 * Memory mappings that were created in the process shall be unmapped
138 before the process is destroyed.
139
140 * Any blocks of typed memory that were mapped in the calling process
141 shall be unmapped, as if munmap() was implicitly called to unmap
142 them.
143
144 * All open message queue descriptors in the calling process shall be
145 closed as if by appropriate calls to mq_close().
146
147 * Any outstanding cancelable asynchronous I/O operations may be can‐
148 celed. Those asynchronous I/O operations that are not canceled
149 shall complete as if the _Exit() or _exit() operation had not yet
150 occurred, but any associated signal notifications shall be sup‐
151 pressed. The _Exit() or _exit() operation may block awaiting such
152 I/O completion. Whether any I/O is canceled, and which I/O may be
153 canceled upon _Exit() or _exit(), is implementation-defined.
154
155 * Threads terminated by a call to _Exit() or _exit() shall not invoke
156 their cancellation cleanup handlers or per-thread data destructors.
157
158 * If the calling process is a trace controller process, any trace
159 streams that were created by the calling process shall be shut down
160 as described by the posix_trace_shutdown() function, and any
161 process' mapping of trace event names to trace event type identi‐
162 fiers built for these trace streams may be deallocated.
163
165 These functions do not return.
166
168 No errors are defined.
169
170 The following sections are informative.
171
173 None.
174
176 Normally applications should use exit() rather than _Exit() or _exit().
177
179 Process Termination
180 Early proposals drew a distinction between normal and abnormal process
181 termination. Abnormal termination was caused only by certain signals
182 and resulted in implementation-defined "actions", as discussed below.
183 Subsequent proposals distinguished three types of termination: normal
184 termination (as in the current specification), simple abnormal termina‐
185 tion, and abnormal termination with actions. Again the distinction
186 between the two types of abnormal termination was that they were caused
187 by different signals and that implementation-defined actions would
188 result in the latter case. Given that these actions were completely
189 implementation-defined, the early proposals were only saying when the
190 actions could occur and how their occurrence could be detected, but not
191 what they were. This was of little or no use to conforming applica‐
192 tions, and thus the distinction is not made in this volume of
193 IEEE Std 1003.1-2001.
194
195 The implementation-defined actions usually include, in most historical
196 implementations, the creation of a file named core in the current work‐
197 ing directory of the process. This file contains an image of the memory
198 of the process, together with descriptive information about the
199 process, perhaps sufficient to reconstruct the state of the process at
200 the receipt of the signal.
201
202 There is a potential security problem in creating a core file if the
203 process was set-user-ID and the current user is not the owner of the
204 program, if the process was set-group-ID and none of the user's groups
205 match the group of the program, or if the user does not have permission
206 to write in the current directory. In this situation, an implementation
207 either should not create a core file or should make it unreadable by
208 the user.
209
210 Despite the silence of this volume of IEEE Std 1003.1-2001 on this fea‐
211 ture, applications are advised not to create files named core because
212 of potential conflicts in many implementations. Some implementations
213 use a name other than core for the file; for example, by appending the
214 process ID to the filename.
215
216 Terminating a Process
217 It is important that the consequences of process termination as
218 described occur regardless of whether the process called _exit() (per‐
219 haps indirectly through exit()) or instead was terminated due to a sig‐
220 nal or for some other reason. Note that in the specific case of exit()
221 this means that the status argument to exit() is treated in the same
222 way as the status argument to _exit().
223
224 A language other than C may have other termination primitives than the
225 C-language exit() function, and programs written in such a language
226 should use its native termination primitives, but those should have as
227 part of their function the behavior of _exit() as described. Implemen‐
228 tations in languages other than C are outside the scope of this version
229 of this volume of IEEE Std 1003.1-2001, however.
230
231 As required by the ISO C standard, using return from main() has the
232 same behavior (other than with respect to language scope issues) as
233 calling exit() with the returned value. Reaching the end of the main()
234 function has the same behavior as calling exit(0).
235
236 A value of zero (or EXIT_SUCCESS, which is required to be zero) for the
237 argument status conventionally indicates successful termination. This
238 corresponds to the specification for exit() in the ISO C standard. The
239 convention is followed by utilities such as make and various shells,
240 which interpret a zero status from a child process as success. For this
241 reason, applications should not call exit(0) or _exit(0) when they ter‐
242 minate unsuccessfully; for example, in signal-catching functions.
243
244 Historically, the implementation-defined process that inherits children
245 whose parents have terminated without waiting on them is called init
246 and has a process ID of 1.
247
248 The sending of a SIGHUP to the foreground process group when a control‐
249 ling process terminates corresponds to somewhat different historical
250 implementations. In System V, the kernel sends a SIGHUP on termination
251 of (essentially) a controlling process. In 4.2 BSD, the kernel does not
252 send SIGHUP in a case like this, but the termination of a controlling
253 process is usually noticed by a system daemon, which arranges to send a
254 SIGHUP to the foreground process group with the vhangup() function.
255 However, in 4.2 BSD, due to the behavior of the shells that support job
256 control, the controlling process is usually a shell with no other pro‐
257 cesses in its process group. Thus, a change to make _exit() behave this
258 way in such systems should not cause problems with existing applica‐
259 tions.
260
261 The termination of a process may cause a process group to become
262 orphaned in either of two ways. The connection of a process group to
263 its parent(s) outside of the group depends on both the parents and
264 their children. Thus, a process group may be orphaned by the termina‐
265 tion of the last connecting parent process outside of the group or by
266 the termination of the last direct descendant of the parent
267 process(es). In either case, if the termination of a process causes a
268 process group to become orphaned, processes within the group are dis‐
269 connected from their job control shell, which no longer has any infor‐
270 mation on the existence of the process group. Stopped processes within
271 the group would languish forever. In order to avoid this problem,
272 newly orphaned process groups that contain stopped processes are sent a
273 SIGHUP signal and a SIGCONT signal to indicate that they have been dis‐
274 connected from their session. The SIGHUP signal causes the process
275 group members to terminate unless they are catching or ignoring SIGHUP.
276 Under most circumstances, all of the members of the process group are
277 stopped if any of them are stopped.
278
279 The action of sending a SIGHUP and a SIGCONT signal to members of a
280 newly orphaned process group is similar to the action of 4.2 BSD, which
281 sends SIGHUP and SIGCONT to each stopped child of an exiting process.
282 If such children exit in response to the SIGHUP, any additional descen‐
283 dants receive similar treatment at that time. In this volume of
284 IEEE Std 1003.1-2001, the signals are sent to the entire process group
285 at the same time. Also, in this volume of IEEE Std 1003.1-2001, but not
286 in 4.2 BSD, stopped processes may be orphaned, but may be members of a
287 process group that is not orphaned; therefore, the action taken at
288 _exit() must consider processes other than child processes.
289
290 It is possible for a process group to be orphaned by a call to
291 setpgid() or setsid(), as well as by process termination. This volume
292 of IEEE Std 1003.1-2001 does not require sending SIGHUP and SIGCONT in
293 those cases, because, unlike process termination, those cases are not
294 caused accidentally by applications that are unaware of job control. An
295 implementation can choose to send SIGHUP and SIGCONT in those cases as
296 an extension; such an extension must be documented as required in <sig‐
297 nal.h>.
298
299 The ISO/IEC 9899:1999 standard adds the _Exit() function that results
300 in immediate program termination without triggering signals or
301 atexit()-registered functions. In IEEE Std 1003.1-2001, this is equiva‐
302 lent to the _exit() function.
303
305 None.
306
308 atexit() , close() , fclose() , longjmp() , posix_trace_shutdown() ,
309 posix_trace_trid_eventid_open() , semop() , shmget() , sigaction() ,
310 wait() , waitid() , waitpid() , the Base Definitions volume of
311 IEEE Std 1003.1-2001, <stdlib.h>, <unistd.h>
312
314 Portions of this text are reprinted and reproduced in electronic form
315 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
316 -- Portable Operating System Interface (POSIX), The Open Group Base
317 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
318 Electrical and Electronics Engineers, Inc and The Open Group. In the
319 event of any discrepancy between this version and the original IEEE and
320 The Open Group Standard, the original IEEE and The Open Group Standard
321 is the referee document. The original Standard can be obtained online
322 at http://www.opengroup.org/unix/online.html .
323
324
325
326IEEE/The Open Group 2003 EXIT(P)