Definition
Shared Memory
Shared memory is an inter-process communication (IPC) mechanism where a region of physical memory is mapped into the address space of two or more processes. This allows the processes to communicate by reading and writing to the same memory locations.
Shared memory is typically the fastest form of IPC because it avoids the overhead of copying data between process address spaces and the kernel.
Mechanism
- Creation: One process creates a shared memory segment.
- Mapping: Other processes attach (map) this segment into their own virtual address space.
- Access: Processes read and write to the memory as if it were local data.
Synchronisation Requirement
Because multiple processes can access the shared memory simultaneously, the operating system does not provide automatic protection against concurrent access. It is the responsibility of the application programmer to use synchronisation primitives to maintain consistency:
- Mutual Exclusion: Ensuring only one process writes at a time (e.g., using Semaphores).
- Condition Synchronisation: Coordinating the order of reads and writes (e.g., ensuring a consumer does not read until a producer has written).
Failure to synchronise properly leads to race conditions.