0
0
Operating Systemsknowledge~10 mins

Memory-mapped files in Operating Systems - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Memory-mapped files
Process requests file data
OS maps file into memory
Process accesses memory address
If page in RAM: data returned immediately
No
Page fault triggers disk read
Data loaded into RAM page
Process continues with data
Changes in memory can be synced back to file
The process asks for file data, OS maps the file into memory, process accesses memory addresses. If data is not in RAM, a page fault loads it from disk. Changes can be saved back to the file.
Execution Sample
Operating Systems
1. Open file
2. Map file to memory
3. Read data via memory
4. Modify data in memory
5. Sync changes to file
This sequence shows how a file is opened, mapped into memory, accessed and modified through memory, then changes are saved back.
Analysis Table
StepActionMemory StateDisk AccessResult
1Open fileNo mapping yetNoFile descriptor ready
2Map file to memoryMapping created, no pages loadedNoMemory addresses linked to file
3Read data at addressPage not in RAMYesPage fault triggers disk read
4Load page into RAMPage loaded in RAMYesData available in memory
5Process reads dataPage in RAMNoData returned immediately
6Modify data in memoryPage modified in RAMNoChanges in memory only
7Sync changes to filePage in RAMYesChanges written back to disk
8Close fileMapping removedNoResources freed
💡 File closed and memory mapping removed, process ends
State Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 7Final
File DescriptorNoneOpenOpenOpenOpenClosed
Memory MappingNoneCreatedPage LoadedPage ModifiedPage ModifiedRemoved
RAM PageEmptyEmptyLoaded with dataModified dataModified dataEmpty
Key Insights - 3 Insights
Why does accessing a memory address sometimes cause a disk read?
Because the page is not yet loaded into RAM, causing a page fault that triggers the OS to read the data from disk (see execution_table step 3).
Are changes made in memory immediately saved to the file?
No, changes are first made in RAM and only saved back to the file when syncing occurs (see execution_table step 7).
What happens when the file is closed?
The memory mapping is removed and resources like file descriptors are freed (see execution_table step 8).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the page fault occur?
AStep 7
BStep 5
CStep 3
DStep 2
💡 Hint
Check the 'Disk Access' column where it says 'Page fault triggers disk read' at step 3.
According to the variable tracker, what is the state of the RAM page after step 6?
ALoaded with data
BModified data
CEmpty
DRemoved
💡 Hint
Look at the 'RAM Page' row under 'After Step 6' in the variable tracker.
If the process never modifies data, which step in the execution table would be skipped?
AStep 7
BStep 4
CStep 6
DStep 8
💡 Hint
Refer to the 'Action' column where 'Sync changes to file' happens only if data was modified.
Concept Snapshot
Memory-mapped files let a process access file data via memory addresses.
OS maps file pages into RAM on demand (page faults trigger disk reads).
Process reads/writes memory directly; changes can be synced back to disk.
This improves performance by avoiding explicit read/write calls.
Mapping is removed when file is closed.
Full Transcript
Memory-mapped files allow a program to treat file contents as if they are part of its memory. When a file is opened and mapped, the operating system links the file's data to memory addresses. When the program accesses these addresses, if the data is not already in RAM, a page fault occurs, and the OS loads the needed data from disk into RAM. The program can then read or modify the data directly in memory. Changes made in memory are not immediately saved to the file; they are written back when syncing occurs. Finally, when the file is closed, the memory mapping is removed and resources are freed.