]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Copyright (C) 2001 Andries Brouwer <aeb@cwi.nl>. |
2 | .\" | |
5fbde956 | 3 | .\" SPDX-License-Identifier: Linux-man-pages-copyleft |
fea681da | 4 | .\" |
bea08fec | 5 | .\" FIXME . There are a lot of other process termination actions that |
0d44d05f MK |
6 | .\" could be listed on this page. See, for example, the list in the |
7 | .\" POSIX exit(3p) page. | |
8 | .\" | |
4c1c5274 | 9 | .TH exit 3 (date) "Linux man-pages (unreleased)" |
fea681da | 10 | .SH NAME |
54221c6a | 11 | exit \- cause normal process termination |
afa8db94 AC |
12 | .SH LIBRARY |
13 | Standard C library | |
14 | .RI ( libc ", " \-lc ) | |
fea681da MK |
15 | .SH SYNOPSIS |
16 | .nf | |
17 | .B #include <stdlib.h> | |
68e4db0a | 18 | .PP |
cec12391 | 19 | .BI "noreturn void exit(int " status ); |
fea681da MK |
20 | .fi |
21 | .SH DESCRIPTION | |
60a90ecd MK |
22 | The |
23 | .BR exit () | |
7776aac9 | 24 | function causes normal process termination and the least significant byte of |
8678102a | 25 | .I status |
7776aac9 | 26 | (i.e., \fIstatus & 0xFF\fP) is returned to the parent (see |
fea681da | 27 | .BR wait (2)). |
dd3568a1 | 28 | .PP |
a210091c | 29 | All functions registered with |
60a90ecd MK |
30 | .BR atexit (3) |
31 | and | |
32 | .BR on_exit (3) | |
a210091c MK |
33 | are called, in the reverse order of their registration. |
34 | (It is possible for one of these functions to use | |
60a90ecd MK |
35 | .BR atexit (3) |
36 | or | |
37 | .BR on_exit (3) | |
38 | to register an additional | |
a210091c MK |
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.) | |
65b70abf MK |
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. | |
dd3568a1 | 55 | .PP |
65b70abf MK |
56 | All open |
57 | .BR stdio (3) | |
58 | streams are flushed and closed. | |
60a90ecd MK |
59 | Files created by |
60 | .BR tmpfile (3) | |
61 | are removed. | |
dd3568a1 | 62 | .PP |
a210091c | 63 | The C standard specifies two constants, |
3b46efc3 | 64 | \fBEXIT_SUCCESS\fP and \fBEXIT_FAILURE\fP, |
60a90ecd MK |
65 | that may be passed to |
66 | .BR exit () | |
67 | to indicate successful or unsuccessful | |
fea681da | 68 | termination, respectively. |
47297adb | 69 | .SH RETURN VALUE |
60a90ecd MK |
70 | The |
71 | .BR exit () | |
72 | function does not return. | |
d0e96150 | 73 | .SH ATTRIBUTES |
2a2948ab MK |
74 | For an explanation of the terms used in this section, see |
75 | .BR attributes (7). | |
c466875e MK |
76 | .ad l |
77 | .nh | |
2a2948ab MK |
78 | .TS |
79 | allbox; | |
c466875e | 80 | lbx lb lb |
2a2948ab MK |
81 | l l l. |
82 | Interface Attribute Value | |
83 | T{ | |
84 | .BR exit () | |
ef38f354 | 85 | T} Thread safety MT-Unsafe race:exit |
2a2948ab | 86 | .TE |
c466875e MK |
87 | .hy |
88 | .ad | |
89 | .sp 1 | |
2a2948ab | 90 | .PP |
d0e96150 PH |
91 | The |
92 | .BR exit () | |
93 | function uses a global variable that is not protected, | |
94 | so it is not thread-safe. | |
3113c7f3 | 95 | .SH STANDARDS |
7a475b7c | 96 | POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD. |
fea681da | 97 | .SH NOTES |
cffa7950 | 98 | The behavior is undefined if one of the functions registered using |
60a90ecd MK |
99 | .BR atexit (3) |
100 | and | |
101 | .BR on_exit (3) | |
102 | calls either | |
103 | .BR exit () | |
104 | or | |
105 | .BR longjmp (3). | |
5eab557e MK |
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). | |
dd3568a1 | 112 | .PP |
8bd58774 MK |
113 | The use of |
114 | .B EXIT_SUCCESS | |
115 | and | |
116 | .B EXIT_FAILURE | |
117 | is slightly more portable | |
008f1ecc | 118 | (to non-UNIX environments) than the use of 0 and some nonzero value |
c13182ef MK |
119 | like 1 or \-1. |
120 | In particular, VMS uses a different convention. | |
dd3568a1 | 121 | .PP |
946bdf72 MF |
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 | |
fea681da | 125 | .IR <sysexits.h> . |
dd3568a1 | 126 | .PP |
60a90ecd MK |
127 | After |
128 | .BR exit (), | |
129 | the exit status must be transmitted to the | |
c13182ef | 130 | parent process. |
44266093 MK |
131 | There are three cases: |
132 | .IP \(bu 3 | |
c13182ef | 133 | If the parent has set |
8bd58774 MK |
134 | .BR SA_NOCLDWAIT , |
135 | or has set the | |
136 | .B SIGCHLD | |
137 | handler to | |
138 | .BR SIG_IGN , | |
44266093 MK |
139 | the status is discarded and the child dies immediately. |
140 | .IP \(bu | |
2a825954 | 141 | If the parent was waiting on the child, |
44266093 MK |
142 | it is notified of the exit status and the child dies immediately. |
143 | .IP \(bu | |
71e87683 MK |
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. | |
dd3568a1 | 153 | .PP |
8bd58774 | 154 | If the implementation supports the |
0daa9e92 | 155 | .B SIGCHLD |
8bd58774 | 156 | signal, this signal |
c13182ef | 157 | is sent to the parent. |
8bd58774 MK |
158 | If the parent has set |
159 | .BR SA_NOCLDWAIT , | |
160 | it is undefined whether a | |
161 | .B SIGCHLD | |
162 | signal is sent. | |
5088242f MK |
163 | .\" |
164 | .SS Signals sent to other processes | |
165 | If the exiting process is a session leader and its controlling terminal | |
37894cb3 | 166 | is the controlling terminal of the session, then each process in |
fea681da | 167 | the foreground process group of this controlling terminal |
8bd58774 MK |
168 | is sent a |
169 | .B SIGHUP | |
170 | signal, and the terminal is disassociated | |
fea681da MK |
171 | from this session, allowing it to be acquired by a new controlling |
172 | process. | |
dd3568a1 | 173 | .PP |
fea681da | 174 | If the exit of the process causes a process group to become orphaned, |
54221c6a | 175 | and if any member of the newly orphaned process group is stopped, |
8bd58774 MK |
176 | then a |
177 | .B SIGHUP | |
178 | signal followed by a | |
179 | .B SIGCONT | |
180 | signal will be | |
fea681da | 181 | sent to each process in this process group. |
aec4704f MK |
182 | See |
183 | .BR setpgid (2) | |
184 | for an explanation of orphaned process groups. | |
ee7c8e26 MK |
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. | |
47297adb | 195 | .SH SEE ALSO |
fea681da | 196 | .BR _exit (2), |
18523cfc | 197 | .BR get_robust_list (2), |
aec4704f | 198 | .BR setpgid (2), |
fea681da MK |
199 | .BR wait (2), |
200 | .BR atexit (3), | |
201 | .BR on_exit (3), | |
202 | .BR tmpfile (3) |