Part 6 of 7 · 3 min read
Beyond files
Anonymous mappings and what else uses mmap
In plain words: If the flags include MAP_ANONYMOUS, no file is involved. The call returns zeroed memory that belongs to the process alone (with MAP_PRIVATE) or can be shared with child processes (with MAP_SHARED). Page 1 showed malloc using this form for large blocks, and Level 6 page 7 used it to test page faults. So mmap is the one basic call behind the heap's large blocks, thread stacks (page 3), shared libraries, the code of every program and shared memory.
| Use | Kind of mapping |
|---|---|
| Program code, shared libraries | file, private (code lines are read-only) |
Large malloc blocks, thread stacks |
anonymous, private |
| Data files in a database | file, shared |
| Memory shared by a parent and its children | anonymous, shared |
The limits to remember:
- Offsets and lengths are in pages, so a file is mapped in whole pages.
- The mapping cannot change size when the file grows. The program must map again (or use
mremap). - A file larger than the free address space cannot be mapped whole, which matters only for 32-bit programs.
- I/O errors and truncation appear as signals, not as error codes.
Anonymous mapping
An anonymous mapping is a mapping with no file behind it, made with MAP_ANONYMOUS, whose pages start as zeros and which has no backing store except swap space.
Example: mmap(NULL, 1 << 20, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) returns 1 MiB of zeroed memory.
Interview angle
"What is the difference between read and mmap for file I/O?" A strong answer says that read copies data into a user buffer through a system call, while mmap maps the page cache pages into the address space and loads them by page faults. It adds when each wins, and the SIGBUS risk.
Check yourself:
1. What does MAP_ANONYMOUS change about the mapping?
2. Name two things other than files that use mmap inside the system.
3. What happens to a mapping when the file grows?
Words you will meet
| Word | Meaning |
|---|---|
mmap |
The call that maps a file or memory into the address space |
munmap |
The call that removes a mapping |
| Shared mapping | Writes go to the file and are seen by all |
| Private mapping | Writes make a private copy |
| Page cache | Memory that holds recent file pages |
msync |
Force changes of a shared mapping to disk |
| SIGBUS | The signal for an access to a page with no file behind it |
| Prefaulting | Making page table entries in advance |
MAP_POPULATE |
The flag that prefaults a mapping |
| Anonymous mapping | A mapping with no file, starting as zeros |
| Dirty page | A page changed in memory and not yet written to disk |
Quick notes
- A memory-mapped file is a file whose contents are made part of a process's address space by
mmap, so that the process reads and writes the file through memory accesses. - A shared mapping (
MAP_SHARED) makes writes go to the underlying file or memory object and become visible to every process that maps it, while a private mapping (MAP_PRIVATE) gives a. - The page cache is the part of memory in which the kernel keeps recently used pages of files, so that reads and mapped accesses are served from memory when the.
- Prefaulting is the creation of the page table entries of a mapping in advance, as
MAP_POPULATEdoes inside themmapcall, so that later accesses meet no page faults. - An anonymous mapping is a mapping with no file behind it, made with
MAP_ANONYMOUS, whose pages start as zeros and which has no backing store except swap space.