CSC264 · 2078 · 10 marks
Why OPR is best but not practically feasible page replacement algorithm? Calculate the number of page faults for OPR, LRU and Clock page replacement algorithm for the reference string: 1, 3, 4, 2, 3, 5, 4, 3, 1, 2, 4, 6, 3, 2, 1, 4, 2. Assume the memory size is 3.
Why OPR is best but not practically feasible page replacement algorithm? Calculate the number of page faults for OPR, LRU and Clock page replacement algorithm for the reference string: 1, 3, 4, 2, 3, 5, 4, 3, 1, 2, 4, 6, 3, 2, 1, 4, 2. Assume the memory size is 3.[10]
OPR Page Replacement and Page Fault Calculations
Given Data
- Reference String: 1, 3, 4, 2, 3, 5, 4, 3, 1, 2, 4, 6, 3, 2, 1, 4, 2
- Number of Frames: 3
Why OPR is Best but Not Practically Feasible
Optimal Page Replacement (OPR) replaces the page that will not be used for the longest period of time in the future.
Why BEST:
- Yields the minimum possible page faults for a given string and frame count.
- Used as a benchmark to evaluate other algorithms.
- Free from Belady's anomaly.
Why NOT feasible:
- Requires future knowledge of the reference string.
- The OS cannot predict which pages a process will reference next.
- Hence impossible to implement; only a theoretical reference point.
1. Optimal (OPR)
Rule: replace the page whose next use is farthest in the future.
| Step | Ref | Frames | Fault/Hit |
|---|---|---|---|
| 1 | 1 | [1] | Fault |
| 2 | 3 | [1,3] | Fault |
| 3 | 4 | [1,3,4] | Fault |
| 4 | 2 | next: 1→9, 3→5, 4→7 → replace 1 → [2,3,4] | Fault |
| 5 | 3 | [2,3,4] | Hit |
| 6 | 5 | next: 2→10, 3→8, 4→7 → replace 2 → [5,3,4] | Fault |
| 7 | 4 | [5,3,4] | Hit |
| 8 | 3 | [5,3,4] | Hit |
| 9 | 1 | next: 5→never, 3→13, 4→11 → replace 5 → [1,3,4] | Fault |
| 10 | 2 | next: 1→15, 3→13, 4→11 → replace 1 → [2,3,4] | Fault |
| 11 | 4 | [2,3,4] | Hit |
| 12 | 6 | next: 2→14, 3→13, 4→16 → replace 4 → [2,3,6] | Fault |
| 13 | 3 | [2,3,6] | Hit |
| 14 | 2 | [2,3,6] | Hit |
| 15 | 1 | next: 2→17, 3→never, 6→never → replace 3 → [2,1,6] | Fault |
| 16 | 4 | next: 2→17, 1→never, 6→never → replace 6 → [2,1,4] | Fault |
| 17 | 2 | [2,1,4] | Hit |
OPR Page Faults = 10, Hits = 7
2. LRU (Least Recently Used)
Replace the page unused for the longest time.
| Step | Ref | Frames (after) | Fault/Hit | Recency (recent→old) |
|---|---|---|---|---|
| 1 | 1 | [1] | Fault | 1 |
| 2 | 3 | [1,3] | Fault | 3,1 |
| 3 | 4 | [1,3,4] | Fault | 4,3,1 |
| 4 | 2 | evict 1 → [2,3,4] | Fault | 2,4,3 |
| 5 | 3 | [2,3,4] | Hit | 3,2,4 |
| 6 | 5 | evict 4 → [2,3,5] | Fault | 5,3,2 |
| 7 | 4 | evict 2 → [4,3,5] | Fault | 4,5,3 |
| 8 | 3 | [4,3,5] | Hit | 3,4,5 |
| 9 | 1 | evict 5 → [4,3,1] | Fault | 1,3,4 |
| 10 | 2 | evict 4 → [2,3,1] | Fault | 2,1,3 |
| 11 | 4 | evict 3 → [2,4,1] | Fault | 4,2,1 |
| 12 | 6 | evict 1 → [2,4,6] | Fault | 6,4,2 |
| 13 | 3 | evict 2 → [3,4,6] | Fault | 3,6,4 |
| 14 | 2 | evict 4 → [3,2,6] | Fault | 2,3,6 |
| 15 | 1 | evict 6 → [3,2,1] | Fault | 1,2,3 |
| 16 | 4 | evict 3 → [4,2,1] | Fault | 4,1,2 |
| 17 | 2 | [4,2,1] | Hit | 2,4,1 |
Count faults: steps 1,2,3,4,6,7,9,10,11,12,13,14,15,16 = 14 faults, Hits = 3
LRU Page Faults = 14
3. Clock (Second-Chance)
Circular list of 3 frames with a reference bit; on fault the pointer advances, clearing set bits until it finds a bit=0 to replace. On a hit, the page's reference bit is set to 1.
Let frame slots be positions 0,1,2. Pointer starts at 0.
| Step | Ref | Action | Frames (bit) | Ptr after | F/H |
|---|---|---|---|---|---|
| 1 | 1 | load pos0 | 1(1) - - | 1 | Fault |
| 2 | 3 | load pos1 | 1(1) 3(1) - | 2 | Fault |
| 3 | 4 | load pos2 | 1(1) 3(1) 4(1) | 0 | Fault |
| 4 | 2 | ptr0: 1 bit1→0, ptr1:3→0, ptr2:4→0, ptr0:1 bit0 replace | 2(1) 3(0) 4(0) | 1 | Fault |
| 5 | 3 | hit, set 3→1 | 2(1) 3(1) 4(0) | 1 | Hit |
| 6 | 5 | ptr1:3 bit1→0, ptr2:4 bit0 replace | 2(1) 3(0) 5(1) | 0 | Fault |
| 7 | 4 | ptr0:2→0, ptr1:3 bit0 replace | 2(0) 4(1) 5(1) | 2 | Fault |
| 8 | 3 | ptr2:5→0, ptr0:2 bit0 replace | 3(1) 4(1) 5(0) | 1 | Fault |
| 9 | 1 | ptr1:4→0, ptr2:5 bit0 replace | 3(1) 4(0) 1(1) | 0 | Fault |
| 10 | 2 | ptr0:3→0, ptr1:4 bit0 replace | 3(0) 2(1) 1(1) | 2 | Fault |
| 11 | 4 | ptr2:1→0, ptr0:3 bit0 replace | 4(1) 2(1) 1(0) | 1 | Fault |
| 12 | 6 | ptr1:2→0, ptr2:1 bit0 replace | 4(1) 2(0) 6(1) | 0 | Fault |
| 13 | 3 | ptr0:4→0, ptr1:2 bit0 replace | 4(0) 3(1) 6(1) | 2 | Fault |
| 14 | 2 | ptr2:6→0, ptr0:4 bit0 replace | 2(1) 3(1) 6(0) | 1 | Fault |
| 15 | 1 | ptr1:3→0, ptr2:6 bit0 replace | 2(1) 3(0) 1(1) | 0 | Fault |
| 16 | 4 | ptr0:2→0, ptr1:3 bit0 replace | 2(0) 4(1) 1(1) | 2 | Fault |
| 17 | 2 | hit, set 2→1 | 2(1) 4(1) 1(1) | 2 | Hit |
Faults: all steps except 5 and 17 = 15 faults, Hits = 2
Clock Page Faults = 15
Summary
| Algorithm | Page Faults |
|---|---|
| OPR | 10 |
| LRU | 14 |
| Clock | 15 |
(OPR gives the fewest faults, confirming it as the optimal benchmark.)
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.