C ++中退出和终止之间的区别 [英] Difference between exit and kill in C++
问题描述
我已经编写了一个信号处理程序来处理SIG
,如果我收到了太多的信号,我想终止该进程.那么,以下哪个代码更好,还是应该同时使用它们?
I have written a signal handler to handle a SIG
, and I want to kill the process if I get too many of it. So, which of the following code is better, or should I use them both?
-
exit(-1); // or some other exit code
-
kill(getpid(), SIGKILL);
exit(-1); // or some other exit code
kill(getpid(), SIGKILL);
推荐答案
C ++中退出和终止之间的区别
Difference between exit and kill in C++
一个区别是C ++标准库中未指定kill
函数.它仅在POSIX中指定. exit
是标准C ++.
One difference is that kill
function is not specified in the C++ standard library. It is merely specified in POSIX. exit
is standard C++.
另一个差异是kill(getpid(), SIGKILL)
将导致操作系统强制终止进程. exit
而是执行清理(通过调用atexit
回调和刷新流等)并自愿终止执行.
Another difference is that kill(getpid(), SIGKILL)
will cause the operating system terminates the process forcefully. exit
instead performs cleanup (by calling a atexit
callback, and flushing streams etc.) and terminates the execution voluntarily.
那么,以下哪个代码更好,还是应该同时使用它们?
So, which of the following code is better, or should I use them both?
取决于用例,但通常exit
更为明智,因为人们通常想要它提供的清理.
Depends on the use case, but usually exit
is more sensible, since one usually wants the cleanup that it provides.
这篇关于C ++中退出和终止之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!