Shared Memory

Jump to navigation Jump to search

In today's modern operating systems, every Process believes to possess the complete available address space. On 32-bit systems, this means 4 GB of adressable space. This also means that no process can see another process' adress space.

On the one hand, this adds to a system's stability, as no process can accidently (or malevolently) crash another process by just overwriting and thus trashing memory used by that victim process. However, sometimes it is necessary for processes to share memory. Sharing memory for two processes means that there is a region of memory which both processes can access (read/write) such that both processes see the same contents of that memory.

Example: Consider two Processes A and B. Both have a shared memory region. In Process A, this region starts at address MA, in Process B it starts at address MB. Let A write the value 0x23 to MA+4, that is, 4 bytes into the shared region. Then Process B sees, in its own address space, the value 0x23 at MB+4.

For this to work, the Operating System needs to become involved. It has to map the memory into both processes' address space in an appropriate manner.

Note that certain problems arise when shared memory is involved. For example, Race Conditions can occur. Thus, in most applications, it is essential that the processes be synchronized in some way. See also Synchronization.

In systems implementing the Win32 API, shared memory is implemented by use of Memory Mapped Files (MMFs, sometimes also called Sections). In these systems, most of the Inter Process Communication methods are internally implemented using MMFs.