OS & Hardware
In a mechanism that maps a file into memory (such as mmap), what happens when the file's contents are read?
- AThe first access page-faults, and that part of the file is read in
- BThe whole file is loaded into physical memory at mapping time, and the disk is not used afterward
- CEach access invokes a system call that reads the file one byte at a time
Show answer and explanation
Answer: A. The first access page-faults, and that part of the file is read in
Mapping a file into the address space only registers the range in the page tables; nothing is read right away. When the program first touches it, a page fault occurs, the OS reads the relevant part of the file into a physical page, and from then on it can be accessed like ordinary memory. It is the same mechanism as demand paging, avoiding a system call per read and reducing extra data copies. The whole file is not loaded up front.
- A. Correct. The first access faults, and only the needed part is read in.
- B. The whole file is not loaded at the start.
- C. Once loaded, it is accessed as ordinary memory with no per-access system call.