:: Re: [DNG] Reaping orphan processes.
Top Page
Delete this message
Reply to this message
Author: Hendrik Boom
Date:  
To: dng
Subject: Re: [DNG] Reaping orphan processes.
On Sat, Mar 04, 2023 at 10:30:53PM +0100, aitor wrote:
> 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.


Presumably because its parent might eventually issue the expected wait.

Should I presume it won't be handed over to PID1 until its parent dies?

But it will be handed over when that happens?

-- hendrik