Memory-mapped file
A memory-mapped file exposes a file region through a process's virtual address space so its contents can be accessed with memory loads and stores.
Mapping mode: MAP_SHARED permits file-backed updates, whereas MAP_PRIVATE keeps modifications private through copy-on-write. Private mapping does not promise a snapshot of later external file changes. Closing the original descriptor leaves the mapping valid. [1]
File extent: mapping length is not file length. A larger mapping does not grow the file, and accesses to whole pages beyond the object can raise SIGBUS. The unused tail of a partial final page is different: stores there do not extend the file, and Linux warns that cached tail bytes may remain visible to later mappings. [2] [1]
Persistence: synchronization of a shared mapping is distinct from an atomic transaction. Background writeback may occur before an explicit msync call. [3]
Relations
- related
- msync, file-truncation, page-cache, copy-on-write, lmdb
Advantages
- Allows a file region to be addressed without an explicit read call for each access.
Limitations
- Concurrent access and file resizing require an application-level coordination protocol.
Citations
- Linux man-pages: mmap(2), mapping modes and lifetime
- POSIX.1-2017: mmap(), object size and mapping boundaries
- POSIX.1-2017: msync(), completion and background writeback
Further reading
Sources & caveats
- The POSIX citations refer to the 2017 edition; Linux-specific details are identified separately.
- The companion experiment checks API behavior on one host, not power-loss survival.