Wikipedia says "A child process that terminates but is never waited on by its parent becomes a zombie process." I run this program:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
pid_t pid, ppid;
printf("Hello World1\n");
pid=fork();
if(pid==0)
{
exit(0);
}
else
{
while(1)
{
printf("I am the parent\n");
printf("The PID of parent is %d\n",getpid());
printf("The PID of parent of parent is %d\n",getppid());
sleep(2);
}
}
}
This creates a zombie process, but I can't understand why a zombie process is created here?
The output of the program is
Hello World1
I am the parent
The PID of parent is 3267
The PID of parent of parent is 2456
I am the parent
The PID of parent is 3267
The PID of parent of parent is 2456
I am the parent
....
.....
But why is it that the "child process terminates but is not waited on by its parent" in this case?
wait()
to read the child's exit status, and hence its entry is left behind in the process table. – Coprolitewait()
for the process. The zombie is dead, but still occupies a process table slot and will continue to do so until its parent waits for it, or the parent exits. If the parent exits, the child will be inherited by theinit
process (usually PID 1), and one of the main purposes of that process (if not the only one) is to wait for children to die. – Lazuliexit(0);
, thus terminating. The parent process enters an infinite loop without waiting for its child to die. In this context, waiting means 'calls one of the functionswait()
orwaitpid()
, or one of the various system-dependent alternatives such aswait3()
orwait4()
'. It doesn't just mean 'the parent goes on living'. When the child doesn't die, there is no possible zombie process; zombie processes are dead. – Lazuli