C program to demonstrate zombie process

C program for zombie process: In this post, we are going to implement program for zombie process.
Submitted by Hritik Raj, on July 26, 2018

Zombie process

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

In the following code, you can see that the parent will sleep for 20 sec, so it will complete its execution after 20 sec. But, Child will finish its execution using exit() system call while its parent process has gone for sleep.

After execution the child must report to its parent, So the child process entry has to be in the process table to report to its parent even after it has finished execution.

Note: fork() is a UNIX system call so following program will work only on UNIX based operating systems.

The following code will not produce any output. It is just for demonstration purpose.

Program for zombie process in C

#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    // fork() creates child process identical to parent
    int pid = fork();

    // if pid is greater than 0 than it is parent process
    // if pid is 0 then it is child process
    // if pid is -ve , it means fork() failed to create child process

    // Parent process
    if (pid > 0)
        sleep(20);

    // Child process
    else {
        exit(0);
    }

    return 0;
}

Reference: Zombie and Orphan Processes in C

C Advance Programs »



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.