Are child processes created with fork() automatically killed when the parent is killed?
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?
How to make child process die after parent exits?
Child can ask kernel to deliver SIGHUP
(or other signal) when parent dies by specifying option PR_SET_PDEATHSIG
in prctl()
syscall like this:
prctl(PR_SET_PDEATHSIG, SIGHUP);
See man 2 prctl
for details.
Edit: This is Linux-only
What happen when a child process encounter a wait instruction?
It will return -1
and set errno
to ECHILD
since the child process has no children of its own to wait on. From the wait(2) man page:
Return value
wait()
: on success, returns the process ID of the terminated
child; on error,-1
is returned....
Each of these calls sets
errno
to an appropriate value in the case of an error.Errors
ECHILD
(forwait()
)
The calling process does not have any unwaited-for children....
Note that the logic here is backwards from the norm:
if (pid > 0) {
execv(*argv, argv);
}
Here the parent process is calling execv()
. Usually it's the child that does so when pid == 0
, and it's the parent that waits.
What happens when first child process exits, then parent exits without calling wait?
It gets reparented (adopted) by init
. Yes, you'll see a zombie process until init
calls wait()
--but init
calls wait()
straightaway, so you're never going to catch the zombie process in practice. init
is very good about immediately reaping zombie processes reparented to it.
What happens to the child process, when the parent process calls an exec command
The pid of the parent process will remain the same after the exec
, so the process hierarchy won't be affected.
The problem with this approach is that the newly replaced parent process generally won't be aware that it has previously spawned a child and will not call wait
or waitpid
on it. This will cause the child process to become a zombie when it exits.
What if the child exits before the parent calls wait()?
But what if the kernel decides to schedule the child first and the
child process terminates before parent can call thewait()
?
It is a pretty possible case. If one of the wait
family functions is used by the parent or signal(SIGCHLD, SIG_IGN);
is called explicitly before fork
ing, it does not turn the child into a zombie even if the parent process is preempted(=not permitted to use CPU at that time).
Moreover, the need of wait or signal-ignorance mentioned is to clean process's unused datas. While using one of the methods, the kernel is told that the child(ren) process is not used anymore. So, you can cleanup unused system resources.
Related Topics
Shell Script to Get List of Defined Users on Linux
The Only Overhead Incurred by Fork Is Page Table Duplication and Process Id Creation
Why Does Utf-8 Text Sort in Different Order Between Os X and Linux
Print Bash Script Result Behind Prompt in The Next Line
Sudo/Apt-Get Command Not Found in Git Bash
Tk Initialization Failed: No Display Name and No $Display Environment Variable
Socket() Returns 0 in C Client Server Application
Git Clone Gnutls Recv Error (-9): a Tls Packet with Unexpected Length Was Received
Building Gcc with Glibc in a Non-Standard Location Without Root
What Is a Shell Command to Find The Longest Common Substring of Two Strings in Unix
Ffmpeg Img to Video = Could Find No File with Path
How to Read N-Th Line from a Text File in Bash
Setting The Umask of The Jenkins Process
System Calls: Difference Between Sys_Exit(), Sys_Exit and Exit()
Awk: Find and Replace in Certain Field Only
How to Get The Output of at Command in Current or Another Terminal Window