PageReplacementAlgorithmsinComputerArchitecture-StudyGuide
PageReplacementAlgorithmsinComputerArchitecture-StudyGuide
The scenario:
Page Fault
A page fault occurs when the CPU tries to access a page that is not present in the main memory.
In such cases, the operating system needs to:
1. Locate the required page in virtual memory (e.g., on the hard drive).
2. Find a page in main memory to replace.
3. Swap the page from virtual memory into main memory.
4. Update the page table to reflect the new page in memory.
The selection of a page to be replaced is critical for performance. The chosen page is swapped
out (moved from main memory to secondary storage), and the requested page is swapped in
(moved from secondary storage to main memory).
Common Page Replacement Algorithms
FIFO (First-In, First-Out)
Concept: The oldest page in memory is replaced, regardless of how frequently it is being
used.
Mechanism: Pages are treated as if they are in a queue. The first page that entered the queue
is the first to be removed.
1. 7, 0, 1 (3 page faults)
2. 2 replaces 7: 2, 0, 1 (1 page fault)
3. 0 (No page fault)
4. 3 replaces 0: 2, 3, 1 (1 page fault)
5. 0 replaces 2: 0, 3, 1 (1 page fault)
6. 4 replaces 3: 0, 4, 1 (1 page fault)
7. 2 replaces 0: 2, 4, 1 (1 page fault)
8. 3 replaces 4: 2, 3, 1 (1 page fault)
9. 0 replaces 2: 0, 3, 1 (1 page fault)
10. 3 (No page fault)
11. 2 replaces 3: 0, 2, 1 (1 page fault)
12. 1 (No page fault)
13. 2 (No page fault)
14. 0 (No page fault)
15. 1 (No page fault)
16. 7 replaces 0: 7, 2, 1 (1 page fault)
17. 0 replaces 2: 7, 0, 1 (1 page fault)
18. 1 (No page fault)
1. 7, 0, 1 (3 page faults)
2. 2 replaces 7: 2, 0, 1 (1 page fault)
3. 0 (No page fault)
4. 3 replaces 1: 2, 0, 3 (1 page fault)
5. 0 (No page fault)
6. 4 replaces 2: 0, 4, 3 (1 page fault)
7. 2 replaces 0: 2, 4, 3 (1 page fault)
8. 3 (No page fault)
9. 0 replaces 4: 2, 0, 3 (1 page fault)
10. 3 (No page fault)
11. 2 (No page fault)
12. 1 replaces 0: 2, 1, 3 (1 page fault)
13. 2 (No page fault)
14. 0 replaces 3: 2, 1, 0 (1 page fault)
15. 1 (No page fault)
16. 7 replaces 2: 7, 1, 0 (1 page fault)
17. 0 (No page fault)
18. 1 (No page fault)
OPT (Optimal)
Concept: Replaces the page that will not be used for the longest time in the future.
Mechanism: Requires knowledge of the entire future sequence of page requests.
Practicality: Not implementable in a real-time operating system because it's impossible to
predict the future. Used primarily for benchmarking other algorithms.
1. 7, 0, 1 (3 page faults)
2. 2 replaces 7: 2, 0, 1 (1 page fault)
3. 0 (No page fault)
4. 3 replaces 1: 2, 0, 3 (1 page fault)
5. 0 (No page fault)
6. 4 replaces 2: 0, 4, 3 (1 page fault)
7. 2 replaces 3: 0, 4, 2 (1 page fault)
8. 3 replaces 4: 0, 3, 2 (1 page fault)
9. 0 (No page fault)
10. 3 (No page fault)
11. 2 (No page fault)
12. 1 replaces 0: 1, 3, 2 (1 page fault)
13. 2 (No page fault)
14. 0 replaces 3: 1, 0, 2 (1 page fault)
15. 1 (No page fault)
16. 7 replaces 2: 1, 0, 7 (1 page fault)
17. 0 (No page fault)
18. 1 (No page fault)
Key Considerations
Locality of Reference: Programs tend to access memory locations that are near each other in
time. This principle is the basis for the effectiveness of many page replacement algorithms.
Number of Frames: Increasing the number of frames (memory allocated to a process)
typically reduces the page fault rate.