What Is Shared Memory in Linux?
Shared memory is a way for different elements of a computer system to communicate with one another by reading and writing to the same block of RAM. The elements could be different execution threads within the same program, different programs, or programs and hardware devices. Systemwide shared memory was the norm in the early days of computing. Modern protected mode operating systems such as Linux do not normally allow different programs to access one another's memory for security and stability reasons, unless specific procedures are followed to explicitly allow it.
-
History
-
All memory was always shared by all programs in early, "real mode" operating systems such as DOS. Any program could read or write any other program's memory at any time without restriction. Authors of DOS TSRs-so called "terminate and stay resident" programs used this feature in many clever ways to simulate multitasking and extend the functionality of other programs without access to the original source code. This feature also allowed viruses to propagate very easily, offered programs no security against the inspection of their data by other programs, and made it easy to crash the entire system through a small programming error that accidentally overwrote the memory of some other processes.
Protected Mode
-
Intel's engineers recognized the security and stability problems created by systemwide shared memory, so they developed "protected mode," which first appeared on 80286 chips. Protected mode introduces the concept of "rings," or privilege levels. Ring 0 processes, intended for the operating system kernel, can read and write any memory. Ring 3 processes, intended for ordinary programs, are stopped from reading and writing each other's memory by the hardware. Attempting to do so results in a "segmentation fault" error and usually terminates the offending program.
-
Modern Shared Memory
-
Protected mode prevented many of the problems caused by systemwide shared memory in real mode, but it is still sometimes desirable for a program to be able to read and write memory that is shared with some other program or device. Operating systems such as Linux, therefore, added support for explicitly shared memory. Programs and devices can choose to share memory with one another, subject to certain restrictions. Threads of execution within one program share their memory by default.
Parallel Computing
-
Many computers contain more than one CPU core. Programs can split themselves into multiple "threads" of execution, each of which can execute at the same time, to speed up the overall operations required on some data. They often need to operate on the same data simultaneously or communicate their state to one another, and shared memory is a popular way to do this.
Dynamic Libraries
-
Commonly used dynamic libraries such as libc are typically loaded by many different programs. Since the object code in these libraries is usually used in a read-only way by programs, significant memory savings can be achieved with shared memory. The dynamic library can be loaded only once by the operating system and shared among any number of programs, decreasing overall memory usage.
-
References
- Photo Credit Memory of a computer. Close up. Isolated on a white background. image by Andrey Khritin from Fotolia.com