Memory Mapping and Explicit File Reads

A memory mapping lets program accesses refer to file-backed pages. Ordinary reads copy bytes through the file interface under its rules.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a large file index
  2. Check update semantics
  3. Compare when bytes become available to the computation
  4. Decide how concurrent file changes affect the reader

Trace a large file index

A lookup may need one region of a file. Reading can seek to that offset, while mapping can expose the region through address accesses. See file positions.

Check update semantics

A mapping can have private or shared modification behavior and platform-specific limits. Confirm what other readers and later opens observe before mixing models. Memory-mapped files cover those guarantees.[1]

Compare when bytes become available to the computation

An explicit read copies bytes into an application-provided buffer under a typical file API. Mapping the file creates a virtual address region, and touching parts of that region can obtain pages on demand. The result still depends on access mode and filesystem behavior. See page faults.

Explicit approach: request offset and length, receive bytes
Mapping approach: establish region, access offsets
First touch may have a different cost from repeated access

Decide how concurrent file changes affect the reader

A mapped reader and a read loop can both encounter changed source data unless the workflow uses a stable snapshot. Handle possible truncation under the actual mapping API. Compare sequential and random access patterns before choosing a method.

References

  1. Remzi H. Arpaci-Dusseau and Andrea C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces ↩