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
mainfunction is a type compatible withint, a return from the initial call to themainfunction is equivalent to calling theexitfunction with the value returned by themainfunction as its argument; (snip)
#include <stdlib.h> void abort(void);The
abortfunction causes abnormal program termination to occur, unless the signalSIGABRTis 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
tmpfilefunction are removed.
#include <stdlib.h> void _Exit(int status);The
_Exitfunction causes normal program termination to occur and control to be returned to the host environment. No functions registered by theatexitfunction or signal handlers registered by thesignalfunction are called. The status returned to the host environment is determined in the same way as for theexitfunction. 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