What happens after the parent of the zombie process stops working?

I'm just curious what happens to the zombie process if the parent doesn't want to wait for him.

Suppose we have a parent and a child. The child is completed before the parent does this.

From APUE:

The kernel stores a small amount of information for each completion process ... Minimum
this information consists of the process identifier, process termination status ....

The parent should get this information using waitpid() .
But if, the parent leaves without waiting for the child, what happens:

Does the kernel delete this information (of course, this is useless)?
Or is he collecting this garbage?
Is this implementation specific?
Or is there a standard way to deal with this situation?

+4
source share
1 answer

Orphaned processes are automatically accepted by init , which has a standard SIGCHLD handler that simply discards any exit status of a dead process.

In your case, if the parent zombie process dies, the zombie orphan will be accepted by init and cleaned up.

The following code checks 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; } 
+6
source

Source: https://habr.com/ru/post/1487478/


All Articles