I have just started learning about fork and wait in Linux and came across this paragraph in the wait() manual page notes:
A child that terminates, but has not been waited for becomes a "zombie". The kernel maintains a minimal set of information about the zombie process (PID, termination status, resource usage information) in order to allow the parent to later perform a wait to obtain information about the child. As long as a zombie is not removed from the system via a wait, it will consume a slot in the kernel process table, and if this table fills, it will not be possible to create further processes. If a parent process terminates, then its "zombie" children (if any) are adopted by init(8), which automatically performs a wait to remove the zombies.
A question that came to mind after reading this:
Isn't the fact that not using wait() causes a resource waste until the parent terminates, a problem that amplifies when the parent process is meant to be a long lived process in the system? Does this means I should always use wait() as soon as possible after using fork?
wait()is to learn about how the child terminated; it should return exit code 0 on success and greater than 0 if something went wrong. If your parent process creates many children during its run time then, indeed, you should also callwait()to avoid wasting system resources; if you fail doing this, sooner or later your process won't be able to create new children and, even worse, the system itself will start working abnormally. - axiacwait()does), you should still do it at some point to free a process slot. - Aakash Jain