Sub topic 2
Communicating Through Shared Memory
This chapter assumes you understand processes, fork, virtual memory, and basic C-like code. Shared-memory communication lets two processes access the same region of physical memory. One process writes data there, and another process reads it. The operating system maps that region into both processes' address spaces.
The shared-memory model
Normally, each process has a private address space. A pointer in one process cannot directly access an ordinary variable in another process. Shared memory creates an exception: selected virtual addresses in different processes refer to the same memory region.
The data exchange itself is fast because processes do not copy every message through the kernel. However, shared memory does not automatically make access safe. If processes read and write at the same time, their operations can interfere.
Common ways to create shared memory include:
| Mechanism | Typical use | Important property |
|---|---|---|
| Anonymous shared `mmap` | Related processes, such as a parent and child | The mapping can survive `fork` |
| POSIX shared memory | Unrelated processes | Named object, often created with `shm_open` |
| System V shared memory | Older Unix and Linux programs | Kernel-managed segment identified by an ID |
A mapping must be created with shared visibility. For example, MAP_SHARED tells mmap that updates can be observed by other processes using the same mapping. MAP_PRIVATE creates copy-on-write behavior instead, so it is unsuitable for communication.
A parent and child sharing an integer
The following program creates one shared integer, forks a child, and uses the shared region to pass a value from the child to the parent.
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void) {
int *shared = mmap(NULL, sizeof(*shared),
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (shared == MAP_FAILED) {
perror("mmap");
return EXIT_FAILURE;
}
*shared = 0;
pid_t pid = fork();
if (pid == -1) {
perror("fork");
munmap(shared, sizeof(*shared));
return EXIT_FAILURE;
}
if (pid == 0) {
*shared = 42;
munmap(shared, sizeof(*shared));
return EXIT_SUCCESS;
}
if (waitpid(pid, NULL, 0) == -1) {
perror("waitpid");
munmap(shared, sizeof(*shared));
return EXIT_FAILURE;
}
printf("parent read: %d\n", *shared);
munmap(shared, sizeof(*shared));
return EXIT_SUCCESS;
}Compile and run it with:
gcc -Wall -Wextra -std=c11 shared.c -o shared
./sharedThe parent waits for the child before reading. This provides the required ordering in this example. Without waitpid, the parent could read the old value before the child writes 42.
Shared data still needs synchronisation
A shared mapping only solves visibility. It does not solve concurrent updates. Consider two processes executing counter++. That statement usually contains three steps:
- Read
counter. - Add one.
- Write the result.
If both processes read 5 before either writes, both may write 6. The correct result, 7, is lost. This is a race condition: the result depends on the timing of an uncontrolled interleaving.
Use a mutex, semaphore, or another synchronisation method around compound operations. The lock itself must also be usable by both processes. For example, a POSIX process-shared mutex must be stored in shared memory and configured with PTHREAD_PROCESS_SHARED.
Exercise
Why does replacing MAPSHARED with MAPPRIVATE prevent this program from reliably communicating the child's value to the parent?
Show answer
MAP_PRIVATE uses copy-on-write. After a process writes, it receives a private copy of the page, so the write is not necessarily visible in the other process.