ishan3243
ishan3243

Reputation: 1928

Is it a zombie?

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?

Upvotes: 1

Views: 179

Answers (3)

user2801317
user2801317

Reputation:

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.

Upvotes: 1

Gangadhar
Gangadhar

Reputation: 10516

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.

Upvotes: 1

devnull
devnull

Reputation: 123448

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.

Upvotes: 3

Related Questions