Search code examples
cprocesssignalsfreebsddefunct

Can a child process go <defunct> without its parent process dying?


kill - does it kill the process right away?

I found my answer and I set up a signal handler for SIGCHLD and introduced wait in that handler. That way, whenever parent process kills a child process, this handler is called and it calls wait to reap the child. - motive is to clear process table entry.

I am still seeing some child processes going for a few seconds even without its parent process dying. - how is this possible?

I am seeing this via ps. Precisely ps -o user,pid,ppid,command -ax and greping for parent process, child process and defunct.


Solution

  • A process goes defunct (zombie) immediately upon exiting (from a signal, call to exit, return from main, whatever). It stays zombie until wait'd on by its parent.

    So, all processes at least briefly become zombies upon exit.

    If the parent process takes a bit (because it was doing other work, or just because the scheduler hasn't given it CPU time yet) before calling wait, then you'll see the zombie for a bit. If the parent never calls wait, then when it eventually exits, init (pid 1) will adopt its zombied children, and call wait on them.