In computing, shared memory is memory that may be simultaneously accessed by multiple
programs with an intent to provide communication among them or avoid redundant copies.
Shared memory is an efficient means of passing data between programs.
Depending on context, programs may run on a single processor or on multiple separate
processors.
Using memory for communication inside a single program, for example among its multiple threads,
is also referred to as shared memory.
In hardware In computer hardware, shared memory refers
to a block of random access memory that can be accessed by several different central processing
units in a multiple-processor computer system.
A shared memory system is relatively easy to program since all processors share a single
view of data and the communication between processors can be as fast as memory accesses
to a same location.
The issue with shared memory systems is that many CPUs need fast access to memory and will
likely cache memory, which has two complications: CPU-to-memory connection becomes a bottleneck.
Shared memory computers cannot scale very well.
Most of them have ten or fewer processors.
Cache coherence: Whenever one cache is updated with information that may be used by other
processors, the change needs to be reflected to the other processors, otherwise the different
processors will be working with incoherent data.
Such coherence protocols can, when they work well, provide extremely high-performance access
to shared information between multiple processors.
On the other hand they can sometimes become overloaded and become a bottleneck to performance.
Technologies like crossbar switches, Omega networks, HyperTransport or Front-side bus
can be used to dampen the bottleneck-effects.
The alternatives to shared memory are distributed memory and distributed shared memory, each
having a similar set of issues.
See also Non-Uniform Memory Access.
In software In computer software, shared memory is either
a method of inter-process communication, i.e. a way of exchanging data between programs
running at the same time.
One process will create an area in RAM which other processes can access, or
a method of conserving memory space by directing accesses to what would ordinarily be copies
of a piece of data to a single instance instead, by using virtual memory mappings or with explicit
support of the program in question.
This is most often used for shared libraries and for XIP.
Since both processes can access the shared memory area like regular working memory, this
is a very fast way of communication.
On the other hand, it is less scalable, as for example the communicating processes must
be running on the same machine can use a computer network), and care must be taken to avoid
issues if processes sharing memory are running on separate CPUs and the underlying architecture
is not cache coherent.
IPC by shared memory is used for example to transfer images between the application and
the X server on Unix systems, or inside the IStream object returned by CoMarshalInterThreadInterfaceInStream
in the COM libraries under Windows.
Dynamic libraries are generally held in memory once and mapped to multiple processes, and
only pages that had to be customized for the individual process are duplicated, usually
with a mechanism known as copy-on-write that transparently copies the page when a write
is attempted, and then lets the write succeed on the private copy.
Support on UNIX platforms POSIX provides a standardized API for using
shared memory, POSIX Shared Memory.
This uses the function shm_open from sys/mman.h.
POSIX interprocess communication includes the shared-memory functions shmat, shmctl,
shmdt and shmget.
UNIX System V provides an API for shared memory as well.
This uses shmget from sys/shm.h.
BSD systems provide "anonymous mapped memory" which can be used by several processes.
The shared memory created by shm_open is persistent.
It stays in the system until explicitly removed by a process.
This has a drawback that if the process crashes and fails to clean up shared memory it will
stay until system shutdown.
To avoid this issue mmap can be used to create a shared memory.
Two communicating processes should open a temporary file with the same name and do mmap
on it to get a file mapping in the memory.
As a result changes in mapped memory are visible by both processes at the same time.
The advantage of these approaches is that when both processes exit, OS will automatically
close the files and remove shared memory.
Recent Linux distributions based on the 2.6 kernel have started to offer shm as shared
memory in the form of a RAM disk, more specifically as a world-writable directory that is stored
in memory.
Both the RedHat and Debian based distributions include it by default.
Support for this type of RAM disk is completely optional within the kernel configuration file.
Support on other platforms On Windows the function CreateSharedMemory
can be used to create a shared memory.
Alternatively one can use CreateFileMapping and MapViewOfFile functions
Some C++ libraries provide a portable and object-oriented access to shared memory functionality.
For example, Boost contains Boost.Interprocess C++ Library.
Qt provides QSharedMemory class.
There is native support for shared memory also in programming languages besides C/C++.
For example, PHP provides API to create a shared memory, similar to POSIX functions.
See also Distributed shared memory
Global variable Nano-threads
Shared graphics memory Shared memory architecture
Execute in place References
External links IPC:Shared Memory by Dave Marshall
Shared Memory Introduction, Ch. 12 from book by Richard Stevens "UNIX Network Programming,
Volume 2, Second Edition: Interprocess Communications".
SharedHashFile, An open source, shared memory hash table.