How to make child process die after parent exits?

后端 未结 24 2111
天涯浪人
天涯浪人 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:25

    If you send a signal to the pid 0, using for instance

    kill(0, 2); /* SIGINT */
    

    that signal is sent to the entire process group, thus effectively killing the child.

    You can test it easily with something like:

    (cat && kill 0) | python
    

    If you then press ^D, you'll see the text "Terminated" as an indication that the Python interpreter have indeed been killed, instead of just exited because of stdin being closed.

提交回复
热议问题