<s>
In	O
computing	O
,	O
particularly	O
in	O
the	O
context	B-Operating_System
of	O
the	O
Unix	B-Application
operating	I-Application
system	I-Application
and	O
its	O
workalikes	B-Operating_System
,	O
fork	B-Language
is	O
an	O
operation	O
whereby	O
a	O
process	B-Operating_System
creates	O
a	O
copy	O
of	O
itself	O
.	O
</s>
<s>
It	O
is	O
an	O
interface	O
which	O
is	O
required	O
for	O
compliance	O
with	O
the	O
POSIX	O
and	O
Single	O
UNIX	B-Application
Specification	O
standards	O
.	O
</s>
<s>
It	O
is	O
usually	O
implemented	O
as	O
a	O
C	B-Language
standard	I-Language
library	I-Language
wrapper	B-Library
to	O
the	O
fork	B-Language
,	O
clone	O
,	O
or	O
other	O
system	B-Operating_System
calls	I-Operating_System
of	O
the	O
kernel	B-Operating_System
.	O
</s>
<s>
Fork	B-Language
is	O
the	O
primary	O
method	O
of	O
process	B-Operating_System
creation	O
on	O
Unix-like	B-Operating_System
operating	I-Operating_System
systems	I-Operating_System
.	O
</s>
<s>
Fork	B-Language
and	O
its	O
variants	O
are	O
typically	O
the	O
only	O
way	O
of	O
doing	O
so	O
in	O
Unix-like	B-Operating_System
systems	I-Operating_System
.	O
</s>
<s>
For	O
a	O
process	B-Operating_System
to	O
start	O
the	O
execution	O
of	O
a	O
different	O
program	O
,	O
it	O
first	O
forks	B-Language
to	O
create	O
a	O
copy	O
of	O
itself	O
.	O
</s>
<s>
Then	O
,	O
the	O
copy	O
,	O
called	O
the	O
"	O
child	B-Operating_System
process	I-Operating_System
"	O
,	O
calls	O
the	O
exec	B-Operating_System
system	B-Operating_System
call	I-Operating_System
to	O
overlay	O
itself	O
with	O
the	O
other	O
program	O
:	O
it	O
ceases	O
execution	O
of	O
its	O
former	O
program	O
in	O
favor	O
of	O
the	O
other	O
.	O
</s>
<s>
The	O
fork	B-Language
operation	O
creates	O
a	O
separate	O
address	B-General_Concept
space	I-General_Concept
for	O
the	O
child	O
.	O
</s>
<s>
The	O
child	B-Operating_System
process	I-Operating_System
has	O
an	O
exact	O
copy	O
of	O
all	O
the	O
memory	O
segments	O
of	O
the	O
parent	B-Operating_System
process	I-Operating_System
.	O
</s>
<s>
In	O
modern	O
UNIX	B-Application
variants	O
that	O
follow	O
the	O
virtual	B-Architecture
memory	I-Architecture
model	O
from	O
SunOS-4.0	O
,	O
copy-on-write	B-Language
semantics	O
are	O
implemented	O
and	O
the	O
physical	O
memory	O
need	O
not	O
be	O
actually	O
copied	O
.	O
</s>
<s>
Instead	O
,	O
virtual	B-General_Concept
memory	I-General_Concept
pages	I-General_Concept
in	O
both	O
processes	O
may	O
refer	O
to	O
the	O
same	O
pages	O
of	O
physical	O
memory	O
until	O
one	O
of	O
them	O
writes	O
to	O
such	O
a	O
page	O
:	O
then	O
it	O
is	O
copied	O
.	O
</s>
<s>
This	O
optimization	O
is	O
important	O
in	O
the	O
common	O
case	O
where	O
fork	B-Language
is	O
used	O
in	O
conjunction	O
with	O
exec	B-Operating_System
to	O
execute	O
a	O
new	O
program	O
:	O
typically	O
,	O
the	O
child	B-Operating_System
process	I-Operating_System
performs	O
only	O
a	O
small	O
set	O
of	O
actions	O
before	O
it	O
ceases	O
execution	O
of	O
its	O
program	O
in	O
favour	O
of	O
the	O
program	O
to	O
be	O
started	O
,	O
and	O
it	O
requires	O
very	O
few	O
,	O
if	O
any	O
,	O
of	O
its	O
parent	O
's	O
data	B-General_Concept
structures	I-General_Concept
.	O
</s>
<s>
When	O
a	O
process	B-Operating_System
calls	O
fork	B-Language
,	O
it	O
is	O
deemed	O
the	O
parent	B-Operating_System
process	I-Operating_System
and	O
the	O
newly	O
created	O
process	B-Operating_System
is	O
its	O
child	O
.	O
</s>
<s>
After	O
the	O
fork	B-Language
,	O
both	O
processes	O
not	O
only	O
run	O
the	O
same	O
program	O
,	O
but	O
they	O
resume	O
execution	O
as	O
though	O
both	O
had	O
called	O
the	O
system	B-Operating_System
call	I-Operating_System
.	O
</s>
<s>
They	O
can	O
then	O
inspect	O
the	O
call	O
's	O
return	B-Language
value	I-Language
to	O
determine	O
their	O
status	O
,	O
child	O
or	O
parent	O
,	O
and	O
act	O
accordingly	O
.	O
</s>
<s>
One	O
of	O
the	O
earliest	O
references	O
to	O
a	O
fork	B-Language
concept	O
appeared	O
in	O
A	O
Multiprocessor	O
System	O
Design	O
by	O
Melvin	O
Conway	O
,	O
published	O
in	O
1962	O
.	O
</s>
<s>
Conway	O
's	O
paper	O
motivated	O
the	O
implementation	O
by	O
L	O
.	O
Peter	O
Deutsch	O
of	O
fork	B-Language
in	O
the	O
GENIE	O
time-sharing	O
system	O
,	O
where	O
the	O
concept	O
was	O
borrowed	O
by	O
Ken	O
Thompson	O
for	O
its	O
earliest	O
appearance	O
in	O
Research	B-Operating_System
Unix	I-Operating_System
.	O
</s>
<s>
Fork	B-Language
later	O
became	O
a	O
standard	O
interface	O
in	O
POSIX	O
.	O
</s>
<s>
The	O
child	B-Operating_System
process	I-Operating_System
starts	O
off	O
with	O
a	O
copy	O
of	O
its	O
parent	O
's	O
file	B-Application
descriptors	I-Application
.	O
</s>
<s>
For	O
interprocess	O
communication	O
,	O
the	O
parent	B-Operating_System
process	I-Operating_System
will	O
often	O
create	O
one	O
or	O
several	O
pipes	B-Operating_System
,	O
and	O
then	O
after	O
forking	O
the	O
processes	O
will	O
close	O
the	O
ends	O
of	O
the	O
pipes	B-Operating_System
that	O
they	O
do	O
n't	O
need	O
.	O
</s>
<s>
Vfork	O
is	O
a	O
variant	O
of	O
fork	B-Language
with	O
the	O
same	O
calling	O
convention	O
and	O
much	O
the	O
same	O
semantics	O
,	O
but	O
only	O
to	O
be	O
used	O
in	O
restricted	O
situations	O
.	O
</s>
<s>
It	O
originated	O
in	O
the	O
3BSD	B-Operating_System
version	O
of	O
Unix	B-Application
,	O
the	O
first	O
Unix	B-Application
to	O
support	O
virtual	B-Architecture
memory	I-Architecture
.	O
</s>
<s>
It	O
was	O
standardized	O
by	O
POSIX	O
,	O
which	O
permitted	O
vfork	O
to	O
have	O
exactly	O
the	O
same	O
behavior	O
as	O
fork	B-Language
,	O
but	O
was	O
marked	O
obsolescent	O
in	O
the	O
2004	O
edition	O
and	O
was	O
replaced	O
by	O
posix_spawn( )	O
(	O
which	O
is	O
typically	O
implemented	O
via	O
vfork	O
)	O
in	O
subsequent	O
editions	O
.	O
</s>
<s>
When	O
a	O
vfork	O
system	B-Operating_System
call	I-Operating_System
is	O
issued	O
,	O
the	O
parent	B-Operating_System
process	I-Operating_System
will	O
be	O
suspended	O
until	O
the	O
child	B-Operating_System
process	I-Operating_System
has	O
either	O
completed	O
execution	O
or	O
been	O
replaced	O
with	O
a	O
new	O
executable	O
image	O
via	O
one	O
of	O
the	O
"	O
exec	B-Operating_System
"	O
family	O
of	O
system	B-Operating_System
calls	I-Operating_System
.	O
</s>
<s>
The	O
child	O
borrows	O
the	O
memory	B-General_Concept
management	I-General_Concept
unit	I-General_Concept
setup	O
from	O
the	O
parent	O
and	O
memory	B-General_Concept
pages	I-General_Concept
are	O
shared	O
among	O
the	O
parent	O
and	O
child	B-Operating_System
process	I-Operating_System
with	O
no	O
copying	O
done	O
,	O
and	O
in	O
particular	O
with	O
no	O
copy-on-write	B-Language
semantics	O
;	O
hence	O
,	O
if	O
the	O
child	B-Operating_System
process	I-Operating_System
makes	O
a	O
modification	O
in	O
any	O
of	O
the	O
shared	O
pages	O
,	O
no	O
new	O
page	O
will	O
be	O
created	O
and	O
the	O
modified	O
pages	O
are	O
visible	O
to	O
the	O
parent	B-Operating_System
process	I-Operating_System
too	O
.	O
</s>
<s>
Since	O
there	O
is	O
absolutely	O
no	O
page	O
copying	O
involved	O
(	O
consuming	O
additional	O
memory	O
)	O
,	O
this	O
technique	O
is	O
an	O
optimization	O
over	O
plain	O
fork	B-Language
in	O
full-copy	O
environments	O
when	O
used	O
with	O
exec	B-Operating_System
.	O
</s>
<s>
In	O
POSIX	O
,	O
using	O
vfork	O
for	O
any	O
purpose	O
except	O
as	O
a	O
prelude	O
to	O
an	O
immediate	O
call	O
to	O
a	O
function	O
from	O
the	O
exec	B-Operating_System
family	O
(	O
and	O
a	O
select	O
few	O
other	O
operations	O
)	O
gives	O
rise	O
to	O
undefined	B-Language
behavior	I-Language
.	O
</s>
<s>
As	O
with	O
vfork	O
,	O
the	O
child	O
borrows	O
data	B-General_Concept
structures	I-General_Concept
rather	O
than	O
copying	O
them	O
.	O
</s>
<s>
vfork	O
is	O
still	O
faster	O
than	O
a	O
fork	B-Language
that	O
uses	O
copy	B-Language
on	I-Language
write	I-Language
semantics	O
.	O
</s>
<s>
System	B-Operating_System
V	I-Operating_System
did	O
not	O
support	O
this	O
function	O
call	O
before	O
System	O
VR4	O
was	O
introduced	O
,	O
because	O
the	O
memory	O
sharing	O
that	O
it	O
causes	O
is	O
error-prone	O
:	O
</s>
<s>
Similarly	O
,	O
the	O
Linux	B-Operating_System
man	O
page	O
for	O
vfork	O
strongly	O
discourages	O
its	O
use	O
:	O
</s>
<s>
Other	O
problems	O
with	O
include	O
deadlocks	B-Operating_System
that	O
might	O
occur	O
in	O
multithreaded	B-Operating_System
programs	O
due	O
to	O
interactions	O
with	O
dynamic	B-Application
linking	I-Application
.	O
</s>
<s>
As	O
a	O
replacement	O
for	O
the	O
interface	O
,	O
POSIX	O
introduced	O
the	O
family	O
of	O
functions	O
that	O
combine	O
the	O
actions	O
of	O
fork	B-Operating_System
and	I-Operating_System
exec	I-Operating_System
.	O
</s>
<s>
These	O
functions	O
may	O
be	O
implemented	O
as	O
library	O
routines	O
in	O
terms	O
of	O
,	O
as	O
is	O
done	O
in	O
Linux	B-Operating_System
,	O
or	O
in	O
terms	O
of	O
for	O
better	O
performance	O
,	O
as	O
is	O
done	O
in	O
Solaris	O
,	O
but	O
the	O
POSIX	O
specification	O
notes	O
that	O
they	O
were	O
"	O
designed	O
as	O
kernel	B-Operating_System
operations	I-Operating_System
"	O
,	O
especially	O
for	O
operating	O
systems	O
running	O
on	O
constrained	O
hardware	O
and	O
real-time	B-General_Concept
systems	I-General_Concept
.	O
</s>
<s>
While	O
the	O
4.4BSD	O
implementation	O
got	O
rid	O
of	O
the	O
vfork	O
implementation	O
,	O
causing	O
vfork	O
to	O
have	O
the	O
same	O
behavior	O
as	O
fork	B-Language
,	O
it	O
was	O
later	O
reinstated	O
in	O
the	O
NetBSD	B-Device
operating	O
system	O
for	O
performance	O
reasons	O
.	O
</s>
<s>
Some	O
embedded	O
operating	O
systems	O
such	O
as	O
uClinux	B-Application
omit	O
fork	B-Language
and	O
only	O
implement	O
vfork	O
,	O
because	O
they	O
need	O
to	O
operate	O
on	O
devices	O
where	O
copy-on-write	B-Language
is	O
impossible	O
to	O
implement	O
due	O
to	O
lack	O
of	O
a	O
memory	B-General_Concept
management	I-General_Concept
unit	I-General_Concept
.	O
</s>
<s>
The	O
Plan	B-Operating_System
9	I-Operating_System
operating	O
system	O
,	O
created	O
by	O
the	O
designers	O
of	O
Unix	B-Application
,	O
includes	O
fork	B-Language
but	O
also	O
a	O
variant	O
called	O
"	O
rfork	O
"	O
that	O
permits	O
fine-grained	O
sharing	O
of	O
resources	O
between	O
parent	O
and	O
child	O
processes	O
,	O
including	O
the	O
address	B-General_Concept
space	I-General_Concept
(	O
except	O
for	O
a	O
stack	B-General_Concept
segment	O
,	O
which	O
is	O
unique	O
to	O
each	O
process	B-Operating_System
)	O
,	O
environment	O
variables	O
and	O
the	O
filesystem	O
namespace	O
;	O
this	O
makes	O
it	O
a	O
unified	O
interface	O
for	O
the	O
creation	O
of	O
both	O
processes	O
and	O
threads	B-Operating_System
within	O
them	O
.	O
</s>
<s>
Both	O
FreeBSD	B-Operating_System
and	O
IRIX	B-Operating_System
adopted	O
the	O
rfork	O
system	B-Operating_System
call	I-Operating_System
from	O
Plan	B-Operating_System
9	I-Operating_System
,	O
the	O
latter	O
renaming	O
it	O
"	O
sproc	O
"	O
.	O
</s>
<s>
clone	O
is	O
a	O
system	B-Operating_System
call	I-Operating_System
in	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
that	O
creates	O
a	O
child	B-Operating_System
process	I-Operating_System
that	O
may	O
share	O
parts	O
of	O
its	O
execution	O
context	B-Operating_System
with	O
the	O
parent	O
.	O
</s>
<s>
Like	O
FreeBSD	B-Operating_System
's	O
rfork	O
and	O
IRIX	B-Operating_System
's	O
sproc	O
,	O
Linux	B-Operating_System
's	O
clone	O
was	O
inspired	O
by	O
Plan	B-Operating_System
9	I-Operating_System
's	O
rfork	O
and	O
can	O
be	O
used	O
to	O
implement	O
threads	B-Operating_System
(	O
though	O
application	O
programmers	O
will	O
typically	O
use	O
a	O
higher-level	O
interface	O
such	O
as	O
pthreads	B-Operating_System
,	O
implemented	O
on	O
top	O
of	O
clone	O
)	O
.	O
</s>
<s>
The	O
"	O
separate	O
stacks	O
"	O
feature	O
from	O
Plan	B-Operating_System
9	I-Operating_System
and	O
IRIX	B-Operating_System
has	O
been	O
omitted	O
because	O
(	O
according	O
to	O
Linus	O
Torvalds	O
)	O
it	O
causes	O
too	O
much	O
overhead	O
.	O
</s>
<s>
In	O
the	O
original	O
design	O
of	O
the	O
VMS	B-Operating_System
operating	I-Operating_System
system	I-Operating_System
(	O
1977	O
)	O
,	O
a	O
copy	O
operation	O
with	O
subsequent	O
mutation	O
of	O
the	O
content	O
of	O
a	O
few	O
specific	O
addresses	O
for	O
the	O
new	O
process	B-Operating_System
as	O
in	O
forking	O
was	O
considered	O
risky	O
.	O
</s>
<s>
Errors	O
in	O
the	O
current	O
process	B-Operating_System
state	O
may	O
be	O
copied	O
to	O
a	O
child	B-Operating_System
process	I-Operating_System
.	O
</s>
<s>
Here	O
,	O
the	O
metaphor	O
of	O
process	B-Operating_System
spawning	O
is	O
used	O
:	O
each	O
component	O
of	O
the	O
memory	O
layout	O
of	O
the	O
new	O
process	B-Operating_System
is	O
newly	O
constructed	O
from	O
scratch	O
.	O
</s>
<s>
The	O
spawn	B-Language
metaphor	O
was	O
later	O
adopted	O
in	O
Microsoft	O
operating	O
systems	O
(	O
1993	O
)	O
.	O
</s>
<s>
The	O
POSIX-compatibility	O
component	O
of	O
VM/CMS	B-Application
(	O
OpenExtensions	O
)	O
provides	O
a	O
very	O
limited	O
implementation	O
of	O
fork	B-Language
,	O
in	O
which	O
the	O
parent	O
is	O
suspended	O
while	O
the	O
child	O
executes	O
,	O
and	O
the	O
child	O
and	O
the	O
parent	O
share	O
the	O
same	O
address	B-General_Concept
space	I-General_Concept
.	O
</s>
<s>
This	O
is	O
essentially	O
a	O
vfork	O
labelled	O
as	O
a	O
fork	B-Language
.	O
</s>
<s>
(	O
This	O
applies	O
to	O
the	O
CMS	O
guest	O
operating	O
system	O
only	O
;	O
other	O
VM	B-Application
guest	O
operating	O
systems	O
,	O
such	O
as	O
Linux	B-Operating_System
,	O
provide	O
standard	O
fork	B-Language
functionality	O
.	O
)	O
</s>
<s>
program	O
demonstrates	O
the	O
mechanics	O
of	O
the	O
system	B-Operating_System
call	I-Operating_System
in	O
the	O
C	B-Language
programming	I-Language
language	I-Language
.	O
</s>
<s>
The	O
program	O
forks	B-Language
into	O
two	O
processes	O
,	O
each	O
deciding	O
what	O
functionality	O
they	O
perform	O
based	O
on	O
the	O
return	B-Language
value	I-Language
of	O
the	O
fork	B-Language
system	B-Operating_System
call	I-Operating_System
.	O
</s>
<s>
Boilerplate	O
code	O
such	O
as	O
header	B-Language
inclusions	I-Language
has	O
been	O
omitted	O
.	O
</s>
<s>
The	O
first	O
statement	O
in	O
calls	O
the	O
system	B-Operating_System
call	I-Operating_System
to	O
split	O
execution	O
into	O
two	O
processes	O
.	O
</s>
<s>
The	O
return	B-Language
value	I-Language
of	O
is	O
recorded	O
in	O
a	O
variable	O
of	O
type	O
,	O
which	O
is	O
the	O
POSIX	O
type	O
for	O
process	B-Operating_System
identifiers	O
(	O
PIDs	O
)	O
.	O
</s>
<s>
Minus	O
one	O
indicates	O
an	O
error	O
in	O
:	O
no	O
new	O
process	B-Operating_System
was	O
created	O
,	O
so	O
an	O
error	O
message	O
is	O
printed	O
.	O
</s>
<s>
To	O
make	O
the	O
processes	O
perform	O
different	O
tasks	O
,	O
the	O
program	O
must	O
branch	B-General_Concept
on	O
the	O
return	B-Language
value	I-Language
of	O
to	O
determine	O
whether	O
it	O
is	O
executing	O
as	O
the	O
child	B-Operating_System
process	I-Operating_System
or	O
the	O
parent	B-Operating_System
process	I-Operating_System
.	O
</s>
<s>
In	O
the	O
child	B-Operating_System
process	I-Operating_System
,	O
the	O
return	B-Language
value	I-Language
appears	O
as	O
zero	O
(	O
which	O
is	O
an	O
invalid	O
process	B-Operating_System
identifier	O
)	O
.	O
</s>
<s>
The	O
child	B-Operating_System
process	I-Operating_System
prints	O
the	O
desired	O
greeting	O
message	O
,	O
then	O
exits	O
.	O
</s>
<s>
(	O
For	O
technical	O
reasons	O
,	O
the	O
POSIX	O
function	O
must	O
be	O
used	O
here	O
instead	O
of	O
the	O
C	B-Language
standard	O
function	O
.	O
)	O
</s>
<s>
The	O
other	O
process	B-Operating_System
,	O
the	O
parent	O
,	O
receives	O
from	O
the	O
process	B-Operating_System
identifier	O
of	O
the	O
child	O
,	O
which	O
is	O
always	O
a	O
positive	O
number	O
.	O
</s>
<s>
The	O
parent	B-Operating_System
process	I-Operating_System
passes	O
this	O
identifier	O
to	O
the	O
system	B-Operating_System
call	I-Operating_System
to	O
suspend	O
execution	O
until	O
the	O
child	O
has	O
exited	O
.	O
</s>
