In computer software,
shared memory is either • a method of
inter-process communication (IPC), 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; • 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
Execute in place (XIP). Since both processes can access the shared memory area like regular working memory, this is a very fast way of communication (as opposed to other mechanisms of IPC such as
named pipes,
Unix domain sockets or
CORBA). On the other hand, it is less scalable, as for example the communicating processes must be running on the same machine (of other IPC methods, only Internet domain sockets—not Unix domain sockets—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 (because a symbol resolved differently there) 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. Compared to multiple address space operating systems, memory sharing -- especially of sharing procedures or pointer-based structures -- is simpler in
single address space operating systems.
Support on Unix-like systems 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 (part of the POSIX:XSI Extension) includes the shared-memory functions shmat, shmctl, shmdt and shmget. POSIX also provides the
mmap API for mapping files into memory; a mapping can be shared, allowing the file's contents to be used as shared memory. Linux distributions based on the 2.6 kernel and later offer /dev/shm as shared memory in the form of a
RAM disk, more specifically as a world-writable directory (a directory in which every user of the system can create files) 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.
Cross-platform support Some C++ libraries provide a portable and object-oriented access to shared memory functionality. For example,
Boost contains the Boost.Interprocess C++ Library and
Qt provides the QSharedMemory class.
Programming language support For programming languages with POSIX bindings (say, C/C++), shared memory regions can be created and accessed by calling the functions provided by the operating system. Other programming languages may have their own ways of using these operating facilities for similar effect. For example,
PHP provides an
API to create shared memory, similar to
POSIX functions. ==See also==