C:exit
lastmod 2018-09-05created 2016-12-273 edits
Causes normal program termination to occur.
abort vs exit vs _exit vs atexit
- exit() and abort()
- Stackoverflow - What is the difference between exit() and abort()?
- Process termination - exit, _exit, atexit
-
exit() - The exit function destroys all static objects, flushes all buffers, closes all I/O channels, and calls the functions registered with
atexit()while terminating the program. If a function is passed to atexit(), an exception is thrown and terminate() is called. -
return()inmain()is equivalent toexit(). - The C standard specifies two constants, EXIT_SUCCESS and EXIT_FAILURE, which can be passed to exit() to indicate successful and unsuccessful termination, respectively.
-
C_exit_process.png - From the exit() manual page:
-
abort() - Terminates the program immediately without any cleanup.
- From the abort() manual page:
-
_exit() - terminates the calling process "immediately". All open file descriptors belonging to the process are closed, the process's children are inherited by process 1, the init process, and the process's parent is sent a SIGCHLD signal. The value status is returned to the parent process as the process's exit status.
- (
_Exit()is equivalent to_exit())
-
atexit() - Registers the given function to be called at normal process termination, either via
exit(3)or via return from the program'smain(). Functions registered this way are called in the reverse order of their registration, and no arguments are passed. (The same function may be registered multiple times; it is called once for each registration.) POSIX.1-2001 requires that an implementation allow at least ATEXIT_MAX (32) such functions to be registered. The actual limit supported by an implementation can be obtained using sysconf(3).- When a child process is created via fork(2), it inherits copies of its parent's registrations.
- Upon a successful call to one of the exec(3) functions, all registrations are removed.
See also
- C/C++
- C:abort
- C:atexit
- C:quick_exit
- C:at_quick_exit
- std::set_terminate
- C:_Exit