Detect death of parent process

前端 未结 3 1526
梦如初夏
梦如初夏 2021-01-04 11:06

How can I detect parent process death in Linux OS?

If in parent process called fork(), that create child process. In the parent process I can use system

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-04 11:45

    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.

    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.

    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.

提交回复
热议问题