Operating System | Zombie process - cook the code

Monday, 1 January 2018

Operating System | Zombie process

Zombie process 
A process which has finished the execution but still has entry in the process table to report to its parent process is known as a zombie process. 

Image result for zombies process

parent process suppose to execute WAIT system call which reads dead child process status and other details. Once wait system call completes, dead child will be removed from memory. At this point, if parent process is not coded properly or unable to read this status from child for some reason






(If a child is not waited on by its parent, it continues to consume this resource indefinitely, and thus is a resource leak. Such situations are typically handled with a special "reaper" process that locates zombies and retrieves their exit status, allowing the operating system to then deallocate their resources.)
In the following code, the child finishes its execution using exit() system call while the parent sleeps for 50 seconds, hence doesn’t call wait() and the child process’s entry still exists in the process table.
// A C program to demonstrate Zombie Process.
// Child becomes Zombie as parent is sleeping
// when child process exits.
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
    // Fork returns process id
    // in parent process
    pid_t child_pid = fork();
    // Parent process
    if (child_pid > 0)
        sleep(50);
    // Child process
    else       
        exit(0);
    return 0;
}

Understanding fork() system call:-

No comments:

Post a Comment