Hi
I tried to create a zombie process with the following program:
int main(void)
{
pid_t pid;
int status;
if ((pid = fork()) < 0)
perror("fork error");
else if (pid == 0){ /* child process*/
exit(0);
}
printf("child process ID: %d\n", pid);
sleep(10);
return 0;
}
I can observe the "Z" state with the ps command, but this zombie process (the child process) only exists in the duration from its termination to its parent termination. I don't wait() the child process in the parent, so why doesn't the zombie process exist after the parent terminates?
In <apue2>,
My understanding is this: by the time the parent terminates, if there are child processes already terminated and still running, init will adopt the running ones, not the already terminated ones. (don't the "active" and "still exists" in apue2 mean this?) So a zombie child process won't be adopted by init. In my case, by the time the parent terminates, the child is not "active" and won't be adopted by init.
Besides, the child process in my program disappears immediately after the parent terminates. As I described, I don't think this is done by init, then who did?
A zombie process is a process that has completed execution but still has an entry in the process table. This entry is still needed to allow the process that started the (now zombie) process to read its exit status. The term zombie process derives from the common definition of zombie (an undead person)
In the term's colourful metaphor, the child process has died but has not yet been reaped.
Zombies can be identified in the output from the UNIX ps command by the presence of a �Z� in the �STAT� column. Zombies that exist for more than a short period of time typically indicate a bug in the parent program, the presence of a few zombies is not worrisome in itself, but may indicate a problem that would grow serious under heavier loads. Since there is no memory allocated to zombie processes except for the process table entry itself, the primary concern with many zombies is not running out of memory, but rather running out of process ID numbers.
To remove zombies from a system, remove the parent process. When a process loses its parent, init becomes its new parent. Init periodically executes the wait system call to reap any zombies with init as parent.
Init will adopt only those processes for which there is no parent process that is currently alive (or a slot in the process table entry) is no more, but in your case, child has exited first and it enters zombie state for the parent to collect the status of the created child process. Since the parent process of the child is alive very much ( in sleep mode ) init cannot adopt that, and once the parent process terminates, child stats is collected and there is no need for the child process to remain as zombie and it becomes completely relieved.