exit(0) 和 raise(SIGTERM) 有什么区别
What is the difference between exit(0) and raise(SIGTERM)
我正试图在某些特定事件中终止我的应用程序。为什么一个人会使用 exit(0) 或 raise(SIGTERM) 而不是另一个?
另外自exit(0) returns EXIT_SUCCESS 到宿主机,SIGTERM是做什么的?总是失败吗?
exit
导致程序以正常退出状态终止,值由 exit
的参数给出,在您的情况下为 0.
raise
发出信号,可能会被捕获。如果未捕获或阻止,则执行默认操作。对于SIGTERM
,默认的动作是异常终止,导致它的信号在程序的退出状态中可见。
这对 iOS 应用程序有什么影响,我不确定。
默认情况下发送 raise( SIGTERM )
会导致 "abnormal termination",这意味着在正常程序终止时完成的许多事情都没有完成。根据 C 11 standard's 7.22.4.4 The exit function
Description
2 The exit
function causes normal program termination to occur. No
functions registered by the at_quick_exit
function are called. If a
program calls the exit
function more than once, or calls the
quick_exit
function in addition to the exit
function, the behavior is
undefined.
3 First, all functions registered by the atexit
function are called,
in the reverse order of their registration, except that a function
is called after any previously registered functions that had already
been called at the time it was registered. If, during the call to any
such function, a call to the longjmp
function is made that would
terminate the call to the registered function, the behavior is
undefined.
4 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.
5 Finally, control is returned to the host environment. ...
None 如果您调用 raise(SIGTERM)
就会发生这种情况,除非您添加一个将以某种方式调用 exit()
的信号处理程序。请注意,您不能直接从信号处理程序直接调用 exit()
,因为 exit()
函数不是异步信号安全的。
特别注意"all open streams with unwritten buffered data are flushed"。如果不这样做,您可能会丢失数据。
而且,如前所述,您的程序创建的任何临时文件都不会被清除。
在 C++ 代码中,具有 static
存储持续时间的对象在标准程序终止时被销毁。在 raise( SIGTERM )
引起的异常终止中不会发生这种情况。因此,这些对象在其析构函数中执行的任何清理都不会完成。
我正试图在某些特定事件中终止我的应用程序。为什么一个人会使用 exit(0) 或 raise(SIGTERM) 而不是另一个?
另外自exit(0) returns EXIT_SUCCESS 到宿主机,SIGTERM是做什么的?总是失败吗?
exit
导致程序以正常退出状态终止,值由 exit
的参数给出,在您的情况下为 0.
raise
发出信号,可能会被捕获。如果未捕获或阻止,则执行默认操作。对于SIGTERM
,默认的动作是异常终止,导致它的信号在程序的退出状态中可见。
这对 iOS 应用程序有什么影响,我不确定。
默认情况下发送 raise( SIGTERM )
会导致 "abnormal termination",这意味着在正常程序终止时完成的许多事情都没有完成。根据 C 11 standard's 7.22.4.4 The exit function
Description
2 The
exit
function causes normal program termination to occur. No functions registered by theat_quick_exit
function are called. If a program calls theexit
function more than once, or calls thequick_exit
function in addition to theexit
function, the behavior is undefined.3 First, all functions registered by the
atexit
function are called, in the reverse order of their registration, except that a function is called after any previously registered functions that had already been called at the time it was registered. If, during the call to any such function, a call to thelongjmp
function is made that would terminate the call to the registered function, the behavior is undefined.4 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.5 Finally, control is returned to the host environment. ...
None 如果您调用 raise(SIGTERM)
就会发生这种情况,除非您添加一个将以某种方式调用 exit()
的信号处理程序。请注意,您不能直接从信号处理程序直接调用 exit()
,因为 exit()
函数不是异步信号安全的。
特别注意"all open streams with unwritten buffered data are flushed"。如果不这样做,您可能会丢失数据。
而且,如前所述,您的程序创建的任何临时文件都不会被清除。
在 C++ 代码中,具有 static
存储持续时间的对象在标准程序终止时被销毁。在 raise( SIGTERM )
引起的异常终止中不会发生这种情况。因此,这些对象在其析构函数中执行的任何清理都不会完成。