Operating System Notes
Operating System Notes
• During MMU address translation, if valid–invalid bit in page table entry is i page
fault
Page Table When Some Pages Are Not in Main Memory
Page Fault
• If there is a reference to a page, first reference to
that page will trap to operating system:
page fault
1. Operating system looks at another table to decide:
– Invalid reference abort
– Just not in memory
2. Find free frame
3. Swap page into frame via scheduled disk operation
4. Reset tables to indicate page now in memory
Set validation bit = v
5. Restart the instruction that caused the page fault
Steps in Handling a Page Fault
Aspects of Demand Paging
• Extreme case – start process with no pages in memory
– OS sets instruction pointer to first instruction of process,
non-memory-resident -> page fault
– And for every other process pages on first access
– Pure demand paging
• Actually, a given instruction could access multiple pages
-> multiple page faults
– Consider fetch and decode of instruction which adds 2
numbers from memory and stores result back to memory
– Pain decreased because of locality of reference
• Hardware support needed for demand paging
– Page table with valid / invalid bit
– Secondary memory (swap device with swap space)
– Instruction restart
Instruction Restart
• Consider an instruction that could access
several different locations
– block move
3. Bring the desired page into the (newly) free frame; update the
page and frame tables
15 page faults
• The first three references cause faults that fill the three empty
frames.
9 page faults
Least Recently Used (LRU) Algorithm
12 page faults
LRU Algorithm (Cont.)
• Counter implementation
– Every page entry has a counter; every time page is referenced
through this entry, copy the clock into the counter
– When a page needs to be changed, look at the counters to find
smallest value
• Search through table needed
• Stack implementation
– Keep a stack of page numbers in a double link form:
– Page referenced:
• move it to the top
• requires 6 pointers to be changed
– But each update more expensive
– No search for replacement
• LRU and OPT are cases of stack algorithms that don’t have
Belady’s Anomaly
Use Of A Stack to Record Most Recent Page References
LRU Approximation Algorithms
• LRU needs special hardware and still slow
• Reference bit
– With each page associate a bit, initially = 0
– When page is referenced bit set to 1
– Replace any with reference bit = 0 (if one exists)
• We do not know the order, however
• Second-chance algorithm
– Generally FIFO, plus hardware-provided reference bit
– Clock replacement
– If page to be replaced has
• Reference bit = 0 -> replace it
• reference bit = 1 then:
– set reference bit 0, leave page in memory
– replace next page, subject to same rules
Second-Chance (clock) Page-Replacement Algorithm
Enhanced Second-Chance Algorithm