]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/exit.3
Many pages: Use correct letter case in page titles (TH)
[thirdparty/man-pages.git] / man3 / exit.3
CommitLineData
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 11exit \- cause normal process termination
afa8db94
AC
12.SH LIBRARY
13Standard 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
22The
23.BR exit ()
7776aac9 24function 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 29All functions registered with
60a90ecd
MK
30.BR atexit (3)
31and
32.BR on_exit (3)
a210091c
MK
33are 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)
36or
37.BR on_exit (3)
38to register an additional
a210091c
MK
39function to be executed during exit processing;
40the new registration is added to the front of the list of functions
41that remain to be called.)
65b70abf
MK
42If one of these functions does not return
43(e.g., it calls
44.BR _exit (2),
45or kills itself with a signal),
46then none of the remaining functions is called,
47and further exit processing (in particular, flushing of
48.BR stdio (3)
49streams) is abandoned.
50If a function has been registered multiple times using
51.BR atexit (3)
52or
53.BR on_exit (3),
54then it is called as many times as it was registered.
dd3568a1 55.PP
65b70abf
MK
56All open
57.BR stdio (3)
58streams are flushed and closed.
60a90ecd
MK
59Files created by
60.BR tmpfile (3)
61are removed.
dd3568a1 62.PP
a210091c 63The C standard specifies two constants,
3b46efc3 64\fBEXIT_SUCCESS\fP and \fBEXIT_FAILURE\fP,
60a90ecd
MK
65that may be passed to
66.BR exit ()
67to indicate successful or unsuccessful
fea681da 68termination, respectively.
47297adb 69.SH RETURN VALUE
60a90ecd
MK
70The
71.BR exit ()
72function does not return.
d0e96150 73.SH ATTRIBUTES
2a2948ab
MK
74For 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
79allbox;
c466875e 80lbx lb lb
2a2948ab
MK
81l l l.
82Interface Attribute Value
83T{
84.BR exit ()
ef38f354 85T} Thread safety MT-Unsafe race:exit
2a2948ab 86.TE
c466875e
MK
87.hy
88.ad
89.sp 1
2a2948ab 90.PP
d0e96150
PH
91The
92.BR exit ()
93function uses a global variable that is not protected,
94so it is not thread-safe.
3113c7f3 95.SH STANDARDS
7a475b7c 96POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
fea681da 97.SH NOTES
cffa7950 98The behavior is undefined if one of the functions registered using
60a90ecd
MK
99.BR atexit (3)
100and
101.BR on_exit (3)
102calls either
103.BR exit ()
104or
105.BR longjmp (3).
5eab557e
MK
106Note that a call to
107.BR execve (2)
108removes registrations created using
109.BR atexit (3)
110and
111.BR on_exit (3).
dd3568a1 112.PP
8bd58774
MK
113The use of
114.B EXIT_SUCCESS
115and
116.B EXIT_FAILURE
117is slightly more portable
008f1ecc 118(to non-UNIX environments) than the use of 0 and some nonzero value
c13182ef
MK
119like 1 or \-1.
120In particular, VMS uses a different convention.
dd3568a1 121.PP
946bdf72
MF
122BSD has attempted to standardize exit codes
123(which some C libraries such as the GNU C library have also adopted);
124see the file
fea681da 125.IR <sysexits.h> .
dd3568a1 126.PP
60a90ecd
MK
127After
128.BR exit (),
129the exit status must be transmitted to the
c13182ef 130parent process.
44266093
MK
131There are three cases:
132.IP \(bu 3
c13182ef 133If the parent has set
8bd58774
MK
134.BR SA_NOCLDWAIT ,
135or has set the
136.B SIGCHLD
137handler to
138.BR SIG_IGN ,
44266093
MK
139the status is discarded and the child dies immediately.
140.IP \(bu
2a825954 141If the parent was waiting on the child,
44266093
MK
142it is notified of the exit status and the child dies immediately.
143.IP \(bu
71e87683
MK
144Otherwise,
145the child becomes a "zombie" process:
146most of the process resources are recycled,
147but a slot containing minimal information about the child process
148(termination status, resource usage statistics) is retained in process table.
149This allows the parent to subsequently use
150.BR waitpid (2)
151(or similar) to learn the termination status of the child;
152at that point the zombie process slot is released.
dd3568a1 153.PP
8bd58774 154If the implementation supports the
0daa9e92 155.B SIGCHLD
8bd58774 156signal, this signal
c13182ef 157is sent to the parent.
8bd58774
MK
158If the parent has set
159.BR SA_NOCLDWAIT ,
160it is undefined whether a
161.B SIGCHLD
162signal is sent.
5088242f
MK
163.\"
164.SS Signals sent to other processes
165If the exiting process is a session leader and its controlling terminal
37894cb3 166is the controlling terminal of the session, then each process in
fea681da 167the foreground process group of this controlling terminal
8bd58774
MK
168is sent a
169.B SIGHUP
170signal, and the terminal is disassociated
fea681da
MK
171from this session, allowing it to be acquired by a new controlling
172process.
dd3568a1 173.PP
fea681da 174If the exit of the process causes a process group to become orphaned,
54221c6a 175and if any member of the newly orphaned process group is stopped,
8bd58774
MK
176then a
177.B SIGHUP
178signal followed by a
179.B SIGCONT
180signal will be
fea681da 181sent to each process in this process group.
aec4704f
MK
182See
183.BR setpgid (2)
184for an explanation of orphaned process groups.
ee7c8e26
MK
185.PP
186Except in the above cases,
187where the signalled processes may be children of the terminating process,
188termination of a process does
189.I not
190in general cause a signal to be sent to children of that process.
191However, a process can use the
192.BR prctl (2)
193.B PR_SET_PDEATHSIG
194operation 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)