C標準ライブラリ_Exit関数およびPOSIX準拠_exit関数は、プログラムを異常終了させるC標準abort関数と同様に開いているストリーム(FILE型)をフラッシュしない*1。ログファイルや標準出力(stdout)への出力欠落に注意。
一方で、C標準ライブラリexit関数ではストリームフラッシュ動作が保証される。エントリポイントmain関数からのreturnもexit関数呼び出しと同義。
C99 5.1.2.2.3, 7.20.4.1/p1-2, 7.20.4.3/p1, p4, 7.20.4.4/p1-2より引用(下線部は強調)。
If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; (snip)
#include <stdlib.h>
void abort(void);
The abort function causes abnormal program termination to occur, unless the signal SIGABRT is being caught and the signal handler does not return. Whether open streams with unwritten buffered data are flushed, open streams are closed, or temporary files are removed is implementation-defined. (snip)
#include <stdlib.h>
void exit(int status);
Next, all open streams with unwritten buffered data are flushed, all open streams are closed, and all files created by the tmpfile function are removed.
#include <stdlib.h>
void _Exit(int status);
The _Exit function causes normal program termination to occur and control to be returned to the host environment. No functions registered by the atexit function or signal handlers registered by the signal function are called. The status returned to the host environment is determined in the same way as for the exit function. Whether open streams with unwritten buffered data are flushed, open streams are closed, or temporary files are removed is implementation-defined.
IEEE Std 1003.1-2024より一部引用(下線部は強調)。この動作仕様はIEEE Std 1003.1-2017で明確化された。
DESCRIPTION
The _Exit() and _exit() functions shall be functionally equivalent.
The _Exit() and _exit() functions shall not call functions registered with atexit() nor at_quick_exit(), nor any registered signal handlers. Open streams shall not be flushed. Whether open streams are closed (without flushing) is implementation-defined. Finally, the calling process shall be terminated with the consequences described below.
CHANGE HISTORY
Issue 7
Austin Group Interpretation 1003.1-2001 #031 is applied, separating these functions from the exit() function.
Austin Group Interpretation 1003.1-2001 #085 is applied, clarifying the text regarding flushing of streams and closing of temporary files.
関連URL