What is a zombie process?

When a process running on a computer system completes its execution, it is terminated and removed from the list of processes, freeing up the resources it was using. However, there can be a case where the process is still in the process table even after it has finished its execution. Such processes are called zombie processes.

Zombie process

A zombie process is a process that has finished its execution but is still in the process table, waiting for the parent to read its exit status and take it out from the process table.

How are they formed?

This can be seen in processes that have parent-child relations. After a child's process has finished execution, it sends an exit status to its parent and waits for the parent's acknowledgment. Until then, the child process remains in a zombie state.

Note: If there is no parent process to reap the child from the process table, it becomes the child of the init process.

Below, we can see a diagram that shows how processes become zombies.

Zombie process formation
Zombie process formation


A parent process creates a child process via the fork() system call. The parent finishes its execution before the child process and gets blocked. The child, when terminated, is now waiting for the parent to send an acknowledgment. Since the parent process is blocked, it doesn't send an acknowledgment. Hence, the child process becomes a zombie process.

Code example

Below is a code snippet showing how zombie processes are formed. In the code, the child process will become a zombie process as the parent will be asleep due to the sleep() system call. It will not collect the child's exit status upon its termination.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(){
    // create a child process
    int cpid = fork();
    // checking if child is created successfully
    if(cpid == -1){
        printf("Error creating child process!\n");
        exit(-1);
    }

    // code for child
    if(cpid == 0){
        // make the child process sleep for 1 second
        printf("\nChild: I have the pid: %d, my parents pid: %d\n", getpid(), getppid());
        sleep(1);
        exit(0);

    }   // code for parent
    else{
        printf("\nParent: I have the pid: %d, my childs pid: %d\n", getpid(), cpid);
        printf("\nParent: I am going to sleep!!\n");
        // make the parent process sleep for 4 seconds
        sleep(4);
        printf("\nParent: I have awoken!!\n");
        execlp("ps", "ps", "-l", NULL);
    }
    return 0;
}
Zombie process in C

When we run the program above, we observe that after the child process prints its exiting message and terminates. However, the parent did not collect its exit status because it was sleeping, making the child a zombie.

We also see a process table showing a process entry labeled a zombie. When we compare the pid of the zombie process in the table and the child process, we see that the child process is the zombie process.

Code explanation

  • Line 7: We call the fork() to create a new process called the child process.

  • Lines 9–12: We check if the fork() function returned a -1 value representing an error in creating a child process.

  • Lines 15–21: Here, we write the code for the child process, we print the PID of the child process and its parent process, and the child sleeps for 1 second via the sleep() system call, after which it exits using the exit() function.

  • Lines 22–29: Here, we can see the code for the parent process. We print the parent's PID and child process, and then the parent sleeps for 4 seconds via the sleep() system call to allow the child to finish execution and become a zombie. It then exits.

Note: To avoid making the child process a zombie process, we can add a wait() system call in the else part which will make the parent wait for the child to finish its execution and terminates it after receiving exit status from the child process.

Quiz

Now that we have learned about the zombie process and its formation, let's test our knowledge.

1

What is the parent PID of a zombie process?

A)

1

B)

0

C)

Can’t be determined.

Question 1 of 40 attempted

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved