Page Replacement Algorithms
Page Replacement Algorithms
h>
return page_faults;
}
// Driver Code
int main() {
int pages[] = {1, 3, 0, 3, 5, 6, 3, 5, 1, 3, 6, 3}; // Page
reference string
int n = sizeof(pages) / sizeof(pages[0]);
int capacity = 3; // Number of frames
#include <stdio.h>
return page_faults;
}
// Driver Code
int main() {
int pages[] = {1, 3, 0, 3, 5, 6, 3, 5, 1, 3, 6, 3}; // Page
reference string
int n = sizeof(pages) / sizeof(pages[0]);
int capacity = 3; // Number of frames
printf("📌 LRU Page Replacement Simulation\n");
printf("===================================\
n");
return 0;
}
Step 1: 1 - -
Step 2: 1 3 -
Step 3: 1 3 0
Step 4: 1 3 0
Step 5: 5 3 0
Step 6: 5 6 0
Step 7: 5 6 3
Step 8: 5 6 3
Step 9: 1 6 3
Step 10: 1 3 3
Step 11: 1 3 6
Step 12: 1 3 6
// Initialize frames
for (int i = 0; i < capacity; i++) {
frames[i].page = -1; // Empty frame
frames[i].refBit = 0; // Reference bit set to 0
}
// Driver Code
int main() {
int pages[] = {1, 3, 0, 3, 5, 6, 3, 5, 1, 3, 6, 3}; // Page
reference string
int n = sizeof(pages) / sizeof(pages[0]);
int capacity = 3; // Number of frames
printf("=======================================
============\n");
return 0;
}
📌 Explanation
1. FIFO with a Second Chance:
o Each page has a reference bit (1 if recently used, 0
if not).
o If a page with reference bit 1 is encountered, it
gets a second chance and its bit is reset to 0.
o If a page with reference bit 0 is found, it is
replaced.
2. Page Fault Handling:
o If the page is in memory, set its reference bit to 1.
📌 Sample Output
yaml
CopyEdit
📌 Second-Chance (Clock) Page Replacement Simulation
============================================
=======
Step 1: 1(1) - -
Step 2: 1(1) 3(1) -
Step 3: 1(1) 3(1) 0(1)
Step 4: 1(1) 3(1) 0(1)
Step 5: 5(1) 3(1) 0(0)
Step 6: 5(1) 6(1) 0(0)
Step 7: 5(1) 6(1) 3(1)
Step 8: 5(1) 6(1) 3(1)
Step 9: 1(1) 6(1) 3(0)
Step 10: 1(1) 3(1) 3(0)
Step 11: 1(1) 3(1) 6(1)
Step 12: 1(1) 3(1) 6(1)
Total Page Faults: 8
(Each page is shown with its reference bit in parentheses)
📌 Complexity Analysis
Best case: O(1) (If all pages fit in memory)
fault)
Average case: O(n)