Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens after the parent of zombie process terminates?

I'm just curious, what happens to zombie process, if it's parent doesn't care to wait for it.

Suppose, we've a parent and a child. Child terminates before parent does.

From APUE:

The kernel keeps a small amount of information for every terminating process...Minimally
this information consists of the process ID, the termination status of the process....

Parent is required to fetch this information using waitpid().
But if, parent exits without waiting for child, what happens:

Does the kernel delete this information (certainly it's of no use)?
Or, it keeps collecting this junk?
Is this implementation specific?
Or, is there a standard way to deal with this situation?

like image 477
mohit Avatar asked Jun 21 '13 13:06

mohit


People also ask

What happens if the parent of a zombie child terminates?

Orphan processes are automatically adopted by init which has a standard SIGCHLD handler that just discards any exit status of a dead process. In your case if the parent of a zombie process dies the zombie orphan will be adopted by init and cleaned up.

What happens when a parent process is terminated?

In most cases, if a parent process is terminated then its child processes are also terminated. This is done because the child process cannot exist without the parent process. If a process requires more memory than is currently available in the system, then it is terminated because of memory scarcity.

How zombie processes are terminated?

The zombie processes can be removed from the system by sending the SIGCHLD signal to the parent, using the kill command. If the zombie process is still not eliminated from the process table by the parent process, then the parent process is terminated if that is acceptable.

What happens when the parent process terminated before the child process?

When a parent process dies before a child process, the kernel knows that it's not going to get a wait call, so instead it makes these processes "orphans" and puts them under the care of init (remember mother of all processes). Init will eventually perform the wait system call for these orphans so they can die.


1 Answers

Orphan processes are automatically adopted by init which has a standard SIGCHLD handler that just discards any exit status of a dead process.

In your case if the parent of a zombie process dies the zombie orphan will be adopted by init and cleaned up.

The following code tests this:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>


int main() {
    pid_t child_pid;
    if (child_pid = fork()) { // fork a child, child will exit straight away
        char name[128];
        sprintf(name, "/proc/%d/stat", child_pid);
        char line[2048];

        // read childs /proc/pid/stat, field 3 will give its status
        FILE * fp = fopen(name, "r");

        while (fgets(line, sizeof(line), fp))
            puts(line);

        fclose(fp);

        usleep(5000000);

        // by now the child will have exited for sure, repeat
        fp = fopen(name, "r");

        while (fgets(line, sizeof(line), fp))
            puts(line);

        fclose(fp);

        // make another child to repeat the process and exit the parent
        if (!fork()) {
            usleep(5000000);
            // both parent and child will have exited by now

            fp = fopen(name, "r");

            // this should fail because init has already cleaned up the child
            if (!fp) {
                perror("fopen");
                return -1;
            }

            while (fgets(line, sizeof(line), fp))
                puts(line);

            fclose(fp);
        }

    }

    return 0;
}
like image 53
Sergey L. Avatar answered Nov 14 '22 22:11

Sergey L.