<s>
Fork	B-Operating_System
–	I-Operating_System
exec	I-Operating_System
is	O
a	O
commonly	O
used	O
technique	O
in	O
Unix	B-Application
whereby	O
an	O
executing	O
process	B-Operating_System
spawns	B-Language
a	O
new	O
program	O
.	O
</s>
<s>
fork( )	O
is	O
the	O
name	O
of	O
the	O
system	B-Operating_System
call	I-Operating_System
that	O
the	O
parent	B-Operating_System
process	I-Operating_System
uses	O
to	O
"	O
divide	O
"	O
itself	O
(	O
"	O
fork	B-Language
"	O
)	O
into	O
two	O
identical	O
processes	O
.	O
</s>
<s>
After	O
calling	O
fork( )	O
,	O
the	O
created	O
child	B-Operating_System
process	I-Operating_System
is	O
an	O
exact	O
copy	O
of	O
the	O
parent	O
except	O
for	O
the	O
return	O
value	O
of	O
the	O
fork( )	O
call	O
.	O
</s>
<s>
This	O
includes	O
open	O
files	O
,	O
register	O
state	O
,	O
and	O
all	O
memory	O
allocations	O
,	O
which	O
includes	O
the	O
program	O
's	O
executable	O
code	B-Language
.	O
</s>
<s>
In	O
some	O
cases	O
the	O
two	O
continue	O
to	O
run	O
the	O
same	O
binary	O
,	O
but	O
often	O
one	O
(	O
usually	O
the	O
child	O
)	O
switches	O
to	O
running	O
another	O
binary	O
executable	O
using	O
the	O
exec( )	O
system	B-Operating_System
call	I-Operating_System
.	O
</s>
<s>
When	O
a	O
process	B-Language
forks	I-Language
,	O
a	O
complete	O
copy	O
of	O
the	O
executing	O
program	O
is	O
made	O
into	O
the	O
new	O
process	B-Operating_System
.	O
</s>
<s>
This	O
new	O
process	B-Operating_System
is	O
a	O
child	O
of	O
the	O
parent	B-Operating_System
process	I-Operating_System
,	O
and	O
has	O
a	O
new	O
process	B-Operating_System
identifier	I-Operating_System
(	O
PID	O
)	O
.	O
</s>
<s>
The	O
fork( )	O
function	O
returns	O
the	O
child	O
's	O
PID	O
to	O
the	O
parent	B-Operating_System
process	I-Operating_System
.	O
</s>
<s>
The	O
fork( )	O
function	O
returns	O
0	O
to	O
the	O
child	B-Operating_System
process	I-Operating_System
.	O
</s>
<s>
The	O
parent	B-Operating_System
process	I-Operating_System
can	O
either	O
continue	O
execution	O
or	O
wait	B-Operating_System
for	O
the	O
child	B-Operating_System
process	I-Operating_System
to	O
complete	O
.	O
</s>
<s>
The	O
child	O
,	O
after	O
discovering	O
that	O
it	O
is	O
the	O
child	O
,	O
replaces	O
itself	O
completely	O
with	O
another	O
program	O
,	O
so	O
that	O
the	O
code	B-Language
and	O
address	B-General_Concept
space	I-General_Concept
of	O
the	O
original	O
program	O
are	O
lost	O
.	O
</s>
<s>
If	O
the	O
parent	O
chooses	O
to	O
wait	B-Operating_System
for	O
the	O
child	O
to	O
die	O
,	O
then	O
the	O
parent	O
will	O
receive	O
the	O
exit	B-Operating_System
code	I-Operating_System
of	O
the	O
program	O
that	O
the	O
child	O
executed	O
.	O
</s>
<s>
To	O
prevent	O
the	O
child	O
becoming	O
a	O
zombie	B-Operating_System
the	O
parent	O
should	O
call	O
wait	B-Operating_System
on	O
its	O
children	O
,	O
either	O
periodically	O
or	O
upon	O
receiving	O
the	O
SIGCHLD	B-General_Concept
signal	O
,	O
which	O
indicates	O
a	O
child	B-Operating_System
process	I-Operating_System
has	O
terminated	O
.	O
</s>
<s>
One	O
can	O
also	O
asynchronously	O
wait	B-Operating_System
on	O
their	O
children	O
to	O
finish	O
,	O
by	O
using	O
a	O
signal	O
handler	O
for	O
SIGCHLD	B-General_Concept
,	O
if	O
they	O
need	O
to	O
ensure	O
everything	O
is	O
cleaned	O
up	O
.	O
</s>
<s>
Here	O
's	O
an	O
example	O
of	O
a	O
signal	O
handler	O
that	O
catches	O
any	O
incoming	O
SIGCHLD	B-General_Concept
signals	O
and	O
handles	O
multiple	O
concurrent	O
signals	O
received	O
.	O
</s>
<s>
When	O
the	O
child	B-Operating_System
process	I-Operating_System
calls	O
exec( )	O
,	O
all	O
data	O
in	O
the	O
original	O
program	O
is	O
lost	O
,	O
and	O
it	O
is	O
replaced	O
with	O
a	O
running	O
copy	O
of	O
the	O
new	O
program	O
.	O
</s>
<s>
This	O
is	O
known	O
as	O
overlaying	B-Operating_System
.	O
</s>
<s>
Although	O
all	O
data	O
are	O
replaced	O
,	O
the	O
file	B-Application
descriptors	I-Application
that	O
were	O
open	O
in	O
the	O
parent	O
are	O
closed	O
only	O
if	O
the	O
program	O
has	O
explicitly	O
marked	O
them	O
close-on-exec	O
.	O
</s>
<s>
This	O
allows	O
for	O
the	O
common	O
practice	O
of	O
the	O
parent	O
creating	O
a	O
pipe	B-Operating_System
prior	O
to	O
calling	O
fork( )	O
and	O
using	O
it	O
to	O
communicate	O
with	O
the	O
executed	O
program	O
.	O
</s>
<s>
Microsoft	B-Application
Windows	I-Application
does	O
not	O
support	O
the	O
fork-exec	B-Operating_System
model	O
,	O
as	O
it	O
does	O
not	O
have	O
a	O
system	B-Operating_System
call	I-Operating_System
analogous	O
to	O
fork( )	O
.	O
</s>
<s>
The	O
spawn( )	O
family	O
of	O
functions	O
declared	O
in	O
process.h	B-Language
can	O
replace	O
it	O
in	O
cases	O
where	O
the	O
call	O
to	O
fork( )	O
is	O
followed	O
directly	O
by	O
exec( )	O
.	O
</s>
