2078 2

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]

Operating SystemsChapter 4: Memory ManagementPage Replacement AlgorithmsNumerical: Page replacement algorithmsVideo solution

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.

StepRefFramesFault/Hit
11[1]Fault
23[1,3]Fault
34[1,3,4]Fault
42next: 1→9, 3→5, 4→7 → replace 1 → [2,3,4]Fault
53[2,3,4]Hit
65next: 2→10, 3→8, 4→7 → replace 2 → [5,3,4]Fault
74[5,3,4]Hit
83[5,3,4]Hit
91next: 5→never, 3→13, 4→11 → replace 5 → [1,3,4]Fault
102next: 1→15, 3→13, 4→11 → replace 1 → [2,3,4]Fault
114[2,3,4]Hit
126next: 2→14, 3→13, 4→16 → replace 4 → [2,3,6]Fault
133[2,3,6]Hit
142[2,3,6]Hit
151next: 2→17, 3→never, 6→never → replace 3 → [2,1,6]Fault
164next: 2→17, 1→never, 6→never → replace 6 → [2,1,4]Fault
172[2,1,4]Hit

OPR Page Faults = 10, Hits = 7


2. LRU (Least Recently Used)

Replace the page unused for the longest time.

StepRefFrames (after)Fault/HitRecency (recent→old)
11[1]Fault1
23[1,3]Fault3,1
34[1,3,4]Fault4,3,1
42evict 1 → [2,3,4]Fault2,4,3
53[2,3,4]Hit3,2,4
65evict 4 → [2,3,5]Fault5,3,2
74evict 2 → [4,3,5]Fault4,5,3
83[4,3,5]Hit3,4,5
91evict 5 → [4,3,1]Fault1,3,4
102evict 4 → [2,3,1]Fault2,1,3
114evict 3 → [2,4,1]Fault4,2,1
126evict 1 → [2,4,6]Fault6,4,2
133evict 2 → [3,4,6]Fault3,6,4
142evict 4 → [3,2,6]Fault2,3,6
151evict 6 → [3,2,1]Fault1,2,3
164evict 3 → [4,2,1]Fault4,1,2
172[4,2,1]Hit2,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.

StepRefActionFrames (bit)Ptr afterF/H
11load pos01(1) - -1Fault
23load pos11(1) 3(1) -2Fault
34load pos21(1) 3(1) 4(1)0Fault
42ptr0: 1 bit1→0, ptr1:3→0, ptr2:4→0, ptr0:1 bit0 replace2(1) 3(0) 4(0)1Fault
53hit, set 3→12(1) 3(1) 4(0)1Hit
65ptr1:3 bit1→0, ptr2:4 bit0 replace2(1) 3(0) 5(1)0Fault
74ptr0:2→0, ptr1:3 bit0 replace2(0) 4(1) 5(1)2Fault
83ptr2:5→0, ptr0:2 bit0 replace3(1) 4(1) 5(0)1Fault
91ptr1:4→0, ptr2:5 bit0 replace3(1) 4(0) 1(1)0Fault
102ptr0:3→0, ptr1:4 bit0 replace3(0) 2(1) 1(1)2Fault
114ptr2:1→0, ptr0:3 bit0 replace4(1) 2(1) 1(0)1Fault
126ptr1:2→0, ptr2:1 bit0 replace4(1) 2(0) 6(1)0Fault
133ptr0:4→0, ptr1:2 bit0 replace4(0) 3(1) 6(1)2Fault
142ptr2:6→0, ptr0:4 bit0 replace2(1) 3(1) 6(0)1Fault
151ptr1:3→0, ptr2:6 bit0 replace2(1) 3(0) 1(1)0Fault
164ptr0:2→0, ptr1:3 bit0 replace2(0) 4(1) 1(1)2Fault
172hit, set 2→12(1) 4(1) 1(1)2Hit

Faults: all steps except 5 and 17 = 15 faults, Hits = 2

Clock Page Faults = 15


Summary

AlgorithmPage Faults
OPR10
LRU14
Clock15

(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.