How to make child process die after parent exits?

后端 未结 24 2120
天涯浪人
天涯浪人 2020-11-22 05:31

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 o

24条回答
  •  天涯浪人
    2020-11-22 06:26

    This solution worked for me:

    • Pass stdin pipe to child - you don't have to write any data into the stream.
    • Child reads indefinitely from stdin until EOF. An EOF signals that the parent has gone.
    • This is foolproof and portable way to detect when the parent has gone. Even if parent crashes, OS will close the pipe.

    This was for a worker-type process whose existence only made sense when the parent was alive.

提交回复
热议问题