BIT204 ยท 2082 ยท 10 marks
When does page fault occur?Given the 7 page reference strings 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3, find the number of page fault using Optimal Page Replacement, FIFO and Second Chance Replacement algorithm. Assume the size of page frame is 4.[1+9]
When does page fault occur?Given the 7 page reference strings 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3, find the number of page fault using Optimal Page Replacement, FIFO and Second Chance Replacement algorithm. Assume the size of page frame is 4.[1+9]
Page Fault and Page Replacement Algorithms
When Does a Page Fault Occur?
A page fault occurs when a process references a page that is not currently present in main memory (RAM). The CPU generates a trap, and the OS must bring the required page from secondary storage into a frame. If no free frame exists, a page replacement algorithm chooses a victim to evict.
Given Data
- Reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3
- Number of frames: 4
- Total references: 14
1. Optimal Page Replacement
Replace the page not needed for the longest time in the future.
| Ref | Frames (contents) | Fault |
|---|---|---|
| 7 | 7 | F |
| 0 | 7 0 | F |
| 1 | 7 0 1 | F |
| 2 | 7 0 1 2 | F |
| 0 | 7 0 1 2 | Hit |
| 3 | replace 1 โ 7 0 3 2 | F |
| 0 | 7 0 3 2 | Hit |
| 4 | replace 7 โ 4 0 3 2 | F |
| 2 | 4 0 3 2 | Hit |
| 3 | Hit | |
| 0 | Hit | |
| 3 | Hit | |
| 2 | Hit | |
| 3 | Hit |
At ref 3 (index 6), page 1 is never used again โ evict 1.
At ref 4, remaining future refs: 2,3,0,3,2,3. Page 7 never used again โ evict 7.
Optimal Page Faults = 6
2. FIFO Page Replacement
Replace the oldest loaded page.
| Ref | Frames | FIFO Queue | Fault |
|---|---|---|---|
| 7 | 7 | [7] | F |
| 0 | 7 0 | [7,0] | F |
| 1 | 7 0 1 | [7,0,1] | F |
| 2 | 7 0 1 2 | [7,0,1,2] | F |
| 0 | 7 0 1 2 | [7,0,1,2] | Hit |
| 3 | replace 7 โ 0 1 2 3 | [0,1,2,3] | F |
| 0 | 0 1 2 3 | [0,1,2,3] | Hit |
| 4 | replace 0 โ 1 2 3 4 | [1,2,3,4] | F |
| 2 | 1 2 3 4 | [1,2,3,4] | Hit |
| 3 | 1 2 3 4 | [1,2,3,4] | Hit |
| 0 | replace 1 โ 2 3 4 0 | [2,3,4,0] | F |
| 3 | 2 3 4 0 | [2,3,4,0] | Hit |
| 2 | 2 3 4 0 | [2,3,4,0] | Hit |
| 3 | 2 3 4 0 | [2,3,4,0] | Hit |
FIFO Page Faults = 7
3. Second Chance (Clock) Page Replacement
FIFO order maintained, but on a hit the reference bit R is set to 1. When selecting a victim, if the oldest page has R=1, clear it to 0 and move it to the back (give it a second chance); if R=0, replace it.
Notation: page(R).
| Ref | Frames (queue oldestโnewest) | Action | Fault |
|---|---|---|---|
| 7 | 7(0) | load | F |
| 0 | 7(0) 0(0) | load | F |
| 1 | 7(0) 0(0) 1(0) | load | F |
| 2 | 7(0) 0(0) 1(0) 2(0) | load | F |
| 0 | 7(0) 0(1) 1(0) 2(0) | hit, set R(0)=1 | Hit |
| 3 | 7 is oldest, R=0 โ evict 7. New: 0(1) 1(0) 2(0) 3(0) | replace 7 | F |
| 0 | 0(1) 1(0) 2(0) 3(0) | hit (0 already R=1) | Hit |
| 4 | Oldest 0 has R=1 โ clear to 0, move back. Next oldest 1 has R=0 โ evict 1. New: 2(0) 3(0) 0(0) 4(0) | replace 1 | F |
| 2 | 2(1) 3(0) 0(0) 4(0) | hit, set R=1 | Hit |
| 3 | 2(1) 3(1) 0(0) 4(0) | hit, set R=1 | Hit |
| 0 | 2(1) 3(1) 0(1) 4(0) | hit, set R=1 | Hit |
| 3 | 2(1) 3(1) 0(1) 4(0) | hit | Hit |
| 2 | 2(1) 3(1) 0(1) 4(0) | hit | Hit |
| 3 | 2(1) 3(1) 0(1) 4(0) | hit | Hit |
Step detail at ref 4: clock pointer at oldest = 0 (R=1) โ set R=0, advance; page 1 (R=0) โ victim. Result frames = {2,3,0,4}. This matches FIFO's replacement here because giving 0 a second chance saved it.
Second Chance Page Faults = 6
Summary
| Algorithm | Page Faults |
|---|---|
| Optimal | 6 |
| FIFO | 7 |
| Second Chance | 6 |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.