Shared memory
In computer science, 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, e.g. 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 multiprocessor computer system.Shared memory systems may use:
- uniform memory access : all the processors share the physical memory uniformly;
- non-uniform memory access : memory access time depends on the memory location relative to a processor;
- cache-only memory architecture : the local memories for the processors at each node is used as cache instead of as actual main memory.
- access time degradation: when several processors try to access the same memory location it causes contention. Trying to access nearby memory locations may cause false sharing. Shared memory computers cannot scale very well. Most of them have ten or fewer processors;
- lack of data 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 cache 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.
In case of a Heterogeneous System Architecture, the memory management unit of the CPU and the input–output memory management unit of the GPU have to share certain characteristics, like a common address space.
The alternatives to shared memory are distributed memory and distributed shared memory, each having a similar set of issues.
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;
- 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.
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.
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
provides a standardized API for using shared memory, POSIX Shared Memory. This uses the functionshm_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 in that if the process crashes and fails to clean up shared memory it will stay until system shutdown; that limitation is not present in an Android-specific implementation dubbed ashmem.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 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 Windows
On Windows, one can useCreateFileMapping and MapViewOfFile functions to map a region of a file into memory in multiple processes.