检测父进程死亡 [英] Detect death of parent process

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

问题描述

我如何检测Linux操作系统父进程的死亡?

How can i detect parent process death in Linux OS?

如果在父进程称为fork()的,用于创建子进程。在父过程中,我可以使用系统调用wait()等待终止的子进程,并得到它的状态。

If in parent process called fork(), that create child process. In the parent process i can use system call wait() for waiting terminated child process, and getting it's status.

但是,我无法找到子进程如何能够检测父进程信息之死?

But, i can't find info about how child process can detect parent process' death?

推荐答案

您可以通过调用获取父进程ID getppid(),然后通过<$发送信号0 C $ C>杀()。返回0 code将表明该过程仍然活着。

You can get the parent process id by calling getppid() and then sending signal 0 via kill(). A return code of 0 will indicate that the process is still alive.

正如@Ariel提到的, getppid()或者返回原始父的PID或初始化的,这将是的pid 1.所以,你要么通过调用 getppid()在启动时或更高版本检查,如果你的父母以PID 1。

As mentioned by @Ariel, getppid() will either return the pid of the original parent or that of init, which will be pid 1. So you either have to store the parent pid by calling getppid() at startup or later check if your parent has pid 1.

根据在Linux上这个答案您还可以通过使用prctl检测的父母去世() PR_SET_PDEATHSIG 选项和一个自我选择的信号。

According to this answer on Linux you can also detect the death of the parent via prctl()'s PR_SET_PDEATHSIG option and a self-chosen signal.

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

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