Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
763 views
in Technique[技术] by (71.8m points)

c - Detect death of parent process

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 call wait() for waiting terminated child process, and getting its status.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...