IPC: Shared Memory Overview on Linux Kernel.
Key points:
- Shared memory mechanism allows processes to share a memory area with other processes.
- Shared memory is an IPC resource and is peristent. This means shared memory will be available to processes till the system is up or the shared memory is explicity removed by some process using appropriate system call.
- Linux Kernel associates each IPC resource (Shared memory here) with an unique 32-bit IPC identifier (shmget() takes an IPC key as input and return this IPC identifier).
- This IPC identifier can be used by processes to communicate with each other through the shared memory (or any other corresponding IPC resource).
- A processes that need to access a shared memory region is required to add a new memory region to its address space (equivalent to mapping the page frames of shared memory). Each process can map the shared memory to a different address (than other processes) in its virtual address space.
***
- Each IPC shared memory region is associated with a file belonging to the "shm" special filesystem. The "shm" filesystem has no mount point in the system directory tree, which effectively means: files on "shm" cannot be accessesed through regular Virtual File System calls. To access a "shm" file, a process needs to attach the shared memory (shmat()) which creates a shared memory mapping (mmap) of the file in the address space of the process.
- Pages belonging to a shared memory region can be swapped out. So to free memory under tight memory situation, swap area can be used to reclaim the page used by shared memory. The point to be taken care of is: since the shared memory region is persistent, kernel cannot discard these pages even when they are no longer used by any process.
Reference: Understanding Linux Kernel, Bovet & Cesati
