Content-type: text/html
exit, atexit, _exit - Terminates a process
Standard C Library (libc.so, libc.a)
#include <stdlib.h>
int atexit(
void (*function) (void));
void exit(
int status);
#include <unistd.h>
Interfaces documented on this reference page conform to industry standards as follows:
exit(): ISO C, XPG4, XPG4-UNIX
atexit(), _exit(): XPG4, XPG4-UNIX
Refer to the standards(5) reference page for more information about industry standards and associated tags.
Indicates the status of the process. Points to a function that is called at normal process termination for cleanup processing. The number of exit handlers that can be specified with the atexit() function is limited by the amount of available virtual memory.
The atexit() function registers functions to be called at normal process termination for cleanup processing. The function adds a single exit handler to a list of handlers to be called at process termination. The system calls the functions in reverse order, calling the function at the top of the list first. Any function that is registered more than once will be repeated.
The exit() function terminates the calling process after calling the _cleanup() function to flush any buffered output. Then it calls any functions registered previously for the process by the atexit() function, in the reverse order to that in which they were registered. In addition, the exit() function flushes all open output streams, closes all open streams, and removes all files created by the tmpfile() function. Finally, it calls the _exit() function, which completes process termination and does not return.
The _exit() and exit() functions terminate the calling process and cause the following to occur: All of the file descriptors and directory streams open in the calling process are closed. Since the exit() function terminates the process, any errors encountered during these close operations go unreported. Message catalog descriptors and conversion descriptors opened in the calling process are also closed with no reporting of errors. The parent process ID of all the calling process' existing child processes and zombie processes is reset. The child processes continue executing; however, their parent process ID is set to the process ID of init. The init process thus adopts each of these processes, catches the SIGCHLD signals that they generate, and calls the wait() function for each of them. If the parent process of the calling process is running a wait() or waitpid() function, that parent process is notified that the calling process is being terminated. The low-order 8 bits (that is, bits 0377 or 0xFF) of the status parameter are made available to the parent process.
[Digital] If a thread calls the _exit() function, the entire process exits and all threads within the process are terminated.
[XPG4-UNIX] An application should call sysconf() to obtain the value of {ATEXIT_MAX}, the number of handlers that can be registered. There is no way for an application to tell how many functions have already been registered with atexit().
To prematurely terminate atexit handler processing from within a handler, _exit() can be called. It is not recommended to call exit() from within an atexit handler.
The exit() function and _exit() function do not return.
The atexit() function returns 0 (zero) if successful. The function fails if an application attempts to register more process cleanup functions than available virtual memory allows. In this case, the function returns a nonzero value.
Functions: acct(2), sigaction(2), sigvec(2), wait(2), ldr_atexit(3), times(3)
Standards: standards(5) delim off