Shared memory in Linux is an inter-process communication (IPC) mechanism in which two or more processes map the same region of physical RAM into their own virtual address spaces. Because the processes read and write the same bytes directly, the kernel does not copy data between them. That is why shared memory is the fastest IPC mechanism on a single machine. The trade-off: you must handle synchronization yourself.In Linux, applications often need to exchange data while running as separate processes. For example, one process may collect sensor readings while another processes the data and displays the results. Shared memory provides an efficient way for these processes to access the same memory region without repeatedly transferring data through traditional inter-process communication (IPC) mechanisms.
Understanding what shared memory in Linux is helps developers build faster and more efficient applications, particularly in embedded Linux, real-time data processing, and multimedia systems. This article covers how shared memory works, why it is often considered the fastest IPC mechanism, its types, practical C programming examples, and the best practices for using it safely.
Shared memory in Linux is a region of RAM that multiple processes map into their address spaces at the same time. Once the mapping exists, each process reads and writes it with ordinary pointer operations. No read() or write() system call and no kernel buffer sits in between.
Here is an everyday way to picture it:
Process A (virtual) Process B (virtual)
+-------------------+ +-------------------+
| stack | | stack |
| shared region ----+-----> [ same ] <-------+---- shared region |
| heap | [ physical ] | heap |
| code | [ page(s) ] | code |
+-------------------+ +-------------------+Shared memory is the fastest IPC mechanism because processes access the same physical memory directly. Pipes, sockets and message queues copy every message from the sender into a kernel buffer and then into the receiver, using a system call on each side. Shared memory removes those copies and system calls from the data path.
Pipe, socket or message queue:
Sender buffer --copy 1--> Kernel buffer --copy 2--> Receiver buffer
(write() syscall) (read() syscall)
Shared memory:
Sender --store--> [ shared page ] <--load-- Receiver
(no kernel copy, no syscall per message)Let’s build the smallest useful example: one process writes a message, another reads it.
#include
#include
#include
#include <sys/mman.h>
#include <sys/stat.h>
#include
#define SHM_NAME "/iies_demo"
#define SHM_SIZE 4096
int main(void) {
int fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0600);
if (fd == -1) { perror("shm_open"); return 1; }
if (ftruncate(fd, SHM_SIZE) == -1) { perror("ftruncate"); return 1; }
char *ptr = mmap(NULL, SHM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED) { perror("mmap"); return 1; }
strcpy(ptr, "Hello from the writer process!");
puts("Writer: message placed in shared memory");
munmap(ptr, SHM_SIZE);
close(fd);
return 0;
}#include
#include
#include <sys/mman.h>
#include <sys/stat.h>
#include
#define SHM_NAME "/iies_demo"
#define SHM_SIZE 4096
int main(void) {
int fd = shm_open(SHM_NAME, O_RDONLY, 0);
if (fd == -1) { perror("shm_open"); return 1; }
char *ptr = mmap(NULL, SHM_SIZE, PROT_READ, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED) { perror("mmap"); return 1; }
puts(ptr);
munmap(ptr, SHM_SIZE);
close(fd);
shm_unlink(SHM_NAME); /* remove the name when finished */
return 0;
}gcc writer.c -o writer
gcc reader.c -o reader # add -lrt on glibc older than 2.34
./writer
ls -l /dev/shm/iies_demo # the object exists even though the writer exited
./readerSo, what is shared memory in Linux? It is one set of physical pages that several processes map into their own address spaces, giving them a direct, copy-free channel to exchange data. That is exactly why shared memory is the fastest IPC mechanism on a single machine, and why it comes with the extra responsibility of synchronization and cleanup.
Ready to go deeper into Linux internals and embedded systems on real hardware? Explore what IIES offers in embedded systems and build these skills hands-on.
Yes, for bulk data. A pipe copies each message into and out of a kernel buffer using system calls, while shared memory lets both processes access the same pages directly. For tiny, infrequent messages the difference is small.
Yes. The kernel does not coordinate access, so you must use mutexes, semaphores or atomics to avoid race conditions.
System V uses numeric keys with shmget and shmat, and is limited by kernel parameters such as shmmax. POSIX uses named objects in /dev/shm with file descriptors via shm_open and mmap, which fits better with modern Linux tools.
Use ipcs -m and ipcrm -m <shmid> for System V segments. For POSIX objects, list /dev/shm and remove them with shm_unlink() or rm.
Threads in one process already share the same address space, so they do not need shared memory APIs. Shared memory is for communication between separate processes.
System V is capped by shmmax and shmall. POSIX shared memory is limited by the size of the /dev/shm tmpfs (half of RAM by default) and available memory.
Indian Institute of Embedded Systems – IIES