Prerequisite: Zombie and Orphan Processes in C
A zombie process is a process that has completed execution but still has an entry in the process table. This entry is still needed to allow the parent process to read its child’s exit status.
A process that terminates cannot leave the system until its parent accepts its return code. If its parent process is already dead, it’ll already have been adopted by the “init” process, which always accepts its children’s return codes. However, if a process’s parent is alive but never executes a wait ( ), the process’s return code will never be accepted and the process will remain a zombie.
An orphan process is a process that is still executing, but whose parent has died. They do not become zombie processes; instead, they are adopted by init (process ID 1), which waits on its children.
When a parent dies before its child, the child is automatically adopted by the original “init” process whose PID is 1.
In the following code, we have made a scenario that there is a parent and it has a child and that child also has a child, firstly if our process gets into child process, we put our system into sleep for 5 sec so that we could finish up the parent process so that its child become orphan, then we have made a child’s child as zombie process, the child’s child finishes its execution while the parent(i.e child) sleeps for 1 seconds, hence the child’s child doesn’t call terminate, and it’s entry still exists in the process table.
Below is the implementation of above approach:
Note: Above code may not work with the online compiler as fork() is disabled.
- Zombie and Orphan Processes in C
- Double forking to prevent Zombie process
- Zombie Processes and their Prevention
- OpenCV C++ Program to create a single colored blank image
- Execute both if and else statements in C/C++ simultaneously
- fork() to execute processes from bottom to up using wait()
- Create n-child process from same parent process using fork() in C
- How to convert a single character to string in C++?
- Implementing Iterator pattern of a single Linked List
- Communication between two process using signals in C
- Get/Set process resource limits in C
- MCQ on Memory allocation and compilation process
- Creating multiple process using fork()
- What is the difference between single quoted and double quoted declaration of char array?
- Exit status of a child process in Linux
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.