How to find out if a third party has killed or broken the program in C++

huangapple go评论63阅读模式
英文:

How to find out if a third party has killed or broken the program in C++

问题

在Linux-Unix世界中,我们知道可以使用ctrl+C来在终端中终止前台运行的程序。

在C++中,我如何知道第三方是如何中断或强制杀死(kill -9)程序的?

我是否需要不断地检查线程中的某些内容?当程序被中断时,我需要在我的程序中返回退出状态码130,而当程序被杀死时,返回退出状态码140。

英文:

We know that in the world of Linux-Unix, we can break the program running in the foreground in the terminal with ctrl+C and end the program.

How can I know in C++ that a third party breaks or forces kill (kill -9) the program?

Do I have to check something with the thread constantly? I have to return exit status 130 in my program when the program is broken and 140 exit status when the program is killed.

答案1

得分: 5

通过 kill() 发送的某些信号可以被捕获和处理。但有些信号则不能(比如 SIGKILL,就是 kill -9 结果产生的信号)。你的程序会被内核简单地终止并清理 - 它没有任何机会注意到发生了什么或以任何方式做出反应。从你的程序角度看,这等同于用户从计算机上断电 - 程序就像被击中头部一样突然终止。

英文:

Some signals sent via kill() can be caught and handled. Some cannot (like SIGKILL which is what kill -9 results in). Your program is simply terminated and cleaned up by the kernel - it doesn't get any chance to notice what happened or react to it in any way. From your programs point of view it is equivalent to the user pulling the power from the computer - the program just dies as if shot in the head.

huangapple
  • 本文由 发表于 2023年4月19日 16:35:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定