Why is a zombie waiting for its child?
The zombie isn't waiting for its child. Like any zombie process, it stays around until its parent collects it.
You should display all the processes involved to understand what's going on, and look at the PPID as well. Use this command line:
ps -t $(tty) -O ppid,pgid
The parent of the process you're killing is cat
. What happens is that bash runs the background command cat <( sleep 100 & wait )
in a subshell. Since the only thing this subshell does is to set up some redirection and then run an external command, this subshell is replaced by the external command. Here's the rundown:
- The original bash (12126) calls
fork
to execute the background commandcat <( sleep 100 & wait )
in a child (14247).- The child (14247) calls
pipe
to create a pipe, thenfork
to create a child to run the process substitutionsleep 100 & wait
.- The grandchild (14248) calls
fork
to runsleep 100
in the background. Since the grandchild isn't interactive, the background process doesn't run in a separate process group. Then the grandchild waits forsleep
to exit.
- The grandchild (14248) calls
- The child (14247) calls
setpgid
(it's a background job in an interactive shell so it gets its own process group), thenexecve
to runcat
. (I'm a bit surprised that the process substitution isn't happening in the background process group.)
- The child (14247) calls
- You kill the grandchild (14248). Its parent is running
cat
, which knows nothing about any child process and has no business callingwait
. Since the grandchild's parent doesn't reap it, the grandchild stays behind as a zombie. - Eventually,
cat
exits — either because you kill it, or becausesleep
returns and closes the pipe socat
sees the end of its input. At that point, the zombie's parent dies, so the zombie is collected by init and init reaps it.
If you change the command to
{ cat <( sleep 100 & wait ); echo done; } &
then cat
runs in a separate process, not in the child of the original bash process: the first child has to stay behind to run echo done
. In this case, if you kill the grandchild, it doesn't stay on as a zombie, because the child (which is still running bash at that point) reaps it.
See also How does linux handles zombie process and Can a zombie have orphans? Will the orphan children be disturbed by reaping the zombie?
Zombie is not waiting for the child. Instead, zombie is the process that already died (by its own, or was killed - as in your example), had its code, data and stack deallocated, and now only contains its exit code, waiting for its parent to call wait(2)
to retrieve it (and thus finally clean process entry completly from the process table)
In your example, when sleep finishes (or is killed), parent will read the exit statuses, and reap the zombies. See above mentioned wait(2)
for details.