OS & Hardware

In a mechanism that maps a file into memory (such as mmap), what happens when the file's contents are read?

  1. AThe first access page-faults, and that part of the file is read in
  2. BThe whole file is loaded into physical memory at mapping time, and the disk is not used afterward
  3. 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.
Practice this topic Back to topic

More OS & Hardware questions

  1. When writing small data one byte at a time, why is accumulating it in a user-space buffer and writing in bulk faster than calling a system call each time?
  2. What is the main purpose of giving a CPU a privileged (kernel) mode and a user mode?
  3. What is the mechanism by which applications ask the OS to perform tasks such as file I/O?
  4. What happens when the ecall instruction at the end of the RISC-V code above executes?
  5. Which is the usual distinction between interrupts and exceptions?