Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?


Pre-existing similar questions:

  • How can I cause a child process to exit when the parent does?
  • How to make child process die after parent exits?
like image 618
GetFree Avatar asked Dec 28 '08 05:12

GetFree


People also ask

What happens when you fork a child process?

When a process calls fork, it is deemed the parent process and the newly created process is its child. After the fork, both processes not only run the same program, but they resume execution as though both had called the system call.

What happens to child process when parent is killed Windows?

In chrome on Windows, the child processes are in a job object and so the OS takes care of killing them when the parent process dies.

What does the fork () return to the child process?

RETURN VALUE Upon successful completion, fork() returns 0 to the child process and returns the process ID of the child process to the parent process. Otherwise, -1 is returned to the parent process, no child process is created, and errno is set to indicate the error.

How does forking affect the parent process?

fork() creates a new process by duplicating the calling process. The new process is referred to as the child process. The calling process is referred to as the parent process. The child process and the parent process run in separate memory spaces.


1 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 waits for them (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?

like image 51
Johannes Schaub - litb Avatar answered Oct 03 '22 20:10

Johannes Schaub - litb