COMS W4118 Operating Systems I

Virtual Memory

Page Swapping & Faulting

Motivation

Process doesn’t need all of its pages in memory all at once in order to run. Only keep referenced pages in main memory. Keep unreferenced pages on slower, cheaper backing store (e.g. disk).

Swapping

When there isn’t enough space in main memory, swap out the page contents to disk so that other page can take its spot in memory. OS updates page table to say that the virtual address to physical address mapping is no longer active (turn off present bit). OS remembers disk address in case process needs to access page again.

swap

Pages are written to a swap partition on disk. When we want to make room for more pages in main memory, write out a “victim” page to the swap partition. When that page is needed again, read is back from the swap partition into main memory (possibly having to evict another victim page).

Note that swapping isn’t too important anymore, modern machines have a lot of RAM.

Page fault

Recall that the hardware performs virtual address translation: lookup PFN in TLB or perform page table walk. What if the page is not present? Need the OS to intervene: raise exception to trap to OS (page fault).

page fault
(adapted from OSCE figure 8.6)

Memory access steps:

  1. load instruction carried out by MMU
    • check TLB (not shown)
    • TLB miss, consult page tables (only 1-level shown for simplicity)
  2. present bit off, trap to OS via page fault
  3. OS determines that page fault is because page was swapped to disk
    • issue I/O request
    • recall that I/O is slow: put process to sleep in the meantime
    • this is why kmalloc()/copy_to/from_user() could block: might need to fetch referenced pages from disk or swap some pages out to disk to make space
  4. I/O request satisfied, page written into main memory
  5. update PTE to point to new PFN, turn on present bit
  6. restart load instruction
    • another TLB miss, but this time, page table walk succeeds, install translation into TLB
    • alternatively, install translation into TLB after updating page table so that restarted instruction is a TLB hit

OS page fault handler handles exception raised by hardware:

Page Replacement Policies

When no frames in main memory are free to be mapped to a page, need to select a victim page to swap out to disk to make room in main memory. How should we select the victim page?

Page replacement algorithm should minimize # of page faults.

Optimal Page Replacement (OPT/MIN)

Swap out the page that won’t be used for longest time in the future!

opt

Only 6 page faults.

Problem: difficult to accurately predict the future..

FIFO Page Replacement

Treat frames as a FIFO queue, swap out the page that was loaded in first

fifo

10 page faults!

Observation, what if physical memory was smaller (i.e. can only hold 3 frames instead of 4 frames?)

fifo3

Only 9 page faults!

Least-Recently-Used (LRU) Page Replacement

Swap page that hasn’t been used in the longest time. Can use arbitrary tiebreaker (e.g. FIFO).

lru

8 page faults

Attempt 1: LRU Hardware Implementation

Attempt 2: LRU Software Implementation

Attempt 3: LRU approximation: Clock/Second-chance algorithm

Let’s approximate LRU: find a not recently accessed page, but not necessarily the least recently accessed

Remove a page that has not been refereced recently.

clock osce
(adapted from OSCE figure 8.17)

next victim: a cursor over the circular list of pages (i.e. the “clock” hand)

If reference bit is 1, clear it and advance hand. Else, return current page as victim.

clock ref

10 page faults

Dirty pages

Dirty page: content has been modified since reading from disk and should be written back to disk to keep file in sync (e.g. mmap() a portion of a file with MAP_SHARED)

Clock algorithm doesn’t take into account dirty pages when selecting victim page

Clock algorithm extension: use dirty bit to prevent dirty pages from being evicted

Page Replacement Summary

Optimal: throw out page that won’t be used for longest time in future

Random: throw out a random page

FIFO: throw out page that was loaded in first

LRU: throw out page that hasn’t been used in longest time

Alternative Paging Strategies

Thrashing

High memory pressure: what if need more pages than can fit into RAM?

Thrashing: system busy just swapping pages

OS needs to make sure dirty pages are periodically written back to disk. Can also take more drastic measures… more on thrashing next lecture.


Last updated: 2023-03-26