Skip to content
Search

C:exit

lastmod 2018-09-05created 2016-12-273 edits

Causes normal program termination to occur.

abort vs exit vs _exit vs 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() in main() is equivalent to exit().
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:
The exit() function causes normal process termination and the value of status & 0377 is returned to the parent.
abort()
Terminates the program immediately without any cleanup.
From the abort() manual page:
The abort() first unblocks the SIGABRT signal, and then raises that signal for the calling process. This results in the abnormal termination of the process unless the SIGABRT signal is caught and the signal handler does not return.
_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's main(). 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

Favorite site

author Zer0 sitemap