Lesson 14: mmap() — Memory-Mapped Files
mmap() maps a file or anonymous memory directly into the VAS. This allows reading a large file as if it were an in-memory array, without explicit read() calls. In this lesson we learn the API, the difference between MAP_PRIVATE and MAP_SHARED, and the connection to CUDA IPC.
mmap is like opening a book directly on your desk instead of copying chapters each time. You read directly from the source.
- mmap
- A system call that maps a file or anonymous memory directly into the process VAS. Returns a pointer to the start of the region.
- page fault
- An exception that occurs when a process accesses a memory page not in RAM. The OS loads the page (from file or swap) and resumes execution.
- MAP_SHARED
- Writes are propagated back to the file and visible to other processes mapping the same file. Used for IPC.
- MAP_PRIVATE
- Copy-on-write: writes are private to the current process and not written back to the file. ELF files are loaded with MAP_PRIVATE.
- file-backed mapping
- An mmap backed by a file as its data source. Page faults load data from the file. Contrasts with anonymous mmap backed by zeros.