]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/exit.3
tsearch.3: Minor tweak to Florian's patch
[thirdparty/man-pages.git] / man3 / exit.3
CommitLineData
fea681da
MK
1.\" Copyright (C) 2001 Andries Brouwer <aeb@cwi.nl>.
2.\"
93015253 3.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
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.
c13182ef 12.\"
fea681da
MK
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.
c13182ef 20.\"
fea681da
MK
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 23.\" %%%LICENSE_END
fea681da 24.\"
bea08fec 25.\" FIXME . There are a lot of other process termination actions that
0d44d05f
MK
26.\" could be listed on this page. See, for example, the list in the
27.\" POSIX exit(3p) page.
28.\"
9ba01802 29.TH EXIT 3 2019-03-06 "Linux" "Linux Programmer's Manual"
fea681da 30.SH NAME
54221c6a 31exit \- cause normal process termination
fea681da
MK
32.SH SYNOPSIS
33.nf
34.B #include <stdlib.h>
68e4db0a 35.PP
fea681da
MK
36.BI "void exit(int " status );
37.fi
38.SH DESCRIPTION
60a90ecd
MK
39The
40.BR exit ()
41function causes normal process termination and the
a210091c 42value of \fIstatus & 0377\fP is returned to the parent
fea681da
MK
43(see
44.BR wait (2)).
dd3568a1 45.PP
a210091c 46All functions registered with
60a90ecd
MK
47.BR atexit (3)
48and
49.BR on_exit (3)
a210091c
MK
50are called, in the reverse order of their registration.
51(It is possible for one of these functions to use
60a90ecd
MK
52.BR atexit (3)
53or
54.BR on_exit (3)
55to register an additional
a210091c
MK
56function to be executed during exit processing;
57the new registration is added to the front of the list of functions
58that remain to be called.)
65b70abf
MK
59If one of these functions does not return
60(e.g., it calls
61.BR _exit (2),
62or kills itself with a signal),
63then none of the remaining functions is called,
64and further exit processing (in particular, flushing of
65.BR stdio (3)
66streams) is abandoned.
67If a function has been registered multiple times using
68.BR atexit (3)
69or
70.BR on_exit (3),
71then it is called as many times as it was registered.
dd3568a1 72.PP
65b70abf
MK
73All open
74.BR stdio (3)
75streams are flushed and closed.
60a90ecd
MK
76Files created by
77.BR tmpfile (3)
78are removed.
dd3568a1 79.PP
a210091c 80The C standard specifies two constants,
3b46efc3 81\fBEXIT_SUCCESS\fP and \fBEXIT_FAILURE\fP,
60a90ecd
MK
82that may be passed to
83.BR exit ()
84to indicate successful or unsuccessful
fea681da 85termination, respectively.
47297adb 86.SH RETURN VALUE
60a90ecd
MK
87The
88.BR exit ()
89function does not return.
d0e96150 90.SH ATTRIBUTES
2a2948ab
MK
91For an explanation of the terms used in this section, see
92.BR attributes (7).
93.TS
94allbox;
95lb lb lb
96l l l.
97Interface Attribute Value
98T{
99.BR exit ()
ef38f354 100T} Thread safety MT-Unsafe race:exit
2a2948ab
MK
101.TE
102.PP
d0e96150
PH
103The
104.BR exit ()
105function uses a global variable that is not protected,
106so it is not thread-safe.
47297adb 107.SH CONFORMING TO
7a475b7c 108POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
fea681da 109.SH NOTES
dd3568a1 110.PP
cffa7950 111The behavior is undefined if one of the functions registered using
60a90ecd
MK
112.BR atexit (3)
113and
114.BR on_exit (3)
115calls either
116.BR exit ()
117or
118.BR longjmp (3).
5eab557e
MK
119Note that a call to
120.BR execve (2)
121removes registrations created using
122.BR atexit (3)
123and
124.BR on_exit (3).
dd3568a1 125.PP
8bd58774
MK
126The use of
127.B EXIT_SUCCESS
128and
129.B EXIT_FAILURE
130is slightly more portable
008f1ecc 131(to non-UNIX environments) than the use of 0 and some nonzero value
c13182ef
MK
132like 1 or \-1.
133In particular, VMS uses a different convention.
dd3568a1 134.PP
946bdf72
MF
135BSD has attempted to standardize exit codes
136(which some C libraries such as the GNU C library have also adopted);
137see the file
fea681da 138.IR <sysexits.h> .
dd3568a1 139.PP
60a90ecd
MK
140After
141.BR exit (),
142the exit status must be transmitted to the
c13182ef 143parent process.
44266093
MK
144There are three cases:
145.IP \(bu 3
c13182ef 146If the parent has set
8bd58774
MK
147.BR SA_NOCLDWAIT ,
148or has set the
149.B SIGCHLD
150handler to
151.BR SIG_IGN ,
44266093
MK
152the status is discarded and the child dies immediately.
153.IP \(bu
2a825954 154If the parent was waiting on the child,
44266093
MK
155it is notified of the exit status and the child dies immediately.
156.IP \(bu
71e87683
MK
157Otherwise,
158the child becomes a "zombie" process:
159most of the process resources are recycled,
160but a slot containing minimal information about the child process
161(termination status, resource usage statistics) is retained in process table.
162This allows the parent to subsequently use
163.BR waitpid (2)
164(or similar) to learn the termination status of the child;
165at that point the zombie process slot is released.
dd3568a1 166.PP
8bd58774 167If the implementation supports the
0daa9e92 168.B SIGCHLD
8bd58774 169signal, this signal
c13182ef 170is sent to the parent.
8bd58774
MK
171If the parent has set
172.BR SA_NOCLDWAIT ,
173it is undefined whether a
174.B SIGCHLD
175signal is sent.
5088242f
MK
176.\"
177.SS Signals sent to other processes
178If the exiting process is a session leader and its controlling terminal
37894cb3 179is the controlling terminal of the session, then each process in
fea681da 180the foreground process group of this controlling terminal
8bd58774
MK
181is sent a
182.B SIGHUP
183signal, and the terminal is disassociated
fea681da
MK
184from this session, allowing it to be acquired by a new controlling
185process.
dd3568a1 186.PP
fea681da 187If the exit of the process causes a process group to become orphaned,
54221c6a 188and if any member of the newly orphaned process group is stopped,
8bd58774
MK
189then a
190.B SIGHUP
191signal followed by a
192.B SIGCONT
193signal will be
fea681da 194sent to each process in this process group.
aec4704f
MK
195See
196.BR setpgid (2)
197for an explanation of orphaned process groups.
ee7c8e26
MK
198.PP
199Except in the above cases,
200where the signalled processes may be children of the terminating process,
201termination of a process does
202.I not
203in general cause a signal to be sent to children of that process.
204However, a process can use the
205.BR prctl (2)
206.B PR_SET_PDEATHSIG
207operation to arrange that it receives a signal if its parent terminates.
47297adb 208.SH SEE ALSO
fea681da 209.BR _exit (2),
18523cfc 210.BR get_robust_list (2),
aec4704f 211.BR setpgid (2),
fea681da
MK
212.BR wait (2),
213.BR atexit (3),
214.BR on_exit (3),
215.BR tmpfile (3)