Shared Memory
Fast IPC.
Shared Memory is a free C Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Fast IPC With Shared Memory
Shared memory lets multiple processes map the same region of physical memory into their address spaces. It is the fastest form of IPC because data is not copied through the kernel.
Why It Is Fast
Pipes and sockets copy data from one process to the kernel and back. Shared memory skips this: once mapped, processes read and write the same bytes directly, with no copy per message.
POSIX vs System V
There are two APIs:
- POSIX:
shm_open+mmap, file-like and modern - System V:
shmget+shmat, older key-based
POSIX is generally preferred for new code.
Step 1: Create the Object
shm_open creates or opens a named shared-memory object, returning a file descriptor. The name starts with a slash, like /myshm.
#include <sys/mman.h>
#include <fcntl.h>
int create_shm(const char *name) {
int fd = shm_open(name, O_CREAT | O_RDWR, 0666);
return fd; /* -1 on error */
}Step 2: Size It
A fresh shared object has zero length. Use ftruncate to set its size before mapping.
#include <unistd.h>
#include <stddef.h>
int size_shm(int fd, size_t bytes) {
return ftruncate(fd, (off_t)bytes); /* set the region size */
}Step 3: Map It
mmap maps the object into the process's address space, returning a pointer you use like ordinary memory.
#include <sys/mman.h>
#include <stddef.h>
void *map_shm(int fd, size_t bytes) {
void *p = mmap(NULL, bytes, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
return p; /* MAP_FAILED on error */
}Reading and Writing
Once mapped, just dereference the pointer. A struct in shared memory is visible to every process that maps the same object.
#include <string.h>
typedef struct { int counter; char text[64]; } Shared;
void producer(Shared *s) {
s->counter = 42;
strcpy(s->text, "hello from producer");
}MAP_SHARED Is Essential
You must pass MAP_SHARED so writes are visible to other processes. MAP_PRIVATE gives a copy-on-write private mapping, which other processes never see.
Synchronization Is Your Job
Shared memory provides no built-in synchronization. Without coordination, two processes can corrupt data in a race. Protect it with semaphores, mutexes, or atomics.
Using a Named Semaphore
A POSIX named semaphore (sem_open) coordinates access. The producer posts when data is ready; the consumer waits before reading.
#include <semaphore.h>
#include <fcntl.h>
sem_t *open_sem(const char *name) {
return sem_open(name, O_CREAT, 0666, 0); /* initial value 0 */
}
/* producer: sem_post(s); consumer: sem_wait(s); */Cleanup
Always release resources:
munmapto unmap the regionclosethe descriptorshm_unlinkto remove the named object
Otherwise the object persists in /dev/shm after the process exits.
#include <sys/mman.h>
#include <unistd.h>
#include <stddef.h>
void cleanup(void *p, size_t bytes, int fd, const char *name) {
munmap(p, bytes);
close(fd);
shm_unlink(name);
}Quick Check
Test your understanding of shared memory.
Recap
You learned shared memory, the fastest IPC.
shm_open,ftruncate, thenmmapwithMAP_SHARED- Dereference the pointer to read and write shared data
- You must add your own synchronization (semaphores)
- Clean up with munmap, close, and shm_unlink
Frequently asked questions
Is the “Shared Memory” lesson free?
Yes — the full text of “Shared Memory” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.
What will I learn in “Shared Memory”?
Fast IPC. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Shared Memory” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Pipes
- Shared Memory
- Message Queues
- Signals