Understanding Shared Memory in Linux: Concepts, APIs, and Practical Examples
This article explains the fundamentals of shared memory as an efficient inter‑process communication method on Linux, covering its principles, core system calls, usage steps, advantages, synchronization challenges, and real‑world case studies with complete C code examples.
Imagine a large library where each person (process) has a private desk (memory space) but can also use a huge common table (shared memory) to place and retrieve data, dramatically improving communication efficiency compared to passing notes.
1. Shared Memory Overview
Shared memory allows multiple processes to access the same memory region, eliminating the need for data copying and providing fast IPC. It is a global resource that requires explicit synchronization (e.g., semaphores, mutexes) to avoid race conditions.
2. Core Principles
In Linux each process has its own virtual address space, but shared memory maps a single physical region into the address spaces of participating processes. The OS allocates a physical block, returns an identifier (shmid), and processes map it using shmat , enabling direct read/write without extra copies.
3. Using Shared Memory on Linux
3.1 Key Functions
shmget : create or obtain a shared memory segment.
shmat : attach the segment to a process’s address space.
shmdt : detach the segment.
shmctl : control operations such as removal.
3.2 Creating and Getting a Segment
Generate a unique key with ftok and call shmget :
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
int main() {
key_t key = ftok(".", 'a');
if (key == -1) { perror("ftok"); return 1; }
int shmid = shmget(key, 1024, IPC_CREAT | 0666);
if (shmid == -1) { perror("shmget"); return 1; }
printf("Shared memory created with shmid: %d\n", shmid);
return 0;
}3.3 Attaching and Accessing
Map the segment and read/write like normal memory:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <string.h>
int main() {
key_t key = ftok(".", 'a');
int shmid = shmget(key, 1024, IPC_CREAT | 0666);
char *addr = (char *)shmat(shmid, NULL, 0);
if (addr == (char *)-1) { perror("shmat"); return 1; }
strcpy(addr, "Hello, shared memory!");
printf("Data written: %s\n", addr);
shmdt(addr);
return 0;
}3.4 Detaching and Deleting
After use, detach with shmdt and remove the segment with shmctl :
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
int main() {
key_t key = ftok(".", 'a');
int shmid = shmget(key, 1024, IPC_CREAT | 0666);
char *addr = (char *)shmat(shmid, NULL, 0);
shmdt(addr);
shmctl(shmid, IPC_RMID, NULL);
printf("Shared memory detached and deleted.\n");
return 0;
}4. Advantages and Common Issues
4.1 Efficiency
Shared memory avoids multiple data copies, offering performance comparable to a highway versus the slower roads of pipes or message queues. It also supports DMA, allowing hardware to access the memory directly without CPU intervention.
4.2 Synchronization & Mutual Exclusion
Because the kernel does not provide built‑in synchronization, developers must use semaphores, mutexes, or other IPC mechanisms to prevent race conditions, especially in producer‑consumer scenarios.
4.3 Memory Management Complexity
Proper lifecycle handling is crucial: the segment must remain until all processes finish, and reference counting or explicit cleanup is needed to avoid leaks. Allocation size should be chosen carefully to prevent fragmentation.
5. Real‑World Case Studies
5.1 Producer‑Consumer Model
One process writes data into shared memory while another reads it, coordinated by semaphores to ensure correct ordering and data integrity.
5.2 Multi‑Process Data Sharing
Large data sets (e.g., query results) can be stored in shared memory so multiple analysis processes can read without redundant copying, improving throughput.
5.3 Video Surveillance System
Capture processes place video frames into shared memory; processing processes perform real‑time analysis (face detection, behavior monitoring). Proper sizing, synchronization, and optional encryption are required for performance and security.
Deepin Linux
Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.