Question

I have some doubt regarding the following code.

#include <stdio.h>
#include <sys/types.h>

int main(void)
{
    int pid=fork();
    if(pid==0) sleep(5);
    printf("Hello World %d %d\n",getpid(),pid);
    if(pid>0) while(1){sleep(1);}
    if(pid==0) printf("In child process!\n");
    return 0;
}

Will the child process ever terminate? Will it remain in zombie state?

Was it helpful?

Solution

In your example, the child process dies but the parent doesn't know about it's exit status. As such, the child (now zombie) is left in the process table.

Moreover, the parent continues to wait for the child and keeps running.

OTHER TIPS

Yes it is zombie ... your Child process dies and your parent does not know the exit status of child process. and parent process is running even after child dies.

ZOMBIE process: when child process dies parent process still running. in this case parent process does not know the exit status of child.
To avoid this parent process uses wait or waitpid to know the child status.

Adding on to other responses.. If the opposite were to happen i.e. Parent dies before the child process completes, then the child would become an orphan and would later be "adopted" by a special system process called init.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top