Table of contents
1.
Introduction
2.
Zombie Processes
2.1.
Working of Zombie Process
3.
Dangers of the Zombie Process
4.
Preventing the Zombie Processes
4.1.
Using wait() system call
4.2.
Ignoring the SIGCHLD signal
4.3.
Using the signal handler
5.
Killing the Zombie Process
6.
SIGHLD Signal
7.
Frequently Asked Questions
7.1.
What problems can zombie processes cause?
7.2.
Is it true that zombie processes consume resources?
7.3.
When the parent process in Linux is destroyed, what happens to the child process?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

Zombie Processes and their Prevention

Author Ankit Kumar
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Operating Systems

Introduction

A process is said to be a Zombie process that, even after completing its execution, still has an entry in the process table to report to its parent process. The article will discuss the Zombie process, reasons why we should prevent zombie processes, and different ways to avoid Zombie Processes.

Also See Topic, procedure call in compiler design, Multiprogramming vs Multitasking"
 

Zombie Processes

A zombie process, also known as a defunct process, has completed execution but still enters the process table. This is true for child processes, where the entry is still required for the parent process to read the exit state of its child. Once the zombie's entry in the process table is removed, it is stated to be "reaped" once the exit status is obtained via the wait system function. Before being deleted from the resource table, a child process must first become a zombie.

In most cases, zombies are promptly cared for by their parents before being harvested by the system as part of routine system operation. Long-running zombie processes are usually mistakes and resource leaks, but they take up space in the process table.

The kid process has died but is not yet reaped. In addition, unlike normal operations, a zombie process is unaffected by the kill command.

Orphan processes are not to be confused with zombie processes. An orphan process is still running but has lost its parent. When a parent dies, init takes over the orphaned child procedure (process ID 1). Orphan processes do not become zombie processes when they die; instead, init wait for them. As a result, a process that is both a zombie and an orphan will immediately reap the benefits.

Working of Zombie Process

A zombie process in an operating system operates as follows:

  1. When a process exits, all associated memory and resources are deallocated so that other processes can use them.
  2. The process's entry in the process table, however, persists. The parent can get the exit status of their child by using the wait system call, which removes the zombie. The wait call can be used in sequential code, but it is most typically used as a handler for the SIGCHLD signal, sent to the parent when a child dies.
  3. After removing the zombie, the process identifier (PID) and entry in the process table can be reused. If a parent does not call the wait, the zombie will remain in the process table, resulting in a resource leak. In some circumstances, this may be good, and the parent process wishes to keep this resource. For example, if another child process is created by the parent, it will not be given the same PID.
  4. Modern UNIX-like systems are subject to the following particular situation. All child exit status information is destroyed, and no zombie processes are left if the parent expressly ignores SIGCHLD by setting its handler to SIG_IGN (rather than merely ignoring the signal by default) or has the SA_NOCLDWAIT option set.
  5. If a "Z" is present in the STAT column in the output of the UNIX "ps" program can be used to identify zombies. Zombies lasting longer than a few minutes are usually the result of a glitch in the parent software or an unusual decision not to reap the child processes.
  6. The KILL COMMAND can manually send the SIGCHLD signal to the parent to eliminate zombies from a system. If the zombie is still not reaped by the parent process, and if terminating the parent process is acceptable, the parent process can be removed. When a process's parent dies, init takes over as the new parent. Init uses the wait system function to regularly reap any zombies that have init as a parent.
#include <sys/wait.h>  

#include <stdlib.h>  

#include <unistd.h>  

int main(void)  

{  

    pid_t pids[10];  

    int i;  

      for (i = 9; i >= 0; --i) {  

        pids[i] = fork();  

        if (pids[i] == 0) {  

            printf("Child%d\n", i);  

            sleep(i+1);  

            _exit(0);  

        }  

    }  

      for (i = 9; i >= 0; --i) {  

        printf("parent%d\n", i);  

        waitpid(pids[i], NULL, 0);  

    }  

      return 0;  

}  
You can also try this code with Online C Compiler
Run Code

OUTPUT

Output

Dangers of the Zombie Process

Although zombie processes do not consume any system resources, they do save their process ID. The presence of a lot of zombie processes will take up all of the available process IDs. Other operations are unable to run because no process IDs are provided.

If their parent processes are no longer executing, zombie processes may signal an operating system issue. If there exist only a few zombie processes, this isn't a big deal, but it might cause problems for the system when under a lot of stress.

Preventing the Zombie Processes

Because each system has only one process table and the size of the process table is limited, we must avoid producing the Zombie process. The process table will be complete if too many zombie processes are spawned. The system will not generate any new methods, and it will then come to a complete halt. As a result, we must avoid the emergence of zombie processes. Here are some of the strategies to prevent zombies from being created, including

Using wait() system call

After generating a child, the parent process will call wait() to wait for the child to complete and return its exit status. The parent process remains suspended until the child process is terminated (staying in a queue). It's important to understand that the parent process does nothing during this time and waits.

#include<stdio.h>  

#include<unistd.h>  

#include<sys/wait.h>  

#include<sys/types.h>  

int main()  

{  

    int i;  

    int pid = fork();  

    if (pid==0)  

    {  

        for (i=0; i<20; i++)  

            printf("I am Child\n");  

    }  

    else  

    {  

        wait(NULL);  

        printf("I am Parent\n");  

        while(1);  

    }  

}  
You can also try this code with Online C Compiler
Run Code

OUTPUT

Output

Ignoring the SIGCHLD signal

The parent is sent a SIGCHLD signal when a child is terminated. When we call 'signal(SIGCHLD, SIG_IGN),' the system ignores the SIGCHLD signal and deletes the child process record from the process table. As a result, no zombies are formed. In this scenario, the parent is unaware of the child's exit status.

#include<stdio.h>  

#include<unistd.h>  

#include<sys/wait.h>  

#include<sys/types.h>     

int main()  

{  

    int i;  

    int pid = fork();  

    if (pid == 0)  

        for (i=0; i<20; i++)  

            printf("I am Child\n");  

    else  

    {  

        signal(SIGCHLD,SIG_IGN);  

        printf("I am Parent\n");  

        while(1);  

    }  

}  
You can also try this code with Online C Compiler
Run Code

OUTPUT

Output

Using the signal handler

The parent process installed a signal handler for the SIGCHLD signal. Within it, the signal handler calls the wait() system call. When the kid is terminated, the SIGCHLD is delivered to the parent in this circumstance. The corresponding handler is invoked when SIGCHLD is received, and it calls the wait() system call. As a result, the parent gets the exit status right away, and the child's record in the process table is cleared. As a result, no zombies are formed.

#include<stdio.h>  

#include<unistd.h>  

#include<sys/wait.h>  

#include<sys/types.h>     

void func(int signum)  

{  

    wait(NULL);  

}

int main()  

{  

    int i;  

    int pid = fork();  

    if (pid == 0)  

        for (i=0; i<20; i++)  

            printf("I am Child\n");  

    else  

    {  

        signal(SIGCHLD, func);  

        printf("I am Parent\n");  

        while(1);  

    }  

} 
You can also try this code with Online C Compiler
Run Code

OUTPUT

Output

Killing the Zombie Process

The kill command is used to kill zombie processes by delivering the SIGCHLD signal to the parent. This signal instructs the parent process to use the wait() system function to clear up the zombie process. With the kill command, this signal is issued. As an example, consider the following:

kill -s SIGCHLD pid. 

The PID in the preceding command is the parent process's process ID

SIGHLD Signal

SIGCHLD is a UNIX and UNIX-like system signal. siginfo_t has the following code value:

  1. The child process has come to a halt. CLD_EXITED
  2. The child process has come to an abrupt end (without core) CLD_KILLED
  3. The child process has come to a short end (with core) CLD_DUMPED
  4. The child process is locked in CLD_TRAPPED because it is being tracked.
  5. CLD_STOPED The kid procedure has come to an end.
  6. CLD_CONTINUED The paused child process has continued. When an operation completes or comes to a halt. It signals its parent process with a SIGCHLD signal. This signal will be ignored by default. The parent process should catch this signal if it wants to be informed about the state of its subsystem. The signal capture function usually calls the wait function to get the process ID and termination status.

 

Must Read Evolution of Operating System and Open Source Operating System.

Frequently Asked Questions

What problems can zombie processes cause?

Although one or two zombie processes won't cause any problems or slow down your computer, a significant number of them can interrupt the workflow of your system by overloading the process table and resources.

Is it true that zombie processes consume resources?

Zombie processes do not use system resources. (Actually, each one stores its process description in a minimal amount of system memory.) On the other hand, each zombie process keeps its process ID (PID). On 32-bit Linux systems, the default number of process IDs is 32767.

When the parent process in Linux is destroyed, what happens to the child process?

When a parent passes away, init takes over the orphaned child process (process ID 1). Orphan processes do not become zombies when they die; instead, init wait for them. As a result, a process that is both a zombie and an orphan will automatically reap its benefits. 

Conclusion

Ninjas, with this, we come to the end of the topic. 

In the article, we discussed the Zombie Processes in detail. We saw that A process is said to be a Zombie process that, even after completing its execution, still has an entry in the process table to report to its parent process. After that, we saw the working of a Zombie Process and the dangers that Zombie processes can possess. After checking on the risks, we saw ways that may help us to prevent the Zombie Processes. We also learned about the kill command for Killing the Zombie Process. And then, we saw the UNIX system SIGHLD signal.

Recommended Reading:

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Live masterclass