1

I am trying to create a zombie process by understanding online solutions

but still I can't find any zombie process using the following code

I don't know what went wrong in my code

I am trying pstree -p and top command to find my zombie process but I can't find any

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


int main(int argc, char const *argv[])
{
    pid_t childPID;

    childPID = fork();

    if(childPID < 0)
    {
        printf("child Process creation failed\n");
    }
    else if(childPID == 0)
    {

        printf("I am child Process.My pid is: %d\n", getpid());

        exit(0);

        sleep(100);
    }
    else
    {
        sleep(10);


        printf("I am parent process.my pid: %d\n", getpid());
        printf("My child process is: %d\n",childPID);
    }

    return 0;
}

enter image description here

enter image description here

SHAH MD IMRAN HOSSAIN
  • 2,558
  • 2
  • 25
  • 44
  • What is the purpose of the 'sleep(100);' line? – Martin James Mar 05 '18 at 05:11
  • By theory, I know that that, child process will exit after exit(0) instruction. – SHAH MD IMRAN HOSSAIN Mar 05 '18 at 05:14
  • but not finding any zombie process, I wrote that line **sleep(100)**. actually it's a result of frustration :) – SHAH MD IMRAN HOSSAIN Mar 05 '18 at 05:16
  • 2
    A zombie process is one that has exited (so it is no longer running; it is dead) but the parent of the zombie has not yet executed a `wait()` or `waitpid()` that collected the zombie's exit status information (so it's process slot is still needed; it is partially alive — hence "zombie"). – Jonathan Leffler Mar 05 '18 at 05:33
  • @JonathanLeffler i understand the theory but can't the fact what went wrong in the code. i tried but it still same – SHAH MD IMRAN HOSSAIN Mar 05 '18 at 05:45
  • @JonathanLeffler and yeah i check the linked post to my post , and still i cant understand the code in solution – SHAH MD IMRAN HOSSAIN Mar 05 '18 at 05:55
  • 2
    Your `sleep(100);` is dead code; it will never be executed. Your `sleep(10);` is not in the best place. You only tell the rest of the world which process is the zombie after the sleep (for 10 seconds) and just before your parent process exits. When the parent exits, the zombie child is cleaned up by the system. Move the `sleep(10);` after the printing. And then consider replacing it with `pause();` which does nothing indefinitely, until the process is signalled (interrupted, usually). That gives you plenty of time to go and have lunch and chase the zombie before the parent dies. – Jonathan Leffler Mar 05 '18 at 05:56
  • @JonathanLeffler by using **pause()** i found the zombie process in **pstree** but still has **no existence** on **top** table – SHAH MD IMRAN HOSSAIN Mar 05 '18 at 08:39
  • @JonathanLeffler Also, that can be accomplished using a while loop instead of pause(),i use **while(1);** statement, but there is no sign of zombie process on top table , but it exist in pstree – SHAH MD IMRAN HOSSAIN Mar 05 '18 at 08:42
  • 1
    Why would you expect `top` to report on dead processes? At the least, zombies are not using any CPU time, so they'd be down at the very bottom of any list from `top`. Yes, you can use `while (1) ;` instead of `pause()`, but using `pause()` stops the process from chewing up all the resources of a CPU and lets the system get on with useful work. Of course, it also means it drops down the list of active processes in `top`; that's OK in my book because it isn't doing anything useful except causing the child to look like a zombie. – Jonathan Leffler Mar 05 '18 at 10:21

1 Answers1

3

Well I can see the zombie process while the parent is still running

enter image description here

but when the parent dies, both processes disappear. My guess is that once the parent process dies, init (or whatever process that runs with pid 1) waitpids for the zombie thus removing it from the process list.

See also Create zombie process

Pablo
  • 13,271
  • 4
  • 39
  • 59