C ++中退出和终止之间的区别 [英] Difference between exit and kill in C++

查看:191
本文介绍了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?

  1. exit(-1); // or some other exit code
  2. kill(getpid(), SIGKILL);
  1. exit(-1); // or some other exit code
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆