Maximum number of Zombie processes a system can handle in C++

C++Server Side ProgrammingProgramming

Given the task is to find the maximum number of Zombie processes that a system can handle or in other words, the program does not stop its execution.

A Zombie process (also known as defunct process) is a process that has completed its process via exit() (system call) but still has an entry in the process table.

Approach used in the below program as follows

  • Note that <unistd.h>should be added in order to run the program.

  • In main() function initialize num = 0 of type int which we will iterate until the program stops being executed.

  • To initiate a zombie process create a while statement with the condition − while( fork() > 0 )

    Fork() system call is used for initiating a new process known as the child process which run concurrently and make the fork() call (which is the parent process).

  • Inside the while loop increment num as well as print it.

Example

#include<iostream>
#include<unistd.h>
using namespace std;
int main(){
   int num = 0;
   while (fork() > 0){
      num++;
      cout<<num<<" ";
   }
}

Output

If we run the above code we will get the following output −

In the above output num stops incrementing at 93. But this number is not fixed and can vary depending upon the system configuration.

raja
Updated on 17-Aug-2020 09:21:18

Advertisements