简体   繁体   English

exit(0) 和 raise(SIGTERM) 有什么区别

[英]What is the difference between exit(0) and raise(SIGTERM)

I am trying to terminate my app on some particular event.我正在尝试在某些特定事件上终止我的应用程序。 Why would one use exit(0) or raise(SIGTERM) over the other?为什么一个人会使用 exit(0) 或 raise(SIGTERM) 而不是另一个?

Also since exit(0) returns EXIT_SUCCESS to the host, what does SIGTERM do?另外由于 exit(0) 将 EXIT_SUCCESS 返回给主机,SIGTERM 有什么作用? Is it always failure?总是失败吗?

exit causes the program to terminate with normal exit status, with value given by the argument to exit , in your case 0. exit导致程序以正常退出状态终止,其值由exit的参数给出,在您的情况下为 0。

raise raises a signal, which may be caught. raise会引发一个信号,该信号可能会被捕获。 If it's not caught or blocked then the default action is carried out.如果它没有被捕获或阻止,则执行默认操作。 For SIGTERM , the default action is abnormal termination, and the signal that caused it is visible in the program's exit status.对于SIGTERM ,默认操作是异常终止,导致它的信号在程序的退出状态中可见。

What consequences this has for iOS applications, I'm not sure.这对 iOS 应用程序有什么影响,我不确定。

Sending raise( SIGTERM ) by default causes an "abnormal termination", which means many things done in a normal program termination are not done.默认情况下发送raise( SIGTERM )会导致“异常终止”,这意味着在正常程序终止中完成的许多事情都没有完成。 Per the C 11 standard's 7.22.4.4 The exit function根据C 11 标准的7.22.4.4 出口 function

Description描述

2 The exit function causes normal program termination to occur. 2 exit function 导致程序正常终止。 No functions registered by the at_quick_exit function are called.没有调用at_quick_exit function 注册的函数。 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.如果一个程序多次调用exit function,或在exit function 之外调用quick_exit function,则行为未定义。

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. 3 首先,所有由atexit function 注册的函数都按照注册的相反顺序被调用,除了 function 在任何先前注册的函数之后被调用,这些函数在注册时已经被调用。 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.如果在调用任何此类 function 期间,对longjmp function 的调用将终止对已注册 function 的调用,则行为未定义。

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. 4 接下来,刷新所有打开的带有未写入缓冲数据的流,关闭所有打开的流,并删除由tmpfile function 创建的所有文件。

5 Finally, control is returned to the host environment. 5 最后,控制权返回到主机环境。 ... ...

None of that will happen if you call raise(SIGTERM) unless you add a signal handler that will somehow invoke exit() .如果您调用raise(SIGTERM) ,这一切都不会发生,除非您添加一个会以某种方式调用exit()的信号处理程序。 And note that you can NOT simply call exit() directly from a signal handler as the exit() function is not async-signal-safe.请注意,您不能简单地从信号处理程序中直接调用exit() ,因为exit() function 不是异步信号安全的。

Note especially "all open streams with unwritten buffered data are flushed".特别注意“所有带有未写入缓冲数据的打开流都被刷新”。 If that's not done, you risk losing data.如果不这样做,您就有丢失数据的风险。

And, as noted, any temporary files created by your program will not be cleaned up.而且,如前所述,您的程序创建的任何临时文件都不会被清理。

In C++ code, objects with static storage duration are destroyed in a standard program termination.在 C++ 代码中,具有static存储持续时间的对象在标准程序终止中被销毁。 That will not happen in an abnormal termination caused by raise( SIGTERM ) .这不会在raise( SIGTERM )引起的异常终止中发生。 So any cleanup that those objects perform in their destructors will not be done.因此,这些对象在其析构函数中执行的任何清理都不会完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM