<s>
On	O
Unix	B-Application
and	O
Unix-like	B-Operating_System
computer	B-General_Concept
operating	I-General_Concept
systems	I-General_Concept
,	O
a	O
zombie	B-Operating_System
process	I-Operating_System
or	O
defunct	B-Operating_System
process	I-Operating_System
is	O
a	O
process	B-Operating_System
that	O
has	O
completed	O
execution	O
(	O
via	O
the	O
exit	B-Operating_System
system	B-Operating_System
call	I-Operating_System
)	O
but	O
still	O
has	O
an	O
entry	O
in	O
the	O
process	B-Operating_System
table	I-Operating_System
:	O
it	O
is	O
a	O
process	B-Operating_System
in	O
the	O
"	O
Terminated	O
state	O
"	O
.	O
</s>
<s>
This	O
occurs	O
for	O
the	O
child	O
processes	O
,	O
where	O
the	O
entry	O
is	O
still	O
needed	O
to	O
allow	O
the	O
parent	B-Operating_System
process	I-Operating_System
to	O
read	O
its	O
child	O
's	O
exit	B-Operating_System
status	I-Operating_System
:	O
once	O
the	O
exit	B-Operating_System
status	I-Operating_System
is	O
read	O
via	O
the	O
wait	B-Operating_System
system	B-Operating_System
call	I-Operating_System
,	O
the	O
zombie	B-Application
's	O
entry	O
is	O
removed	O
from	O
the	O
process	B-Operating_System
table	I-Operating_System
and	O
it	O
is	O
said	O
to	O
be	O
"	O
reaped	O
"	O
.	O
</s>
<s>
A	O
child	B-Operating_System
process	I-Operating_System
always	O
first	O
becomes	O
a	O
zombie	B-Application
before	O
being	O
removed	O
from	O
the	O
resource	O
table	O
.	O
</s>
<s>
In	O
most	O
cases	O
,	O
under	O
normal	O
system	O
operation	O
zombies	B-Application
are	O
immediately	O
waited	O
on	O
by	O
their	O
parent	O
and	O
then	O
reaped	O
by	O
the	O
system	O
–	O
processes	O
that	O
stay	O
zombies	B-Application
for	O
a	O
long	O
time	O
are	O
generally	O
an	O
error	O
and	O
cause	O
a	O
resource	O
leak	O
,	O
but	O
the	O
only	O
resource	O
they	O
occupy	O
is	O
the	O
process	B-Operating_System
table	I-Operating_System
entry	O
–	O
process	B-Operating_System
ID	I-Operating_System
.	O
</s>
<s>
The	O
term	O
zombie	B-Operating_System
process	I-Operating_System
derives	O
from	O
the	O
common	O
definition	O
of	O
zombie	B-Application
—	O
an	O
undead	O
person	O
.	O
</s>
<s>
In	O
the	O
term	O
's	O
metaphor	O
,	O
the	O
child	B-Operating_System
process	I-Operating_System
has	O
"	O
died	O
"	O
but	O
has	O
not	O
yet	O
been	O
"	O
reaped	O
"	O
.	O
</s>
<s>
Also	O
,	O
unlike	O
normal	O
processes	O
,	O
the	O
kill	B-Operating_System
command	I-Operating_System
has	O
no	O
effect	O
on	O
a	O
zombie	B-Operating_System
process	I-Operating_System
.	O
</s>
<s>
Zombie	B-Operating_System
processes	I-Operating_System
should	O
not	O
be	O
confused	O
with	O
orphan	O
processes	O
:	O
an	O
orphan	B-Operating_System
process	I-Operating_System
is	O
a	O
process	B-Operating_System
that	O
is	O
still	O
executing	O
,	O
but	O
whose	O
parent	O
has	O
died	O
.	O
</s>
<s>
When	O
the	O
parent	O
dies	O
,	O
the	O
orphaned	O
child	B-Operating_System
process	I-Operating_System
is	O
adopted	O
by	O
init	B-Operating_System
(	O
process	B-Operating_System
ID	I-Operating_System
1	O
)	O
.	O
</s>
<s>
When	O
orphan	O
processes	O
die	O
,	O
they	O
do	O
not	O
remain	O
as	O
zombie	B-Operating_System
processes	I-Operating_System
;	O
instead	O
,	O
they	O
are	O
waited	O
on	O
by	O
init	B-Operating_System
.	O
</s>
<s>
The	O
result	O
is	O
that	O
a	O
process	B-Operating_System
that	O
is	O
both	O
a	O
zombie	B-Application
and	O
an	O
orphan	O
will	O
be	O
reaped	O
automatically	O
.	O
</s>
<s>
When	O
a	O
process	B-Operating_System
ends	O
via	O
exit	B-Operating_System
,	O
all	O
of	O
the	O
memory	O
and	O
resources	O
associated	O
with	O
it	O
are	O
deallocated	O
so	O
they	O
can	O
be	O
used	O
by	O
other	O
processes	O
.	O
</s>
<s>
However	O
,	O
the	O
process	B-Operating_System
's	O
entry	O
in	O
the	O
process	B-Operating_System
table	I-Operating_System
remains	O
.	O
</s>
<s>
The	O
parent	O
can	O
read	O
the	O
child	O
's	O
exit	B-Operating_System
status	I-Operating_System
by	O
executing	O
the	O
wait	B-Operating_System
system	B-Operating_System
call	I-Operating_System
,	O
whereupon	O
the	O
zombie	B-Application
is	O
removed	O
.	O
</s>
<s>
The	O
wait	B-Operating_System
call	O
may	O
be	O
executed	O
in	O
sequential	O
code	O
,	O
but	O
it	O
is	O
commonly	O
executed	O
in	O
a	O
handler	O
for	O
the	O
SIGCHLD	B-General_Concept
signal	B-Operating_System
,	O
which	O
the	O
parent	O
receives	O
whenever	O
a	O
child	O
has	O
died	O
.	O
</s>
<s>
After	O
the	O
zombie	B-Application
is	O
removed	O
,	O
its	O
process	B-Operating_System
identifier	I-Operating_System
(	O
PID	O
)	O
and	O
entry	O
in	O
the	O
process	B-Operating_System
table	I-Operating_System
can	O
then	O
be	O
reused	O
.	O
</s>
<s>
However	O
,	O
if	O
a	O
parent	O
fails	O
to	O
call	O
wait	B-Operating_System
,	O
the	O
zombie	B-Application
will	O
be	O
left	O
in	O
the	O
process	B-Operating_System
table	I-Operating_System
,	O
causing	O
a	O
resource	O
leak	O
.	O
</s>
<s>
In	O
some	O
situations	O
this	O
may	O
be	O
desirable	O
–	O
the	O
parent	B-Operating_System
process	I-Operating_System
wishes	O
to	O
continue	O
holding	O
this	O
resource	O
–	O
for	O
example	O
if	O
the	O
parent	O
creates	O
another	O
child	B-Operating_System
process	I-Operating_System
it	O
ensures	O
that	O
it	O
will	O
not	O
be	O
allocated	O
the	O
same	O
PID	O
.	O
</s>
<s>
On	O
modern	O
UNIX-like	B-Operating_System
systems	I-Operating_System
(	O
that	O
comply	O
with	O
SUSv3	O
specification	O
in	O
this	O
respect	O
)	O
,	O
the	O
following	O
special	O
case	O
applies	O
:	O
if	O
the	O
parent	O
explicitly	O
ignores	O
SIGCHLD	B-General_Concept
by	O
setting	O
its	O
handler	O
to	O
SIG_IGN	O
(	O
rather	O
than	O
simply	O
ignoring	O
the	O
signal	B-Operating_System
by	O
default	O
)	O
or	O
has	O
the	O
SA_NOCLDWAIT	O
flag	O
set	O
,	O
all	O
child	O
exit	B-Operating_System
status	I-Operating_System
information	O
will	O
be	O
discarded	O
and	O
no	O
zombie	B-Operating_System
processes	I-Operating_System
will	O
be	O
left	O
.	O
</s>
<s>
Zombies	B-Application
can	O
be	O
identified	O
in	O
the	O
output	O
from	O
the	O
Unix	B-Application
ps	B-Application
command	B-Application
by	O
the	O
presence	O
of	O
a	O
"	O
Z	O
"	O
in	O
the	O
"	O
STAT	O
"	O
column	O
.	O
</s>
<s>
Zombies	B-Application
that	O
exist	O
for	O
more	O
than	O
a	O
short	O
period	O
of	O
time	O
typically	O
indicate	O
a	O
bug	O
in	O
the	O
parent	O
program	O
,	O
or	O
just	O
an	O
uncommon	O
decision	O
to	O
not	O
reap	B-Operating_System
children	O
(	O
see	O
example	O
)	O
.	O
</s>
<s>
If	O
the	O
parent	O
program	O
is	O
no	O
longer	O
running	O
,	O
zombie	B-Operating_System
processes	I-Operating_System
typically	O
indicate	O
a	O
bug	O
in	O
the	O
operating	B-General_Concept
system	I-General_Concept
.	O
</s>
<s>
As	O
with	O
other	O
resource	O
leaks	O
,	O
the	O
presence	O
of	O
a	O
few	O
zombies	B-Application
is	O
not	O
worrisome	O
in	O
itself	O
,	O
but	O
may	O
indicate	O
a	O
problem	O
that	O
would	O
grow	O
serious	O
under	O
heavier	O
loads	O
.	O
</s>
<s>
Since	O
there	O
is	O
no	O
memory	O
allocated	O
to	O
zombie	B-Operating_System
processes	I-Operating_System
–	O
the	O
only	O
system	O
memory	O
usage	O
is	O
for	O
the	O
process	B-Operating_System
table	I-Operating_System
entry	O
itself	O
–	O
the	O
primary	O
concern	O
with	O
many	O
zombies	B-Application
is	O
not	O
running	O
out	O
of	O
memory	O
,	O
but	O
rather	O
running	O
out	O
of	O
process	B-Operating_System
table	I-Operating_System
entries	O
,	O
concretely	O
process	B-Operating_System
ID	I-Operating_System
numbers	O
.	O
</s>
<s>
To	O
remove	O
zombies	B-Application
from	O
a	O
system	O
,	O
the	O
SIGCHLD	B-General_Concept
signal	B-Operating_System
can	O
be	O
sent	O
to	O
the	O
parent	O
manually	O
,	O
using	O
the	O
kill	B-Operating_System
command	I-Operating_System
.	O
</s>
<s>
If	O
the	O
parent	B-Operating_System
process	I-Operating_System
still	O
refuses	O
to	O
reap	B-Operating_System
the	O
zombie	B-Application
,	O
and	O
if	O
it	O
would	O
be	O
fine	O
to	O
terminate	B-Operating_System
the	O
parent	B-Operating_System
process	I-Operating_System
,	O
the	O
next	O
step	O
can	O
be	O
to	O
remove	O
the	O
parent	B-Operating_System
process	I-Operating_System
.	O
</s>
<s>
When	O
a	O
process	B-Operating_System
loses	O
its	O
parent	O
,	O
init	B-Operating_System
becomes	O
its	O
new	O
parent	O
.	O
</s>
<s>
init	B-Operating_System
periodically	O
executes	O
the	O
wait	B-Operating_System
system	B-Operating_System
call	I-Operating_System
to	O
reap	B-Operating_System
any	O
zombies	B-Application
with	O
init	B-Operating_System
as	O
parent	O
.	O
</s>
<s>
Synchronously	O
waiting	O
for	O
the	O
specific	O
child	O
processes	O
in	O
a	O
(	O
specific	O
)	O
order	O
may	O
leave	O
zombies	B-Application
present	O
longer	O
than	O
the	O
above-mentioned	O
"	O
short	O
period	O
of	O
time	O
"	O
.	O
</s>
<s>
In	O
the	O
first	O
loop	O
,	O
the	O
original	O
(	O
parent	O
)	O
process	B-Operating_System
forks	O
10	O
copies	O
of	O
itself	O
.	O
</s>
<s>
During	O
the	O
loop	O
,	O
an	O
array	O
of	O
child	B-Operating_System
process	I-Operating_System
IDs	O
is	O
built	O
.	O
</s>
<s>
(	O
Not	O
that	O
this	O
really	O
matters	O
,	O
as	O
only	O
the	O
parent	B-Operating_System
process	I-Operating_System
actually	O
uses	O
this	O
array	O
.	O
)	O
</s>
<s>
The	O
second	O
loop	O
executes	O
only	O
in	O
the	O
parent	B-Operating_System
process	I-Operating_System
(	O
because	O
all	O
of	O
the	O
children	O
have	O
exited	O
before	O
this	O
point	O
)	O
,	O
and	O
waits	O
for	O
each	O
child	O
to	O
exit	B-Operating_System
.	O
</s>
<s>
There	O
is	O
no	O
possibility	O
of	O
random	O
ordering	O
here	O
,	O
since	O
it	O
is	O
driven	O
by	O
a	O
loop	O
in	O
a	O
single	O
process	B-Operating_System
.	O
</s>
<s>
This	O
again	O
is	O
just	O
the	O
random	O
behavior	O
of	O
the	O
process	B-Operating_System
scheduler	O
-	O
the	O
"	O
parent9	O
"	O
message	O
could	O
have	O
appeared	O
anywhere	O
in	O
the	O
sequence	O
prior	O
to	O
"	O
parent8	O
"	O
.	O
</s>
<s>
Child0	O
through	O
Child8	O
spend	O
one	O
or	O
more	O
seconds	O
in	O
this	O
state	O
,	O
between	O
the	O
time	O
they	O
exited	O
and	O
the	O
time	O
the	O
parent	O
did	O
a	O
waitpid( )	O
on	O
them	O
.	O
</s>
<s>
The	O
parent	O
was	O
already	O
waiting	O
on	O
Child9	O
before	O
it	O
exited	O
,	O
so	O
that	O
one	O
process	B-Operating_System
spent	O
essentially	O
no	O
time	O
as	O
a	O
zombie	B-Application
.	O
</s>
