Search code examples
csignalsfreebsdzombie-processsigchld

An unreaped child and its future


So when a child dies parent gets SIGCHLD but if parent dies before doing wait(), the child is reparented to init. At this point in time the child is a zombie i.e. <defunct>.

What happens next?

Does init do wait() on that child? if yes, when does it do it? Any guarantees regarding time-limit?


Solution

  • Yes, init will immediately reap all children. If you make a process which ignores SIGCHLD and accumulates many zombies you can get rid of them by killing that parent via the mechanism you describe.

    For reference here is the main loop of init while in multi user mode. requested_transition is set by signalling init (e.g. the classic kill -1 1 to read an updated inittab):

        while (!requested_transition)
                if ((pid = waitpid(-1, (int *) 0, 0)) != -1)
                        collect_child(pid);
    

    (collect_child handles cases where init has some special interest in the process, such as a login shell which now needs a new getty)