A memory mapping is not a file snapshot
mmap makes file contents addressable as memory, but it does not promise a stable snapshot, coherent concurrent update, or durable write. Those guarantees must come from the surrounding file protocol.
A pointer into a file is not a read transaction
A memory mapping makes file bytes addressable through a process virtual address. That convenience can remove explicit read buffers and let the operating system bring pages in as they are touched. It does not, by itself, promise that the mapped region is a stable snapshot, a complete copy, or a transactionally consistent view of a file.
The distinction matters whenever another actor can replace, truncate, or modify the file while the mapping is in use. A parser may see a header from one logical version and a payload from another. Worse, if the file becomes shorter, touching a page that lies beyond its new end can raise SIGBUS on Linux. Code that treats mapped memory like an immutable byte array has quietly assumed a coordination protocol that mmap did not provide. [1]
Mapping lifetime and file-descriptor lifetime differ
On Linux, a successful file mapping remains valid after the process closes the file descriptor used to create it. The mapping refers to the mapped object; it does not need the descriptor to remain open. Conversely, closing that descriptor is not a way to release the virtual memory: munmap does that, and process exit also removes it. [1]
This is useful for resource ownership. A loader can open a file, validate the mapping request, map it, then close the descriptor if it no longer needs descriptor operations. But the mapped region still has its own lifetime. Any pointer into it must not escape past munmap, and any consumer must finish before the owning mapping is released. Treat the mapping as a resource with an explicit owner, just as you would a heap allocation or locked file handle.
Shared and private mappings answer a write question
MAP_SHARED says that updates to the mapped region are visible to other processes mapping the same region and, for a file-backed mapping, are carried through to the underlying file. MAP_PRIVATE is copy-on-write: writes through the mapping do not update the file or other mappings. A subtle Linux-specific warning is that visibility of file changes made after mmap on a MAP_PRIVATE mapping is unspecified. It therefore cannot be used as a portable snapshot mechanism or as a reliable live-update feed. [1]
Imagine a service maps a configuration file privately, then an administrator edits the file in place. The service must not assume every subsequent read sees either the entire old configuration or the entire new one. Nor should it rely on whether modified bytes become visible in this private mapping. If a reader requires a consistent version, define that property explicitly: read a bounded copy under a lock, publish immutable versioned files and atomically switch a name, or use a format with generation/checksum validation and retry. The appropriate choice depends on who controls writers and what failure modes are acceptable.
A mapping does not make file replacement atomic for readers
Replacing a pathname and changing the contents of an already-open file are different operations. A mapping is tied to the file object established through the descriptor, so a writer that writes a new temporary file and renames it over the pathname can let later opens find a new object while existing mappings continue to refer to the old one. That can be a useful deployment pattern, but the guarantee comes from the filesystem rename protocol and writer discipline, not from mmap alone.
By contrast, truncating the mapped object changes its size while readers may still hold addresses into it. The Linux manual documents SIGBUS for an attempted access to a page beyond the end of the mapped file. Bounds checks against the old size cannot protect against a concurrent truncation unless the design also prevents that change or coordinates access. [1]
Visibility is not durability
For a writable MAP_SHARED mapping, seeing a store in memory and knowing it has reached durable storage are separate requirements. Linux msync synchronizes a selected mapped range with the filesystem. MS_ASYNC schedules the update and returns; MS_SYNC requests the update and waits for it to complete. The manual notes that without msync there is no guarantee that updates have been written back before munmap. [2]
This still does not make a multi-field update crash-atomic. If a record contains a length, payload, and checksum, a crash can leave an intermediate combination unless the file format and update protocol account for it. A robust design might write a new record elsewhere, validate it, and then publish a small commit marker or switch to a new file according to documented filesystem guarantees. Persistence ordering and recovery rules belong to the storage format; an addressable mapping does not supply them.
Choose the consistency contract before choosing mmap
Before mapping a file, write down four properties: whether writers can act concurrently; whether readers need one coherent version; whether changes should be shared; and what “saved” means after a process or machine failure. Then choose synchronization and storage layout to provide those properties. For a read-only asset replaced as a whole, immutable versioned files may be simple. For a shared index updated by multiple processes, a lock or carefully designed atomic protocol may be necessary. For durable writable data, msync is one part of the contract, not the entire crash-recovery design.
Mmap is often an effective access mechanism, especially for random reads and operating-system-managed paging. The design mistake is asking that mechanism to prove consistency, ownership, or durability properties it does not promise. Map the bytes only after the surrounding protocol says which bytes belong together, who may change them, and when the mapping may be retired.