Home » Linux » Are child processes created with fork() automatically killed when the parent is killed?

Are child processes created with fork() automatically killed when the parent is killed?

Posted by: admin November 29, 2017 Leave a comment

Questions:

I’m creating child processes with fork() in C/C++.
When the parent process ends (or is killed for some reason) I want all child processes to be killed as well.
Is that done automatically by the system? Or I have to do it myself?

Thanks.


Pre-existing similar questions:

Answers:

No. If the parent is killed, children become children of the init process (that has the process id 1 and is launched as the first user process by the kernel).

The init process checks periodically for new children, and kills them if they have exited (thus freeing resources that are allocated by their return value).

The question was already discussed with quality answers here:
How to make child process die after parent exits?