Linux 进程如何杀死自己?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4632819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can a process kill itself?
提问by Durin
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>
int main(){
pid_t pid = fork();
if(pid==0){
system("watch ls");
}
else{
sleep(5);
killpg(getpid(),SIGTERM); //to kill the complete process tree.
}
return 0;
}
Terminal:
终端:
anirudh@anirudh-Aspire-5920:~/Desktop/testing$ gcc test.c
anirudh@anirudh-Aspire-5920:~/Desktop/testing$ ./a.out
Terminated
for the first 5 secs the output of the "watch ls" is shown and then it terminates because I send a SIGTERM.
前 5 秒显示“watch ls”的输出,然后它终止,因为我发送了一个 SIGTERM。
Question:How can a process kills itself ? I have done kill(getpid(),SIGTERM);
问题:进程如何杀死自己?我已经完成了 kill(getpid(),SIGTERM);
My hypothesis:so during the kill() call the process switches to kernel mode. The kill call sends the SIGTERM to the process and copies it in the process's process table. when the process comes back to user mode it sees the signal in its table and it terminates itself (HOW ? I REALLY DO NOT KNOW ) (I think I am going wrong (may be a blunder) somewhere in my hypothesis ... so Please enlighten me)
我的假设:所以在 kill() 调用过程中,进程切换到内核模式。kill 调用将 SIGTERM 发送到进程并将其复制到进程的进程表中。当进程返回到用户模式时,它会在其表中看到信号并自行终止(如何?我真的不知道) (我认为我在假设的某个地方出错了(可能是一个错误)......所以请开导我)
This code is actually a stub which I am using to test my other modules of the Project. Its doing the job for me and I am happy with it but there lies a question in my mind how actually a process kills itself. I want to know the step by step hypothesis.
这段代码实际上是一个存根,我用它来测试项目的其他模块。它为我完成了这项工作,我对此感到满意,但我的脑海中存在一个问题,一个过程实际上是如何自我毁灭的。我想知道逐步假设。
Thanks in advance
提前致谢
Anirudh Tomer
阿尼鲁德·托默
回答by Durin
I think it when it sees the SIGTERM signal in its process tables it first kills its child processes( complete tree since I have called killpg() ) and then it calls exit().
我认为当它在其进程表中看到 SIGTERM 信号时,它首先杀死它的子进程(完整的树,因为我已经调用了 killpg() ),然后它调用了 exit()。
I am still looking for a better answer to this question.
我仍在寻找这个问题的更好答案。
回答by Andrea Spadaccini
Your process dies because you are using killpg()
, that sends a signal to a process group, not to a process.
您的进程因为您正在使用而死亡killpg()
,它向进程组而不是进程发送信号。
When you fork()
, the children inherits from the father, among the other things, the process group. From man fork
:
当你fork()
,孩子从父亲那里继承,除其他外,进程组。来自man fork
:
* The child's parent process ID is the same as the parent's process ID.
So you kill the parent along with the child.
所以你杀死了父母和孩子。
If you do a simple kill(getpid(), SIGTERM)
then the father will kill the child (that is watch
ing ls
) and then will peacefully exit.
如果你做一个简单的kill(getpid(), SIGTERM)
然后父亲会杀死孩子(即watch
ing ls
)然后和平退出。
回答by Maxim Egorushkin
so during the kill() call the process switches to kernel mode. The kill call sends the SIGTERM to the process and copies it in the process's process table. when the process comes back to user mode it sees the signal in its table and it terminates itself (HOW ? I REALLY DO NOT KNOW )
所以在 kill() 调用过程中,进程切换到内核模式。kill 调用将 SIGTERM 发送到进程并将其复制到进程的进程表中。当进程返回用户模式时,它会在其表中看到信号并自行终止(如何?我真的不知道)
In Linux, when returning from the kernel mode to the user-space mode the kernel checks if there are any pending signals that can be delivered. If there are some it delivers the signals just before returning to the user-space mode. It can also deliver signals at other times, for example, if a process was blocked on select()
and then killed, or when a thread accesses an unmapped memory location.
在 Linux 中,当从内核模式返回到用户空间模式时,内核会检查是否有任何可以传递的挂起信号。如果有一些,它会在返回用户空间模式之前传递信号。它也可以在其他时间传递信号,例如,如果一个进程被阻塞select()
然后被杀死,或者当一个线程访问一个未映射的内存位置时。
回答by tchrist
This is super-easy in Perl:
这在 Perl 中非常简单:
{
local $SIG{TERM} = "IGNORE";
kill TERM => -$$;
}
Conversion into C is left as an exercise for the reader.
转换为 C 留给读者作为练习。
回答by ivan
kill(getpid(), SIGKILL); // itself I think
I tested it after a fork
with case 0: and it quit regular from separate parent process.
我在fork
case 0:之后对其进行了测试,它从单独的父进程中定期退出。
I don't know if this is a standard certification method ....
不知道这是不是标准的认证方式....
(I can see from my psensor tool that CPU usage return in 34% like a normal program code with a counter stopped ) .
(我可以从我的 psensor 工具中看到 CPU 使用率返回 34%,就像一个计数器停止的正常程序代码)。