I am trying some simple code on fork. When I give code like this, it works fine. It will print
I am the child
I am the parent
and then waits for 30 seconds. I understand this is due to switching between these two process. First child executes then parent and then child…
#include<stdio.h>
#include<stdlib.h>
main()
{
int pid;
pid=fork();
if(pid==0)
{
printf("\nI am the child\n");
sleep(30);
exit(0);
}
if(pid>0)
{
printf("\nI am the parent\n");
wait();
}
}
But when I gave like (without wait in parent)
#include<stdio.h>
#include<stdlib.h>
main()
{
int pid;
pid=fork();
if(pid==0)
{
printf("\nI am the child\n");
sleep(30);
exit(0);
}
if(pid>0)
{
printf("\nI am the parent\n");
}
}
it just prints
I am the child
I am the parent
and exits ( no waiting for 30 seconds).
So is it because without wait call parent exits and child still executing? But why is it not showing up in terminal (the waiting)?
Whether parent becomes zombie here?
Your observations are correct.
The terminal waits for the original processes (which is the parent) to exit. It doesn’t wait for any child processes to exit.
Zombies: A process is a zombie if it has exited but its parent has not called wait() on it.
In your case, the parent does not become a zombie because the terminal is waiting for it.