Can a <defunct> child process die without its parent process?
kill - does it delete the process right now?
I found my answer, and I set up a signal handler for SIGCHLD and introduced wait in this handler. Thus, whenever a parent kill process executes a child process, this handler is called and calls wait to reap the child. - The motive is to clear the process table entry.
I can still see how some child processes last several seconds, even if its parent process does not die. - how is this possible ?
I see it through ps . Exactly ps -o user,pid,ppid,command -ax and greping for the parent process, child process and nonexistent.
The process disappears (zombies) immediately after exiting (from the signal, exit is called, returns from main , whatever). He remains a zombie until wait from his parent.
So, all processes, at least for a short time, become zombies at the exit.
If the parent process takes a bit (because it was doing other work, or just because the scheduler hadn't given it CPU time yet) before calling wait , you'll see a little zombie. If the parent never calls wait , then when it finally exits, init (pid 1) will accept its zombie children and call wait on them.
The child process ceases to function (it becomes a zombie) only when its parent process has not died and has not yet waited for it. If the original parent died, then the parent becomes the identifier of process 1, and the main task of the process is to wait until his (inherited) children die, and remove them from the list of processes so that they are not zombies. (Note: a lost child or demon is automatically inherited by PID 1, it does not receive attachment to grandparents according to the hierarchy of processes.)
Meanwhile, when the child dies and the parent collects exit information through wait() (or waitpid() or waitid() or any other option), this is a zombie in the process list, shown as defunct on ps .
But answer your question:
- Yes, a process can disappear without its parental dying.
(And he can disappear only if his parent is not dead.)