UNIHIVE

Study Hub UNIPORT CSC 394

CSC 394 · Operating Systems

Virtual memory and page replacement

Demand paging, page faults, page-replacement algorithms (FIFO, Optimal, LRU) with worked counts, Belady's anomaly and thrashing.

Our notes

2 min read

Virtual memory

Virtual memory lets a process run when only part of it is in RAM; the rest stays on disk. Benefits: programs can be larger than physical memory, more programs can run at once, less I/O to load a program.

Demand paging

A page is loaded only when it is first used. Each page-table entry has a valid/invalid bit. Using a page that is not in memory causes a page fault:

  1. Trap to the OS.
  2. Find a free frame (or choose a victim page to replace).
  3. Read the page from disk into the frame.
  4. Update the page table, set the valid bit.
  5. Restart the instruction.

A modified (dirty) victim must be written back to disk first.

Effective access time

EAT = (1 − p) × memory time + p × page-fault time. With memory 200 ns, fault service 8 ms (8,000,000 ns) and p = 0.001: EAT = 0.999 × 200 + 0.001 × 8,000,000 = 199.8 + 8,000 ≈ 8,200 ns, about 40 times slower than without faults. Page faults must be very rare.

Page-replacement algorithms (worked)

Reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, with 3 frames, all empty at the start.

FIFO (replace the page that has been in memory longest):
7 F [7] · 0 F [7,0] · 1 F [7,0,1] · 2 F replace 7 [2,0,1] · 0 hit · 3 F replace 0 [2,3,1] · 0 F replace 1 [2,3,0] · 4 F replace 2 [4,3,0] · 2 F replace 3 [4,2,0] · 3 F replace 0 [4,2,3] · 0 F replace 4 [0,2,3] · 3 hit · 2 hit.
10 page faults.

Optimal (replace the page not needed for the longest time in the future):
7 F · 0 F · 1 F · 2 F: future 0, 3, 0, 4, 2… 7 is never used again → replace 7 [2,0,1] · 0 hit · 3 F: among 2, 0, 1, page 1 is never used again → replace 1 [2,0,3] · 0 hit · 4 F: next uses: 2 at position 9, 3 at 10, 0 at 11 → replace 0 (used furthest away) [2,4,3] · 2 hit · 3 hit · 0 F: remaining 3, 2 → replace 4 [2,0,3] · 3 hit · 2 hit.
7 page faults (the lowest possible; needs knowledge of the future, so it is a yardstick).

LRU (replace the least recently used):
7 F · 0 F · 1 F · 2 F replace 7 [2,0,1] · 0 hit · 3 F: least recent of 2, 0, 1 is 1 → [2,0,3] · 0 hit · 4 F: least recent of 2, 0, 3 is 2 → [4,0,3] · 2 F: least recent is 3 → [4,0,2] · 3 F: least recent is 0 → [4,3,2] · 0 F: least recent is 4 → [0,3,2] · 3 hit · 2 hit.
9 page faults.

Belady's anomaly

With FIFO, more frames can give more page faults for some reference strings (1,2,3,4,1,2,5,1,2,3,4,5 gives 9 faults with 3 frames but 10 with 4). LRU and Optimal never show this (they are "stack algorithms").

Thrashing

When processes do not have enough frames for the pages they actively use, they page-fault constantly and spend more time swapping than working; CPU use collapses. Causes: too many processes. Remedies: the working-set model (give each process enough frames for the pages it used recently), page-fault-frequency control, and running fewer processes.

Exams usually ask: define virtual memory and demand paging; steps in handling a page fault; count page faults for FIFO, Optimal and LRU; Belady's anomaly; thrashing and its cure; EAT with page faults.

Common mistake: in LRU, choosing the page that arrived earliest (that is FIFO). LRU looks at when each page was last used.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All CSC 394 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.