<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
shared	B-Operating_System
memory	I-Operating_System
is	O
memory	B-Architecture
that	O
may	O
be	O
simultaneously	O
accessed	O
by	O
multiple	O
programs	O
with	O
an	O
intent	O
to	O
provide	O
communication	O
among	O
them	O
or	O
avoid	O
redundant	O
copies	O
.	O
</s>
<s>
Shared	B-Operating_System
memory	I-Operating_System
is	O
an	O
efficient	O
means	O
of	O
passing	O
data	O
between	O
programs	O
.	O
</s>
<s>
Using	O
memory	B-Architecture
for	O
communication	O
inside	O
a	O
single	O
program	O
,	O
e.g.	O
</s>
<s>
among	O
its	O
multiple	O
threads	B-Operating_System
,	O
is	O
also	O
referred	O
to	O
as	O
shared	B-Operating_System
memory	I-Operating_System
.	O
</s>
<s>
In	O
computer	O
hardware	O
,	O
shared	B-Operating_System
memory	I-Operating_System
refers	O
to	O
a	O
(	O
typically	O
large	O
)	O
block	O
of	O
random	B-Architecture
access	I-Architecture
memory	I-Architecture
(	O
RAM	B-Architecture
)	O
that	O
can	O
be	O
accessed	O
by	O
several	O
different	O
central	B-General_Concept
processing	I-General_Concept
units	I-General_Concept
(	O
CPUs	B-Device
)	O
in	O
a	O
multiprocessor	B-Operating_System
computer	I-Operating_System
system	I-Operating_System
.	O
</s>
<s>
Shared	B-Operating_System
memory	I-Operating_System
systems	O
may	O
use	O
:	O
</s>
<s>
uniform	B-Operating_System
memory	I-Operating_System
access	I-Operating_System
(	O
UMA	O
)	O
:	O
all	O
the	O
processors	O
share	O
the	O
physical	O
memory	B-Architecture
uniformly	O
;	O
</s>
<s>
non-uniform	B-Operating_System
memory	I-Operating_System
access	I-Operating_System
(	O
NUMA	O
)	O
:	O
memory	B-Architecture
access	O
time	O
depends	O
on	O
the	O
memory	B-Architecture
location	O
relative	O
to	O
a	O
processor	O
;	O
</s>
<s>
cache-only	B-Operating_System
memory	I-Operating_System
architecture	I-Operating_System
(	O
COMA	O
)	O
:	O
the	O
local	O
memories	O
for	O
the	O
processors	O
at	O
each	O
node	O
is	O
used	O
as	O
cache	B-General_Concept
instead	O
of	O
as	O
actual	O
main	O
memory	B-Architecture
.	O
</s>
<s>
A	O
shared	B-Operating_System
memory	I-Operating_System
system	O
is	O
relatively	O
easy	O
to	O
program	O
since	O
all	O
processors	O
share	O
a	O
single	O
view	O
of	O
data	O
and	O
the	O
communication	O
between	O
processors	O
can	O
be	O
as	O
fast	O
as	O
memory	B-Architecture
accesses	O
to	O
the	O
same	O
location	O
.	O
</s>
<s>
The	O
issue	O
with	O
shared	B-Operating_System
memory	I-Operating_System
systems	O
is	O
that	O
many	O
CPUs	B-Device
need	O
fast	O
access	O
to	O
memory	B-Architecture
and	O
will	O
likely	O
cache	B-General_Concept
memory	I-General_Concept
,	O
which	O
has	O
two	O
complications	O
:	O
</s>
<s>
access	O
time	O
degradation	O
:	O
when	O
several	O
processors	O
try	O
to	O
access	O
the	O
same	O
memory	B-Architecture
location	O
it	O
causes	O
contention	O
.	O
</s>
<s>
Trying	O
to	O
access	O
nearby	O
memory	B-Architecture
locations	O
may	O
cause	O
false	B-General_Concept
sharing	I-General_Concept
.	O
</s>
<s>
Shared	B-Operating_System
memory	I-Operating_System
computers	O
cannot	O
scale	O
very	O
well	O
.	O
</s>
<s>
lack	O
of	O
data	O
coherence	O
:	O
whenever	O
one	O
cache	B-General_Concept
is	O
updated	O
with	O
information	O
that	O
may	O
be	O
used	O
by	O
other	O
processors	O
,	O
the	O
change	O
needs	O
to	O
be	O
reflected	O
to	O
the	O
other	O
processors	O
,	O
otherwise	O
the	O
different	O
processors	O
will	O
be	O
working	O
with	O
incoherent	O
data	O
.	O
</s>
<s>
Such	O
cache	B-General_Concept
coherence	I-General_Concept
protocols	O
can	O
,	O
when	O
they	O
work	O
well	O
,	O
provide	O
extremely	O
high-performance	O
access	O
to	O
shared	O
information	O
between	O
multiple	O
processors	O
.	O
</s>
<s>
Technologies	O
like	O
crossbar	O
switches	O
,	O
Omega	B-Architecture
networks	I-Architecture
,	O
HyperTransport	B-Device
or	O
front-side	B-Architecture
bus	I-Architecture
can	O
be	O
used	O
to	O
dampen	O
the	O
bottleneck-effects	O
.	O
</s>
<s>
In	O
case	O
of	O
a	O
Heterogeneous	B-Architecture
System	I-Architecture
Architecture	I-Architecture
(	O
processor	O
architecture	O
that	O
integrates	O
different	O
types	O
of	O
processors	O
,	O
such	O
as	O
CPUs	B-Device
and	O
GPUs	B-Architecture
,	O
with	O
shared	B-Operating_System
memory	I-Operating_System
)	O
,	O
the	O
memory	B-General_Concept
management	I-General_Concept
unit	I-General_Concept
(	O
MMU	B-General_Concept
)	O
of	O
the	O
CPU	B-Device
and	O
the	O
input	B-General_Concept
–	I-General_Concept
output	I-General_Concept
memory	I-General_Concept
management	I-General_Concept
unit	I-General_Concept
(	O
IOMMU	B-General_Concept
)	O
of	O
the	O
GPU	B-Architecture
have	O
to	O
share	O
certain	O
characteristics	O
,	O
like	O
a	O
common	O
address	O
space	O
.	O
</s>
<s>
The	O
alternatives	O
to	O
shared	B-Operating_System
memory	I-Operating_System
are	O
distributed	B-Operating_System
memory	I-Operating_System
and	O
distributed	B-Operating_System
shared	I-Operating_System
memory	I-Operating_System
,	O
each	O
having	O
a	O
similar	O
set	O
of	O
issues	O
.	O
</s>
<s>
a	O
method	O
of	O
inter-process	B-Operating_System
communication	I-Operating_System
(	O
IPC	O
)	O
,	O
i.e.	O
</s>
<s>
One	O
process	B-Operating_System
will	O
create	O
an	O
area	O
in	O
RAM	B-Architecture
which	O
other	O
processes	O
can	O
access	O
;	O
</s>
<s>
a	O
method	O
of	O
conserving	O
memory	B-Architecture
space	O
by	O
directing	O
accesses	O
to	O
what	O
would	O
ordinarily	O
be	O
copies	O
of	O
a	O
piece	O
of	O
data	O
to	O
a	O
single	O
instance	O
instead	O
,	O
by	O
using	O
virtual	B-Architecture
memory	I-Architecture
mappings	O
or	O
with	O
explicit	O
support	O
of	O
the	O
program	O
in	O
question	O
.	O
</s>
<s>
This	O
is	O
most	O
often	O
used	O
for	O
shared	O
libraries	O
and	O
for	O
Execute	B-Application
in	I-Application
place	I-Application
(	O
XIP	B-Application
)	O
.	O
</s>
<s>
Since	O
both	O
processes	O
can	O
access	O
the	O
shared	B-Operating_System
memory	I-Operating_System
area	O
like	O
regular	O
working	O
memory	B-Architecture
,	O
this	O
is	O
a	O
very	O
fast	O
way	O
of	O
communication	O
(	O
as	O
opposed	O
to	O
other	O
mechanisms	O
of	O
IPC	O
such	O
as	O
named	B-Operating_System
pipes	I-Operating_System
,	O
Unix	B-Protocol
domain	I-Protocol
sockets	I-Protocol
or	O
CORBA	B-Architecture
)	O
.	O
</s>
<s>
On	O
the	O
other	O
hand	O
,	O
it	O
is	O
less	O
scalable	O
,	O
as	O
for	O
example	O
the	O
communicating	O
processes	O
must	O
be	O
running	O
on	O
the	O
same	O
machine	O
(	O
of	O
other	O
IPC	O
methods	O
,	O
only	O
Internet	O
domain	O
sockets	O
—	O
not	O
Unix	B-Protocol
domain	I-Protocol
sockets	I-Protocol
—	O
can	O
use	O
a	O
computer	B-Architecture
network	I-Architecture
)	O
,	O
and	O
care	O
must	O
be	O
taken	O
to	O
avoid	O
issues	O
if	O
processes	O
sharing	O
memory	B-Architecture
are	O
running	O
on	O
separate	O
CPUs	B-Device
and	O
the	O
underlying	O
architecture	O
is	O
not	O
cache	B-General_Concept
coherent	I-General_Concept
.	O
</s>
<s>
IPC	O
by	O
shared	B-Operating_System
memory	I-Operating_System
is	O
used	O
for	O
example	O
to	O
transfer	O
images	O
between	O
the	O
application	O
and	O
the	O
X	B-Operating_System
server	I-Operating_System
on	O
Unix	O
systems	O
,	O
or	O
inside	O
the	O
IStream	O
object	O
returned	O
by	O
CoMarshalInterThreadInterfaceInStream	O
in	O
the	O
COM	O
libraries	O
under	O
Windows	B-Application
.	O
</s>
<s>
Dynamic	O
libraries	O
are	O
generally	O
held	O
in	O
memory	B-Architecture
once	O
and	O
mapped	O
to	O
multiple	O
processes	O
,	O
and	O
only	O
pages	O
that	O
had	O
to	O
be	O
customized	O
for	O
the	O
individual	O
process	B-Operating_System
(	O
because	O
a	O
symbol	O
resolved	O
differently	O
there	O
)	O
are	O
duplicated	O
,	O
usually	O
with	O
a	O
mechanism	O
known	O
as	O
copy-on-write	B-Language
that	O
transparently	O
copies	O
the	O
page	O
when	O
a	O
write	O
is	O
attempted	O
,	O
and	O
then	O
lets	O
the	O
write	O
succeed	O
on	O
the	O
private	O
copy	O
.	O
</s>
<s>
is	O
simpler	O
in	O
single	B-Operating_System
address	I-Operating_System
space	I-Operating_System
operating	I-Operating_System
systems	I-Operating_System
.	O
</s>
<s>
POSIX	O
provides	O
a	O
standardized	O
API	B-General_Concept
for	O
using	O
shared	B-Operating_System
memory	I-Operating_System
,	O
POSIX	O
Shared	B-Operating_System
Memory	I-Operating_System
.	O
</s>
<s>
POSIX	O
interprocess	B-Operating_System
communication	I-Operating_System
(	O
part	O
of	O
the	O
POSIX:XSI	O
Extension	O
)	O
includes	O
the	O
shared-memory	B-Operating_System
functions	O
shmat	O
,	O
shmctl	O
,	O
shmdt	O
and	O
shmget	O
.	O
</s>
<s>
Unix	O
System	O
V	O
provides	O
an	O
API	B-General_Concept
for	O
shared	B-Operating_System
memory	I-Operating_System
as	O
well	O
.	O
</s>
<s>
This	O
uses	O
shmget	O
from	O
sys/shm.h.	O
</s>
<s>
BSD	O
systems	O
provide	O
"	O
anonymous	O
mapped	O
memory	B-Architecture
"	O
which	O
can	O
be	O
used	O
by	O
several	O
processes	O
.	O
</s>
<s>
The	O
shared	B-Operating_System
memory	I-Operating_System
created	O
by	O
shm_open	O
is	O
persistent	O
.	O
</s>
<s>
It	O
stays	O
in	O
the	O
system	O
until	O
explicitly	O
removed	O
by	O
a	O
process	B-Operating_System
.	O
</s>
<s>
This	O
has	O
a	O
drawback	O
in	O
that	O
if	O
the	O
process	B-Operating_System
crashes	O
and	O
fails	O
to	O
clean	O
up	O
shared	B-Operating_System
memory	I-Operating_System
it	O
will	O
stay	O
until	O
system	O
shutdown	O
;	O
that	O
limitation	O
is	O
not	O
present	O
in	O
an	O
Android-specific	O
implementation	O
dubbed	O
ashmem	O
.	O
</s>
<s>
POSIX	O
also	O
provides	O
the	O
mmap	B-Operating_System
API	B-General_Concept
for	O
mapping	O
files	O
into	O
memory	B-Architecture
;	O
a	O
mapping	O
can	O
be	O
shared	O
,	O
allowing	O
the	O
file	O
's	O
contents	O
to	O
be	O
used	O
as	O
shared	B-Operating_System
memory	I-Operating_System
.	O
</s>
<s>
Linux	O
distributions	O
based	O
on	O
the	O
2.6	O
kernel	O
and	O
later	O
offer	O
/dev/shm	O
as	O
shared	B-Operating_System
memory	I-Operating_System
in	O
the	O
form	O
of	O
a	O
RAM	B-Application
disk	I-Application
,	O
more	O
specifically	O
as	O
a	O
world-writable	O
directory	O
(	O
a	O
directory	O
in	O
which	O
every	O
user	O
of	O
the	O
system	O
can	O
create	O
files	O
)	O
that	O
is	O
stored	O
in	O
memory	B-Architecture
.	O
</s>
<s>
Support	O
for	O
this	O
type	O
of	O
RAM	B-Application
disk	I-Application
is	O
completely	O
optional	O
within	O
the	O
kernel	O
configuration	B-Protocol
file	I-Protocol
.	O
</s>
<s>
On	O
Windows	B-Application
,	O
one	O
can	O
use	O
CreateFileMapping	O
and	O
MapViewOfFile	O
functions	O
to	O
map	O
a	O
region	O
of	O
a	O
file	O
into	O
memory	B-Architecture
in	O
multiple	O
processes	O
.	O
</s>
<s>
Some	O
C++	O
libraries	O
provide	O
a	O
portable	O
and	O
object-oriented	O
access	O
to	O
shared	B-Operating_System
memory	I-Operating_System
functionality	O
.	O
</s>
<s>
For	O
example	O
,	O
Boost	B-Language
contains	O
the	O
Boost.Interprocess	O
C++	O
Library	O
and	O
Qt	B-Language
provides	O
the	O
QSharedMemory	O
class	O
.	O
</s>
<s>
For	O
programming	O
languages	O
with	O
POSIX	O
bindings	O
(	O
say	O
,	O
C/C	O
++	O
)	O
,	O
shared	B-Operating_System
memory	I-Operating_System
regions	O
can	O
be	O
created	O
and	O
accessed	O
by	O
calling	O
the	O
functions	O
provided	O
by	O
the	O
operating	O
system	O
.	O
</s>
<s>
For	O
example	O
,	O
PHP	B-Application
provides	O
an	O
API	B-General_Concept
to	O
create	O
shared	B-Operating_System
memory	I-Operating_System
,	O
similar	O
to	O
POSIX	O
functions	O
.	O
</s>
