Can somebody please explain when parent process receives the exit status of a dead child process via wait, who actually reallocates the memory of the child process and removes it from the process table?

2 Answers
2

Kernel assumes that the parent process is interested in knowing the result of the child process that it forked. First the child process sends to parent SIGCHLD signal. Then parent calls one of the wait functions to retrieve return status from child.

If the return status is not retrieved, child remains a zombie. However if the parent process exists before the child process, the child is adopted by init, which collects the status immediately, effectively removing the zombie process.

thank You, but what if the parent process doesn't exist before the child process, and father received status value, who will remove ZOMBIE? also init?
–
macindowsJun 29 '11 at 12:33

@macindows: Yes. If the parent process doesn't exist, responsibility passes to its parent, then that process's parent, and so on. If no other ancestor process currently exists, it will end up with init.
–
Dave SherohmanJun 29 '11 at 14:43

Manipulating the process table and the memory mappings is always the kernel's job. The kernel acts when some process makes a system call. When a process exits, all of the resources that it uses, including memory, except for the entry in the process table, are deleted − that's what the _exit system call does. Then, when the parent process calls wait or waitpid, part of that system call's job is to clean up the process table entry. The parent process may decide to call wait whenever it wants (if the parent is init, it calls wait pretty much all the time).