Shared memory

From Wikipedia, the free encyclopedia
Jump to: navigation, search
An illustration of a shared memory system of three computers


Using memory for communication inside a single program, for example among its multiple threads, is also referred to as shared memory.

In hardware[edit]



  • 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 (see cache coherence and memory coherence). 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[edit]

In computer software, shared memory is either


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 powerful, as for example the communicating processes must be running on the same machine (of other IPC methods only Internet Domain sockets (not UNIX 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.



Support on UNIX platforms[edit]

POSIX provides a standardized API for using shared memory, POSIX Shared Memory. This uses the function shm_open from sys/mman.h.[1] POSIX interprocess communication (part of the POSIX:XSI Extension) includes the shared-memory functions shmat, shmctl, shmdt and shmget.[2][3] 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.[4] 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.


Support on other platforms[edit]




See also[edit]


  1. ^ Documentation of shm_open from the Single UNIX Specification
  2. ^ Robbins, Kay A.; Steven Robbins (2003). UNIX systems programming: communication, concurrency, and threads (2 ed.). Prentice Hall PTR. p. 512. ISBN 978-0-13-042411-2. Retrieved 2011-05-13. "The POSIX interprocess communication (IPC) is part of the POSIX:XSI Extension and has its origin in UNIX System V interprocess communication." 
  3. ^ Shared memory facility from the Single UNIX Specification.
  4. ^ Stevens, Richard (1999). UNIX Network Programming, Volume 2, Second Edition: Interprocess Communications. (2 ed.). Prentice Hall PTR. p. 311. ISBN 0-13-081081-9. 
  5. ^ Christoph Rohland, Hugh Dickins, KOSAKI Motohiro. "tmpfs.txt". Retrieved 2010-03-16. 
  6. ^ CreateSharedMemory function from Win32-SDK
  7. ^ Creating Named Shared Memory from MSDN.
  8. ^ Boost.Interprocess C++ Library
  9. ^ QSharedMemory Class Reference
  10. ^ Shared Memory Functions in PHP-API

External links[edit]