Deep dive
mmap trades explicit I/O for VM mechanisms:
- The OS loads pages on-demand via page faults.
- Can reduce syscalls and copy overhead.
- Useful for shared memory between processes.
- Requires careful handling of page faults and alignment.
Examples
Random access to a large file:
mmap file -> pointer arithmetic -> OS pages in data as needed
Common pitfalls
- Assuming mmap is always faster than read/write.
- Not handling SIGBUS on truncated files.
- Poor access patterns causing thrashing.
Interview follow-ups
- When would you prefer read/write over mmap?
- How does mmap interact with page cache?
- What are the risks for concurrent writers?