父进程退出后如何让子进程死亡? [英] How to make child process die after parent exits?

查看:36
本文介绍了父进程退出后如何让子进程死亡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个进程,它产生一个子进程.现在,当父进程因任何原因(正常或异常,通过kill、^C、断言失败或其他任何原因)退出时,我希望子进程死亡.如何正确地做到这一点?

Suppose I have a process which spawns exactly one child process. Now when the parent process exits for whatever reason (normally or abnormally, by kill, ^C, assert failure or anything else) I want the child process to die. How to do that correctly?

关于stackoverflow的一些类似问题:

Some similar question on stackoverflow:

关于 Windows 的 stackoverflow 的一些类似问题:

Some similar question on stackoverflow for Windows:

推荐答案

通过指定选项PR_SET_PDEATHSIG,子进程可以要求内核在父进程死亡时传递SIGHUP(或其他信号)在 prctl() 这样的系统调用中:

Child can ask kernel to deliver SIGHUP (or other signal) when parent dies by specifying option PR_SET_PDEATHSIG in prctl() syscall like this:

prctl(PR_SET_PDEATHSIG, SIGHUP);

详见man 2 prctl.

仅限 Linux

这篇关于父进程退出后如何让子进程死亡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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