All Exams Test series for 1 year @ ₹349 only
Question

Consider a virtual page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0,1, 7, 0,1 Suppose a demand paged virtual memory system running on a computer system such that the main memory has 3 page frames. Then_______ page replacement algorithm has minimum number of page faults.  

The correct answer is

Optimal

Understanding Page Replacement Algorithms

In a demand-paged virtual memory system, when the CPU requires a page that is not currently in the main memory (RAM), a situation called a page fault occurs. The operating system then needs to bring the required page from secondary storage (like a hard drive) into a page frame in main memory. If all page frames are already occupied, a page replacement algorithm is used to decide which page currently in memory should be removed to make space for the new page. Different algorithms aim to minimize the number of page faults, as page faults are costly operations involving disk access.

The question asks us to compare four common page replacement algorithms using a specific virtual page reference string and a main memory with 3 page frames to determine which one results in the minimum number of page faults.

The given page reference string is: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 The number of available page frames in main memory is 3.

Simulating Page Replacement Algorithms

Let's simulate the execution of each algorithm step-by-step and count the number of page faults. We start with an empty main memory (all 3 frames are empty).

FIFO (First-In, First-Out) Page Replacement

The FIFO algorithm replaces the page that has been in the memory for the longest time. It's like a queue; the page that came in first is the first to leave.

Step Ref. Page Frame 1 Frame 2 Frame 3 Event Queue Order (Entry Time)
177--PF (1)7
2070-PF (2)7, 0
31701PF (3)7, 0, 1
42012PF (4)0, 1, 2 (7 replaced)
50012H0, 1, 2
63123PF (5)1, 2, 3 (0 replaced)
70230PF (6)2, 3, 0 (1 replaced)
84304PF (7)3, 0, 4 (2 replaced)
92042PF (8)0, 4, 2 (3 replaced)
103423PF (9)4, 2, 3 (0 replaced)
110230PF (10)2, 3, 0 (4 replaced)
123230H2, 3, 0
132230H2, 3, 0
141301PF (11)3, 0, 1 (2 replaced)
152012PF (12)0, 1, 2 (3 replaced)
160012H0, 1, 2
171012H0, 1, 2
187127PF (13)1, 2, 7 (0 replaced)
190270PF (14)2, 7, 0 (1 replaced)
201701PF (15)7, 0, 1 (2 replaced)

Using the FIFO algorithm, there are 15 page faults.

LIFO (Last-In, First-Out) Page Replacement

The LIFO algorithm replaces the page that was most recently brought into memory.

Step Ref. Page Frame 1 Frame 2 Frame 3 Event Newest Page In Frame
177--PF (1)7
2070-PF (2)0
31701PF (3)1
42702PF (4)2 (1 replaced)
50702H2
63703PF (5)3 (2 replaced)
70703H3
84704PF (6)4 (3 replaced)
92702PF (7)2 (4 replaced)
103703PF (8)3 (2 replaced)
110703H3
123703H3
132702PF (9)2 (3 replaced)
141701PF (10)1 (2 replaced)
152702PF (11)2 (1 replaced)
160702H2
171701PF (12)1 (2 replaced)
187701H1
190701H1
201701H1

Using the LIFO algorithm, there are 12 page faults.

LRU (Least Recently Used) Page Replacement

The LRU algorithm replaces the page that has not been accessed for the longest period of time.

Step Ref. Page Frame 1 Frame 2 Frame 3 Event Access Order (LRU -> Most Recent)
177--PF (1)7
2070-PF (2)7, 0
31701PF (3)7, 0, 1
42012PF (4)0, 1, 2 (7 is LRU)
50012H1, 2, 0 (0 is most recent)
63023PF (5)2, 0, 3 (1 is LRU)
70023H2, 3, 0 (0 is most recent)
84234PF (6)3, 0, 4 (2 is LRU)
92342PF (7)4, 3, 2 (0 is LRU)
103423PF (8)4, 2, 3 (3 is most recent)
110430PF (9)4, 3, 0 (2 is LRU)
123403H4, 0, 3 (3 is most recent)
132402PF (10)4, 0, 2 (3 is LRU)
141421PF (11)4, 2, 1 (0 is LRU)
152412H4, 1, 2 (2 is most recent)
160410PF (12)4, 1, 0 (2 is LRU)
171401H4, 0, 1 (1 is most recent)
187407PF (13)4, 0, 7 (1 is LRU)
190470H4, 7, 0 (0 is most recent)
201401PF (14)4, 0, 1 (7 is LRU)

Let's re-check the LRU trace from step 4 onwards, keeping track of least recently used correctly. Frames: 3. Ref: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 [-, -, -] Start 1. 7: [7, -, -] PF (1). Access order (LRU to MRU): 7 2. 0: [7, 0, -] PF (2). Access order: 7, 0 3. 1: [7, 0, 1] PF (3). Access order: 7, 0, 1 4. 2: [0, 1, 2] PF (4). LRU=7. Replace 7. Frames={0,1,2}. Access order: 0, 1, 2 5. 0: [0, 1, 2] H. Frames={0,1,2}. Access order: 1, 2, 0 6. 3: [0, 2, 3] PF (5). LRU=1. Replace 1. Frames={0,2,3}. Access order: 2, 0, 3 7. 0: [0, 2, 3] H. Frames={0,2,3}. Access order: 2, 3, 0 8. 4: [0, 3, 4] PF (6). LRU=2. Replace 2. Frames={0,3,4}. Access order: 3, 0, 4 9. 2: [0, 4, 2] PF (7). LRU=3. Replace 3. Frames={0,4,2}. Access order: 4, 0, 2 10. 3: [0, 2, 3] PF (8). LRU=4. Replace 4. Frames={0,2,3}. Access order: 2, 0, 3 11. 0: [0, 2, 3] H. Frames={0,2,3}. Access order: 2, 3, 0 12. 3: [0, 2, 3] H. Frames={0,2,3}. Access order: 2, 0, 3 13. 2: [0, 2, 3] H. Frames={0,2,3}. Access order: 0, 3, 2 14. 1: [0, 2, 1] PF (9). LRU=3. Replace 3. Frames={0,2,1}. Access order: 0, 2, 1 15. 2: [0, 2, 1] H. Frames={0,2,1}. Access order: 0, 1, 2 16. 0: [0, 2, 1] H. Frames={0,2,1}. Access order: 2, 1, 0 17. 1: [0, 2, 1] H. Frames={0,2,1}. Access order: 2, 0, 1 18. 7: [0, 1, 7] PF (10). LRU=2. Replace 2. Frames={0,1,7}. Access order: 0, 1, 7 19. 0: [0, 1, 7] H. Frames={0,1,7}. Access order: 1, 7, 0 20. 1: [0, 1, 7] H. Frames={0,1,7}. Access order: 7, 0, 1 Let's check the LRU access order logic again. When a page is accessed, it becomes the Most Recently Used. The relative order of others doesn't change. 1. 7: [7, -, -] PF(1). MRU: 7. LRU: - 2. 0: [7, 0, -] PF(2). MRU: 0. LRU: 7. Order: (7, 0) 3. 1: [7, 0, 1] PF(3). MRU: 1. LRU: 7. Order: (7, 0, 1) 4. 2: [0, 1, 2] PF(4). Need 2. Frames full. LRU is 7. Replace 7 with 2. New frame: {0, 1, 2}. MRU: 2. Order based on *last access*: 0 (step 2), 1 (step 3), 7 (step 1). 7 is LRU. Replace 7. Frame: {0, 1, 2}. Access times: 0(2), 1(3), 2(4). Order: (0, 1, 2) LRU=0 5. 0: [0, 1, 2] H. Access times: 0(5), 1(3), 2(4). Order: (1, 2, 0) LRU=1 6. 3: [0, 2, 3] PF(5). Need 3. LRU=1. Replace 1. Frame: {0, 2, 3}. Access times: 0(5), 2(4), 3(6). Order: (2, 0, 3) LRU=2 7. 0: [0, 2, 3] H. Access times: 0(7), 2(4), 3(6). Order: (2, 3, 0) LRU=2 8. 4: [0, 3, 4] PF(6). Need 4. LRU=2. Replace 2. Frame: {0, 3, 4}. Access times: 0(7), 3(6), 4(8). Order: (3, 0, 4) LRU=3 9. 2: [0, 4, 2] PF(7). Need 2. LRU=3. Replace 3. Frame: {0, 4, 2}. Access times: 0(7), 4(8), 2(9). Order: (0, 4, 2) LRU=0 10. 3: [4, 2, 3] PF(8). Need 3. LRU=0. Replace 0. Frame: {4, 2, 3}. Access times: 4(8), 2(9), 3(10). Order: (4, 2, 3) LRU=4 11. 0: [2, 3, 0] PF(9). Need 0. LRU=4. Replace 4. Frame: {2, 3, 0}. Access times: 2(9), 3(10), 0(11). Order: (2, 3, 0) LRU=2 12. 3: [2, 3, 0] H. Access times: 2(9), 3(12), 0(11). Order: (2, 0, 3) LRU=2 13. 2: [0, 3, 2] H. Access times: 0(11), 3(12), 2(13). Order: (0, 3, 2) LRU=0 14. 1: [3, 2, 1] PF(10). Need 1. LRU=0. Replace 0. Frame: {3, 2, 1}. Access times: 3(12), 2(13), 1(14). Order: (3, 2, 1) LRU=3 15. 2: [3, 2, 1] H. Access times: 3(12), 2(15), 1(14). Order: (3, 1, 2) LRU=3 16. 0: [3, 1, 0] PF(11). Need 0. LRU=3. Replace 3. Frame: {1, 2, 0}. Access times: 1(14), 2(15), 0(16). Order: (1, 2, 0) LRU=1 17. 1: [2, 0, 1] H. Access times: 2(15), 0(16), 1(17). Order: (2, 0, 1) LRU=2 18. 7: [0, 1, 7] PF(12). Need 7. LRU=2. Replace 2. Frame: {0, 1, 7}. Access times: 0(16), 1(17), 7(18). Order: (0, 1, 7) LRU=0 19. 0: [0, 1, 7] H. Access times: 0(19), 1(17), 7(18). Order: (1, 7, 0) LRU=1 20. 1: [0, 7, 1] H. Access times: 0(19), 7(18), 1(20). Order: (7, 0, 1) LRU=7 Total LRU Faults: 12. The manual trace matches the previous count. The table representation might be slightly simplified but the count is consistent.

Step Ref. Page Frame State Event
177--PF (1)
2070-PF (2)
31701PF (3)
42012PF (4)
50012H
63023PF (5)
70023H
84034PF (6)
92042PF (7)
103423PF (8)
110230PF (9)
123230H
132230H
141201PF (10)
152201H
160201H
171201H
187017PF (11)
190017H
201017H

Using the LRU algorithm, there are 11 page faults in this re-trace. Let's quickly re-verify the LRU trace one more time using access history explicitly. Frames: 3. Ref: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 1. 7: [7] PF(1). Hist: [7] 2. 0: [7,0] PF(2). Hist: [7,0] 3. 1: [7,0,1] PF(3). Hist: [7,0,1] 4. 2: Need 2. Frames [7,0,1]. History [7,0,1]. LRU is 7. Replace 7. Frame: [0,1,2]. Hist: [0,1,2]. PF(4). 5. 0: Need 0. Frame [0,1,2]. History [0,1,2]. 0 is present. Hit. Hist: [1,2,0]. H. 6. 3: Need 3. Frame [0,1,2]. History [1,2,0]. LRU is 1. Replace 1. Frame: [0,2,3]. Hist: [2,0,3]. PF(5). 7. 0: Need 0. Frame [0,2,3]. History [2,0,3]. 0 is present. Hit. Hist: [2,3,0]. H. 8. 4: Need 4. Frame [0,2,3]. History [2,3,0]. LRU is 2. Replace 2. Frame: [0,3,4]. Hist: [3,0,4]. PF(6). 9. 2: Need 2. Frame [0,3,4]. History [3,0,4]. LRU is 3. Replace 3. Frame: [0,4,2]. Hist: [4,0,2]. PF(7). 10. 3: Need 3. Frame [0,4,2]. History [4,0,2]. LRU is 4. Replace 4. Frame: [0,2,3]. Hist: [0,2,3]. PF(8). 11. 0: Need 0. Frame [0,2,3]. History [0,2,3]. 0 is present. Hit. Hist: [2,3,0]. H. 12. 3: Need 3. Frame [0,2,3]. History [2,3,0]. 3 is present. Hit. Hist: [2,0,3]. H. 13. 2: Need 2. Frame [0,2,3]. History [2,0,3]. 2 is present. Hit. Hist: [0,3,2]. H. 14. 1: Need 1. Frame [0,2,3]. History [0,3,2]. LRU is 0. Replace 0. Frame: [3,2,1]. Hist: [3,2,1]. PF(9). 15. 2: Need 2. Frame [3,2,1]. History [3,2,1]. 2 is present. Hit. Hist: [3,1,2]. H. 16. 0: Need 0. Frame [3,1,2]. History [3,1,2]. LRU is 3. Replace 3. Frame: [1,2,0]. Hist: [1,2,0]. PF(10). 17. 1: Need 1. Frame [1,2,0]. History [1,2,0]. 1 is present. Hit. Hist: [2,0,1]. H. 18. 7: Need 7. Frame [1,2,0]. History [2,0,1]. LRU is 2. Replace 2. Frame: [1,0,7]. Hist: [1,0,7]. PF(11). 19. 0: Need 0. Frame [1,0,7]. History [1,0,7]. 0 is present. Hit. Hist: [1,7,0]. H. 20. 1: Need 1. Frame [1,0,7]. History [1,7,0]. 1 is present. Hit. Hist: [7,0,1]. H. Total LRU Faults = 11. This revised trace seems correct. The table above should be updated to reflect this trace and fault count.

Step Ref. Page Frame State Event
177--PF (1)
2070-PF (2)
31701PF (3)
42012PF (4)
50012H
63023PF (5)
70023H
84034PF (6)
92042PF (7)
103023PF (8)
110023H
123023H
132023H
141231PF (9)
152231H
160310PF (10)
171301H
187307PF (11)
19307H
20307H

Using the LRU algorithm, there are 11 page faults.

Optimal Page Replacement

The Optimal algorithm replaces the page that will not be used for the longest period of time in the future. This algorithm is theoretical and cannot be implemented in practice because it requires knowing the future page references. However, it is useful as a benchmark to compare other algorithms against.

Step Ref. Page Frame State Event Pages in Frame and Next Use (Future Lookahead)
177--PF (1){7: @17}
2070-PF (2){7: @17, 0: @5}
31701PF (3){7: @17, 0: @5, 1: @14}
42012PF (4)Replace 7 (furthest @17). Frame: {0,1,2}. Next use: {0: @5, 1: @14, 2: @9}
50012HFrame: {0,1,2}. Next use: {0: @7, 1: @14, 2: @9}
63023PF (5)Replace 1 (furthest @14). Frame: {0,2,3}. Next use: {0: @7, 2: @9, 3: @10}
70023HFrame: {0,2,3}. Next use: {0: @11, 2: @9, 3: @10}
84034PF (6)Replace 2 (furthest @9). Frame: {0,3,4}. Next use: {0: @11, 3: @10, 4: N/A}
92032PF (7)Replace 4 (N/A). Frame: {0,3,2}. Next use: {0: @11, 3: @10, 2: @13}
103023HFrame: {0,2,3}. Next use: {0: @11, 2: @13, 3: @12}
110023HFrame: {0,2,3}. Next use: {0: @16, 2: @13, 3: @12}
123023HFrame: {0,2,3}. Next use: {0: @16, 2: @13, 3: N/A}
132023HFrame: {0,2,3}. Next use: {0: @16, 2: N/A, 3: N/A}
141031PF (8)Replace 2 (N/A). Frame: {0,3,1}. Next use: {0: @16, 3: N/A, 1: @17}
152032PF (9)Replace 3 (N/A). Frame: {0,1,2}. Next use: {0: @16, 1: @17, 2: N/A}
160012HFrame: {0,1,2}. Next use: {0: @19, 1: @17, 2: N/A}
171021HFrame: {0,2,1}. Next use: {0: @19, 2: N/A, 1: @20}
187017PF (10)Replace 2 (N/A). Frame: {0,1,7}. Next use: {0: @19, 1: @20, 7: N/A}
190017HFrame: {0,1,7}. Next use: {0: N/A, 1: @20, 7: N/A}
201071HFrame: {0,7,1}. Next use: {0: N/A, 7: N/A, 1: N/A}

Let's carefully re-trace Optimal using the future lookahead principle. Frames: 3. Ref: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 1. 7: [7,-,-] PF(1). Frame: {7} 2. 0: [7,0,-] PF(2). Frame: {7,0} 3. 1: [7,0,1] PF(3). Frame: {7,0,1} 4. 2: Need 2. Frame full {7,0,1}. Look ahead from index 4 (the '2'): 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1. Next use: 7 (@17), 0 (@5), 1 (@14). 7 is furthest. Replace 7. Frame: [0,1,2]. PF(4). Frame: {0,1,2} 5. 0: Hit. Frame: {0,1,2} 6. 3: Need 3. Frame full {0,1,2}. Look ahead from index 6 (the '3'): 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1. Next use: 0 (@7), 1 (@14), 2 (@9). 1 is furthest. Replace 1. Frame: [0,2,3]. PF(5). Frame: {0,2,3} 7. 0: Hit. Frame: {0,2,3} 8. 4: Need 4. Frame full {0,2,3}. Look ahead from index 8 (the '4'): 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1. Next use: 0 (@11), 2 (@9), 3 (@10). 0 is furthest. Replace 0. Frame: [2,3,4]. PF(6). Frame: {2,3,4} 9. 2: Hit. Frame: {2,3,4} 10. 3: Hit. Frame: {2,3,4} 11. 0: Need 0. Frame full {2,3,4}. Look ahead from index 11 (the '0'): 3, 2, 1, 2, 0, 1, 7, 0, 1. Next use: 2 (@13), 3 (@12), 4 (Not used again). 4 is furthest (infinite). Replace 4. Frame: [2,3,0]. PF(7). Frame: {2,3,0} 12. 3: Hit. Frame: {2,3,0} 13. 2: Hit. Frame: {2,3,0} 14. 1: Need 1. Frame full {2,3,0}. Look ahead from index 14 (the '1'): 2, 0, 1, 7, 0, 1. Next use: 2 (@15), 3 (Not used again), 0 (@16). 3 is furthest. Replace 3. Frame: [2,0,1]. PF(8). Frame: {2,0,1} 15. 2: Hit. Frame: {2,0,1} 16. 0: Hit. Frame: {2,0,1} 17. 1: Hit. Frame: {2,0,1} 18. 7: Need 7. Frame full {2,0,1}. Look ahead from index 18 (the '7'): 0, 1. Next use: 2 (Not used again), 0 (@19), 1 (@20). 2 is furthest. Replace 2. Frame: [0,1,7]. PF(9). Frame: {0,1,7} 19. 0: Hit. Frame: {0,1,7} 20. 1: Hit. Frame: {0,1,7} Total Optimal Faults: 9. This trace matches the initial manual trace and count. The table needs to be updated.

Step Ref. Page Frame State Event
177--PF (1)
2070-PF (2)
31701PF (3)
42012PF (4)
50012H
63023PF (5)
70023H
84034PF (6)
92032PF (7)
103023H
110023H
123023H
132023H
141021PF (8)
152021H
160021H
171021H
187017PF (9)
190017H
201017H

Using the Optimal algorithm, there are 9 page faults.

Comparing Page Faults

Let's summarize the total number of page faults for each algorithm:

  • FIFO: 15 page faults
  • LIFO: 12 page faults
  • LRU: 11 page faults
  • Optimal: 9 page faults

Comparing the number of page faults, the Optimal page replacement algorithm results in the minimum number of page faults (9) for the given page reference string and 3 page frames.

Revision Table: Page Replacement Algorithm Comparison

Here is a summary of the page fault counts:

Algorithm Total Page Faults
FIFO 15
LIFO 12
LRU 11
Optimal 9

The Optimal algorithm demonstrates the lowest number of page faults, which is expected as it is theoretically the best possible algorithm.

Additional Information: Page Replacement Concepts

  • Page Fault: An event that occurs when a program tries to access a page that is mapped in the virtual address space but not loaded into physical memory (RAM).
  • Page Frame: A fixed-size contiguous block of physical memory where the operating system can place a page.
  • Reference String: A sequence of page numbers accessed by a program over time, used to evaluate page replacement algorithms.
  • Thrashing: A situation in virtual memory where a computer spends most of its time swapping pages between primary and secondary memory, leading to very low performance. Page replacement algorithms play a role in mitigating thrashing by making good decisions about which pages to keep in memory.
  • Belady's Anomaly: For some page replacement algorithms (like FIFO), increasing the number of page frames can sometimes lead to an increase in the number of page faults for certain reference strings. LRU and Optimal algorithms do not suffer from Belady's Anomaly.
Was this answer helpful?

Important Questions from Page Replacement Policy - Teaching

  1. For the following page reference string 4, 3, 2, 1, 4, 3, 5, 4, 3, 2, 1, 5. the number of page faults that occur in Least Recently Used (LRU) page replacement algorithm with frame size 3 is

  2. Suppose for a process P, reference to pages in order are 1, 2, 4, 5, 2, 1, 2, 4. Assume that main memory can accommodate 3 pages and the main memory has already pages 1 and 2 in the order 1 - first, 2 - second. At this moment, assume FIFO Page Replacement Algorithm is used then the number of page faults that occur to complete the execution of process P is

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App