Linux 考虑进程树的 SIGKILL SIGTERM 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5399355/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:22:23  来源:igfitidea点击:

Difference between SIGKILL SIGTERM considering process tree

linuxthread-safetysignals

提问by notytony

What is the difference between SIGTERM and SIGKILL when it comes to the process tree?
When a root thread recieves SIGKILL does it get killed cleanly or does it leave it's child threads as zombies?
Is there any signal which can be sent to a root thread to cleanly exit by not leaving any zombie threads ?

SIGTERM 和 SIGKILL 在进程树方面有什么区别?
当根线程收到 SIGKILL 时,它会被干净地杀死还是将其子线程保留为僵尸?
是否有任何信号可以发送到根线程以通过不留下任何僵尸线程来干净地退出?

Thanks.

谢谢。

采纳答案by Chris Marie

If you kill the root process (parent process), this should make orphan children, not zombie children. orphan children are made when you kill a process's parent, and the kernel makes init the parent of orphans. init is supposed to wait until orphan dies, then use wait to clean it up.

如果你杀死根进程(父进程),这应该是孤儿,而不是僵尸孩子。当您杀死进程的父进程时,会生成孤儿子进程,内核使 init 成为孤儿的父进程。init 应该等到孤儿死了,然后使用 wait 来清理它。

Zombie children are created when a process (not its parent) ends and its parent does not take up its exit status from the process table.

当一个进程(不是它的父进程)结束并且它的父进程没有从进程表中获取它的退出状态时,就会创建僵尸子进程。

It sounds to me like you are worried about leaving orphans because by definition, when you kill a zombies parent process, the zombie child itself dies.

在我看来,您很担心留下孤儿,因为根据定义,当您杀死僵尸父进程时,僵尸子进程本身就会死亡。

To kill your orphans, use kill -9 , which is the equivalent SIGKILL.

要杀死孤儿,请使用 kill -9 ,这相当于 SIGKILL。

Here is a more in depth tutorial for killing stuff on linux: http://riccomini.name/posts/linux/2012-09-25-kill-subprocesses-linux-bash/

这是在 linux 上杀死东西的更深入的教程:http: //riccomini.name/posts/linux/2012-09-25-kill-subprocesses-linux-bash/

回答by geekosaur

You can't control that by signal; only its parent process can control that, by calling waitpid()or setting signal handlers for SIGCHLD. See SIGCHLDand SA_NOCLDWAITin the sigaction(2)manpage for details.

你不能通过信号来控制它;只有它的父进程可以通过为 调用waitpid()或设置信号处理程序来控制它SIGCHLD。见SIGCHLDSA_NOCLDWAITsigaction(2)手册页了解详情。

Also, what happens to child threads depends on the Linux kernel version. With 2.6's POSIX threads, killing the main thread should cause the other threads to exit cleanly. With 2.4 LinuxThreads, each thread is actually a separate process and SIGKILLdoesn't give the root thread a chance to tell the others to shut down, whereas SIGTERMdoes.

此外,子线程会发生什么取决于 Linux 内核版本。对于 2.6 的 POSIX 线程,杀死主线程应该会导致其他线程干净地退出。使用 2.4 LinuxThreads,每个线程实际上是一个单独的进程,SIGKILL并且不会给根线程一个机会告诉其他线程关闭,而SIGTERM确实如此。