:: Re: [DNG] Reaping orphan processes.
Forside
Slet denne besked
Besvar denne besked
Skribent: aitor
Dato:  
Til: dng
Emne: Re: [DNG] Reaping orphan processes.
Hi Hendrik,

On 4/3/23 18:44, Hendrik Boom wrote:
> Why can't they just terminate by themselves?


Consider the following example:

/* ---------- example.c ------------ */

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

int main()
{
    int pid;

    pid=fork();
    if(pid==0)
    {
        printf("\nI am the child\n");
        sleep(60);
        return 0;
    }
    if(pid>0)
    {
        while(1)
            printf("\nI am the parent\n");
    }

    return 0;
}

/* ---------------------- */

Compile with `gcc example.c -o example` and run it.

Now get the pid of both the parent and the child. Say:

$ pidof example

30260  30259

During the first 60 seconds you'll find the following line in "/proc/30260/status":

    State:    Z (sleeping)


that will be turned into:

    State:    Z (zombie)


at the end of the delay period. In this example, the child became a zombie process
because it exited but its parent is still alive and has not called wait() on it.

In this scenario, if you run the `ps` command you'll still find both processes; that is,
the original process (which is the parent spawned by the shell) and its child:

$ ps -A | grep example
30259 pts/1    00:00:36 example
30260 pts/1    00:00:00 example <defunct>

However, you'll not be able to terminate the zombie process by sending a signal to it.

HTH,

Aitor.