Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i prevent a child process to NOT become a zombie if parent exits

Tags:

c

linux

My main process spawns a child process. If the main process is killed, the child would be assigned ppid of 1. When the child exits, it would become a zombie as init has not called wait() on this child. Is there a way to avoid this situation?

like image 619
Jimm Avatar asked Dec 04 '25 17:12

Jimm


1 Answers

init will call wait() on processes that it inherits. Zombies should only exist where the child has exited but the parent is still around, but is yet to reap the exit code. From the init manpage:

init is the parent of all processes on the system, it is executed by the kernel and is responsible for starting all other processes; it is the parent of all processes whose natural parents have died and it is responsible for reaping those when they die.

You should make a distinction between orphans (they're still alive, their parent is dead and hence they've been adopted by init), and zombies (they're dead but their parent is alive and has not yet reaped them).

Orphans will become zombies for a very short period of time after they exit but before init reaps them but this period should be small enough that no-one notices. In fact, all exiting processes (except possibly init itself) go through this short zombie phase, it's only those cases where the parent doesn't reap quickly enough that you notice.


The actual code in the init child death (SIGCHLD) handler goes something like this (my comments):

void chld_handler (int sig) {
    CHILD  *ch;
    int    pid, st;
    int    saved_errno = errno;

    while ((pid = waitpid(-1, &st, WNOHANG)) != 0) { // << WAIT done here
        if (errno == ECHILD) break;
        for (ch = family; ch; ch = ch->next) {
            if (ch->pid == pid && (ch->flags & RUNNING)) {
                INITDBG (L_VB, child_handler: marked %d as zombie", ch->pid);
                ADDSET (got_signals, SIGCHLD);
                ch->exstat = st;
                ch->flags |= ZOMBIE;                 // Set to zombie here.
                if (ch->new) {
                    ch->new->exstat = st;
                    ch->new->flags |= ZOMBIE;
                }
                break;
            }
        }
        if (ch == NULL) {
            INITDBG (L_VB, "chld_handler: unknown child %d exited.", pid);
        }
    }
    errno = saved_errno;
}

And then, later on in the main loop (not signal handler), any processes in the process table marked as ZOMBIE are cleaned up:

if (ISMEMBER (got_signals, SIGCHLD)) {
    INITDBG(L_VB, "got SIGCHLD");
    /* First set flag to 0 */
    DELSET(got_signals, SIGCHLD);

    /* See which child this was */
    for (ch = family; ch; ch = ch->next) {
        if (ch->flags & ZOMBIE) {                    // ZOMBIE detected here
            INITDBG (L_VB, "Child died, PID= %d", ch->pid);
            ch->flags &= ~(RUNNING|ZOMBIE|WAITING);  // And cleared here.
            if (ch->process[0] != '+') {
                write_utmp_wtmp ("", ch->id, ch->pid, DEAD_PROCESS, NULL);
            }
        }
    }
}
like image 162
paxdiablo Avatar answered Dec 06 '25 08:12

paxdiablo