This part of the Chapter 49 series covers how mmap() works with actual files — both the private mappings the kernel uses to load programs, and the shared mappings that enable memory-mapped I/O and fast IPC. These are among the most important and widely used kernel features in embedded Linux and systems programming.
Tutorial Files
Covers why the kernel uses MAP_PRIVATE to map the text and data segments of every ELF binary, how Copy-on-Write works, and how to use private file mappings for simplified file input. Includes a working program that proves COW does not modify the original file.
Covers how shared file mappings work, memory-mapped I/O as an alternative to read()/write(), msync() for durability, and using shared file mappings for fast persistent IPC. Includes the classic t_mmap.c program, a writer/reader IPC pair, and a struct-based mapping example.
Quick Reference: MAP_PRIVATE vs MAP_SHARED
| Property | MAP_PRIVATE | MAP_SHARED |
|---|---|---|
| Writes reach file? | ✗ No — COW copies | ✓ Yes — written back |
| Visible to other processes? | ✗ No | ✓ Yes |
| Use case | Loading ELF text/data, file input | Memory-mapped I/O, IPC |
| fd open mode | O_RDONLY or O_RDWR | Must be O_RDWR for PROT_WRITE |
| Physical pages shared? | Yes (until first write) | Yes (always) |
| msync() needed? | Not applicable | For durability / cross-process visibility |
EmbeddedPathashala — Free embedded systems & Linux tutorials for engineers and students.
