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