<s>
Copy-on-write	B-Language
(	O
COW	O
)	O
,	O
sometimes	O
referred	O
to	O
as	O
implicit	B-Language
sharing	I-Language
or	O
shadowing	O
,	O
is	O
a	O
resource-management	O
technique	O
used	O
in	O
computer	B-General_Concept
programming	I-General_Concept
to	O
efficiently	O
implement	O
a	O
"	O
duplicate	O
"	O
or	O
"	O
copy	O
"	O
operation	O
on	O
modifiable	O
resources	O
.	O
</s>
<s>
Copy-on-write	B-Language
finds	O
its	O
main	O
use	O
in	O
sharing	O
the	O
virtual	B-Architecture
memory	I-Architecture
of	O
operating	B-General_Concept
system	I-General_Concept
processes	B-Operating_System
,	O
in	O
the	O
implementation	O
of	O
the	O
fork	B-Language
system	I-Language
call	I-Language
.	O
</s>
<s>
Typically	O
,	O
the	O
process	O
does	O
not	O
modify	O
any	O
memory	B-General_Concept
and	O
immediately	O
executes	O
a	O
new	O
process	O
,	O
replacing	O
the	O
address	O
space	O
entirely	O
.	O
</s>
<s>
Thus	O
,	O
it	O
would	O
be	O
wasteful	O
to	O
copy	O
all	O
of	O
the	O
process	O
's	O
memory	B-General_Concept
during	O
a	O
fork	B-Language
,	O
and	O
instead	O
the	O
copy-on-write	B-Language
technique	O
is	O
used	O
.	O
</s>
<s>
Copy-on-write	B-Language
can	O
be	O
implemented	O
efficiently	O
using	O
the	O
page	B-General_Concept
table	I-General_Concept
by	O
marking	O
certain	O
pages	O
of	O
memory	B-General_Concept
as	O
read-only	O
and	O
keeping	O
a	O
count	O
of	O
the	O
number	O
of	O
references	O
to	O
the	O
page	O
.	O
</s>
<s>
When	O
data	O
is	O
written	O
to	O
these	O
pages	O
,	O
the	O
operating-system	O
kernel	B-Operating_System
intercepts	O
the	O
write	O
attempt	O
and	O
allocates	O
a	O
new	O
physical	O
page	O
,	O
initialized	O
with	O
the	O
copy-on-write	B-Language
data	O
,	O
although	O
the	O
allocation	O
can	O
be	O
skipped	O
if	O
there	O
is	O
only	O
one	O
reference	O
.	O
</s>
<s>
The	O
kernel	B-Operating_System
then	O
updates	O
the	O
page	B-General_Concept
table	I-General_Concept
with	O
the	O
new	O
(	O
writable	O
)	O
page	O
,	O
decrements	O
the	O
number	O
of	O
references	O
,	O
and	O
performs	O
the	O
write	O
.	O
</s>
<s>
The	O
new	O
allocation	O
ensures	O
that	O
a	O
change	O
in	O
the	O
memory	B-General_Concept
of	O
one	O
process	O
is	O
not	O
visible	O
in	O
another	O
's	O
.	O
</s>
<s>
The	O
copy-on-write	B-Language
technique	O
can	O
be	O
extended	O
to	O
support	O
efficient	O
memory	B-General_Concept
allocation	I-General_Concept
by	O
having	O
a	O
page	O
of	O
physical	O
memory	B-General_Concept
filled	O
with	O
zeros	O
.	O
</s>
<s>
When	O
the	O
memory	B-General_Concept
is	O
allocated	O
,	O
all	O
the	O
pages	O
returned	O
refer	O
to	O
the	O
page	O
of	O
zeros	O
and	O
are	O
all	O
marked	O
copy-on-write	B-Language
.	O
</s>
<s>
This	O
way	O
,	O
physical	O
memory	B-General_Concept
is	O
not	O
allocated	O
for	O
the	O
process	O
until	O
data	O
is	O
written	O
,	O
allowing	O
processes	B-Operating_System
to	O
reserve	O
more	O
virtual	B-Architecture
memory	I-Architecture
than	O
physical	O
memory	B-General_Concept
and	O
use	O
memory	B-General_Concept
sparsely	O
,	O
at	O
the	O
risk	O
of	O
running	O
out	O
of	O
virtual	O
address	O
space	O
.	O
</s>
<s>
The	O
combined	O
algorithm	O
is	O
similar	O
to	O
demand	B-General_Concept
paging	I-General_Concept
.	O
</s>
<s>
Copy-on-write	B-Language
pages	O
are	O
also	O
used	O
in	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
's	O
same-page	B-Application
merging	I-Application
feature	O
.	O
</s>
<s>
COW	O
is	O
also	O
used	O
in	O
library	B-Library
,	O
application	B-Application
and	O
system	B-Application
code	O
.	O
</s>
<s>
The	O
string	B-Language
class	O
provided	O
by	O
the	O
C++	B-Language
standard	I-Language
library	I-Language
was	O
specifically	O
designed	O
to	O
allow	O
copy-on-write	B-Language
implementations	O
in	O
the	O
initial	O
C++98	O
standard	O
,	O
but	O
not	O
in	O
the	O
newer	O
C++11	O
standard	O
:	O
</s>
<s>
In	O
the	O
PHP	B-Application
programming	I-Application
language	I-Application
,	O
all	O
types	O
except	O
references	O
are	O
implemented	O
as	O
copy-on-write	B-Language
.	O
</s>
<s>
In	O
the	O
Qt	B-Language
framework	I-Language
,	O
many	O
types	O
are	O
copy-on-write	B-Language
(	O
"	O
implicitly	O
shared	O
"	O
in	O
Qt	B-Language
's	O
terms	O
)	O
.	O
</s>
<s>
Qt	B-Language
uses	O
atomic	O
compare-and-swap	B-Operating_System
operations	O
to	O
increment	O
or	O
decrement	O
the	O
internal	O
reference	O
counter	O
.	O
</s>
<s>
Since	O
the	O
copies	O
are	O
cheap	O
,	O
Qt	B-Language
types	O
can	O
often	O
be	O
safely	O
used	O
by	O
multiple	B-General_Concept
threads	I-General_Concept
without	O
the	O
need	O
of	O
locking	B-Operating_System
mechanisms	I-Operating_System
such	O
as	O
mutexes	B-Operating_System
.	O
</s>
<s>
COW	O
may	O
also	O
be	O
used	O
as	O
the	O
underlying	O
mechanism	O
for	O
snapshots	B-Application
,	O
such	O
as	O
those	O
provided	O
by	O
logical	B-Application
volume	I-Application
management	I-Application
,	O
file	O
systems	O
such	O
as	O
Btrfs	B-Operating_System
and	O
ZFS	B-Application
,	O
and	O
database	O
servers	O
such	O
as	O
Microsoft	O
SQL	O
Server	O
.	O
</s>
<s>
Typically	O
,	O
the	O
snapshots	B-Application
store	O
only	O
the	O
modified	O
data	O
,	O
and	O
are	O
stored	O
close	O
to	O
the	O
original	O
,	O
so	O
they	O
are	O
only	O
a	O
weak	O
form	O
of	O
incremental	B-General_Concept
backup	I-General_Concept
and	O
cannot	O
substitute	O
for	O
a	O
full	B-Protocol
backup	I-Protocol
.	O
</s>
