Hi
I tried to create a zombie process with the following program:
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?Code: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; }
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.But what happens if the parent terminates before the child? The answer is
that the init process becomes the parent process of any process whose
parent terminates. We say that the process has been inherited by init. What
normally happens is that whenever a process terminates, the kernel goes
through all active processes to see whether the terminating process is the
parent of any process that still exists.
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?