exit(3)
๐ exit(3) โ the manual page, but make it make sense ๐
okay bestie, welcome to exit(3) from the Library Functions Manual. yes that's the whole title. we're doing process termination today and we're doing it cutely. ๐
โจ NAME โจ
exit โ cause normal process termination
like. the vibe check that ends the whole program. periodt. ๐
๐ LIBRARY ๐
Standard C library (libc, -lc) ๐ซถ
๐ธ SYNOPSIS ๐ธ
#include <stdlib.h>
[[noreturn]] void exit(int status);
so you include <stdlib.h>, and you call it with an int status. that's it. that's the whole summoning ritual. โจ
๐ฆ DESCRIPTION ๐ฆ
okay so exit() causes normal process termination, and the least significant byte of status (aka status & 0xFF) gets handed back to the parent process โ check out wait(2) for the receiving end of that. ๐
Every function you registered with atexit(3) and on_exit(3) gets called, in reverse order of registration. yes, reverse. last one in, first one out, it's giving stack energy fr fr.
and here's the spicy part โ it is literally possible for one of those functions to call atexit(3) or on_exit(3) and register yet another function to run during exit processing, and that new registration gets added to the front of the list of functions still waiting to be called. chaotic? yes. legal? also yes. ๐ญ
but if one of those functions doesn't return (like, it calls _exit(3), or it just kills itself with a signal), then none of the remaining functions get called, and further exit processing โ especially the flushing of stdio(3) streams โ is abandoned. gone. not happening. ๐
oh and if a function was registered multiple times with atexit(3) or on_exit(3), it gets called as many times as it was registered. equal treatment. no favoritism. ๐
All open stdio(3) streams get flushed and closed. Files created by tmpfile(3) get removed. clean up after yourself queen. ๐งน
The C standard specifies two constants โ <mint>EXIT_SUCCESS</mint> and <mint>EXIT_FAILURE</mint> โ and you can pass those to exit() to say "successful termination" or "unsuccessful termination" respectively. use them. they're iconic. ๐
๐ซ RETURN VALUE ๐ซ
The exit() function does not return.
not "returns 0". not "returns NULL". it does not return. no return value era. she's gone. ๐ฆโจ
๐ฅ ATTRIBUTES ๐ฅ
for an explanation of the terms used in this section, go see attributes(7) bestie.
- Interface:
exit() - Attribute: Thread safety
- Value:
MT-Unsafe race:exit
so basically: the exit() function uses a global variable that is not protected, which means it is not thread-safe. heads up if you're in your multithreaded girlboss era. ๐
โญ STANDARDS โญ
C11, POSIX.1-2008. certified. โจ
๐ HISTORY ๐
C89, POSIX.1-2001, SVr4, 4.3BSD. she's been around. she's got lore. ๐
๐ NOTES ๐
okay important stuff, pay attention:
The behavior is undefined if one of the functions registered using atexit(3) and on_exit(3) calls either exit() or longjmp(3). undefined. like, technically anything can happen. do not do this. ๐
Also note that a call to execve(2) removes registrations created using atexit(3) and on_exit(3). bye registrations. โ๏ธ
The use of EXIT_SUCCESS and EXIT_FAILURE is slightly more portable (to non-UNIX environments) than using 0 and some nonzero value like 1 or -1. In particular, VMS uses a different convention. so like, if you wanna be a global traveler with your code, use the constants. ๐
BSD has attempted to standardize exit codes (and some C libraries, such as the GNU C library, have adopted that too) โ see the file <sysexits.h>. ๐
After exit(), the exit status must be transmitted to the parent process. There are three cases:
- If the parent has set
SA_NOCLDWAIT, or has set theSIGCHLDhandler toSIG_IGN, the status is discarded and the child dies immediately. - If the parent was waiting on the child, it is notified of the exit status and the child dies immediately.
- Otherwise, the child becomes a "zombie" process: most of the process resources are recycled, but a slot containing minimal information about the child process (termination status, resource usage statistics) is retained in the process table. This allows the parent to subsequently use
waitpid(2)(or similar) to learn the termination status of the child; at that point the zombie process slot is released. ๐ง
If the implementation supports the SIGCHLD signal, this signal is sent to the parent. If the parent has set SA_NOCLDWAIT, it is undefined whether a SIGCHLD signal is sent.
๐ Signals sent to other processes ๐
If the exiting process is a session leader and its controlling terminal is the controlling terminal of the session, then every process in the foreground process group of this controlling terminal gets a SIGHUP signal, and the terminal is disassociated from this session โ which lets it be acquired by a new controlling process. handing off the aux cord, basically. ๐ง
If the exit of the process causes a process group to become orphaned, and if any member of the newly orphaned process group is stopped, then a SIGHUP signal followed by a SIGCONT signal will be sent to each process in this process group. See setpgid(2) for an explanation of orphaned process groups. ๐ซถ
Except in the above cases โ where the signalled processes may be children of the terminating process โ termination of a process does not in general cause a signal to be sent to children of that process. However, a process can use the prctl(2) PR_SET_PDEATHSIG operation to arrange that it receives a signal if its parent terminates. so if you wanna know when mom leaves, there's a way. ๐ญ
๐ SEE ALSO ๐
get_robust_list(2), exit(2), setpgid(2), wait(2), _exit(3), atexit(3), on_exit(3), tmpfile(3)
go read them. do it. iykyk. ๐โจ
and that's the whole page bestie!! ๐๐
this whole thing is from Linux man-pages 6.20, dated 2026-06-05 โ that's the exit(3) page, and honestly? she ate. ๐
๐ฆโจ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 2001, Andries E. Brouwer <aeb@cwi.nl>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms