C99 offers the _Exit
function, which exits "immediately", although it does may close file descriptors. Unix/POSIX extends this behavior by mandating the closing of all fd's without flushing (and offers the synonym _exit
).
Will these functions call destructors for static
objects when called from a C++ program? Does the C++ standard make any guarantees about _Exit
?
(Inspired by this question; I suddenly wondered what happens in the typical fork
-exec
-_exit
idiom in C++.)
The _Exit() function in C/C++ gives normal termination of a program without performing any cleanup tasks. For example, it does not execute functions registered with atexit. Syntax: void _Exit(int exit_code); // Here the exit_code represent the exit // status of the program which can be // 0 or non-zero.
The exit , _Exit and _exit functions terminate the calling process. The exit function calls destructors for thread-local objects, then calls—in last-in-first-out (LIFO) order—the functions that are registered by atexit and _onexit , and then flushes all file buffers before it terminates the process.
The exit() function in C. The exit() function is used to terminate a process or function calling immediately in the program. It means any open file or function belonging to the process is closed immediately as the exit() function occurred in the program.
abort() -- Stop a ProgramThe abort() function causes an abnormal end of the program and returns control to the host environment. Like the exit() function, the abort() function deletes buffers and closes open files before ending the program. Calls to the abort() function raise the SIGABRT signal.
First, no form of program exit will automatically call destructors for heap objects (implied in ISO/IEC 14882:1998(E) 12.4.10).
Calling exit()
will not call destructors for objects with automatic duration, as it does not return through their enclosing scopes (3.6.1.4). However, destructors for static objects will be called, in reverse order of construction (18.3.8).
Calling abort()
does not call any destructors for any type of object, nor does it call atexit()
registered functions (18.3.3). The C++ standard copy I have here is a bit dated and does not mention _exit
or _Exit
directly, but I'd imagine that, if present, they should behave the same - that is, not calling any destructors. In particular, in the C99 standard, _Exit()
skips atexit
handlers (it is implementation defined whether stream buffers are flushed, open streams are closed, or temporary files removed).
Further note that abort()
can be cancelled by trapping signal SIGABRT
(ISO/IEC 9899:1999 (E) 7.20.4.1.2 - I only have C99 here but I expect it would be the same in the version referenced by C++). _Exit()
cannot.
On a more practical note, on most unix implementations of abort()
and _exit()
, abort()
raises a SIGABRT
while _exit()
simply calls an operating system call to terminate the process immediately. This means that the main differences are:
_exit()
abort()
may be trapped by a signal handlerabort()
may result in a core dump or similarIn a fork()/exec()
pattern, _exit()
would probably be preferable, to avoid the possibility of core dump.
It simply doesn't exist in standard C++, so there are no guarantees.
It is planned for inclusion in C++0x. That specifies (§18.5):
The function _Exit(int status) has additional behavior in this International Standard:
— The program is terminated without executing destructors for objects of automatic, thread, or static storage duration and without calling functions passed to atexit() (3.6.3).
Followup:
ISO approved C++0x on August 12, 2011.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With