The second-chance (clock) algorithm approximates LRU with one reference bit per frame, which the hardware sets whenever the page is used. A hand goes round the frames like a clock hand looking for a victim.
What to build
Count the page faults of the clock algorithm and show the frames at the end.
Requirements
- Memory has
fframes, all empty at the start, and the hand points at frame 0. A reference to a page already in a frame is a hit: its reference bit is set to 1. - A fault when some frame is empty puts the page in the lowest-numbered empty frame, sets its bit to 1, and leaves the hand alone.
- A fault when every frame is full: while the frame under the hand has its bit set, clear the bit and move the hand to the next frame (wrapping round). Then the page in that frame is the victim: put the new page there, set its bit to 1 and move the hand to the next frame.
- Print
faults: F,hits: Handframes:followed by the page in each frame, in frame order (-for an empty frame).
Input
The first line has f and n (1 ≤ f ≤ 8, 1 ≤ n ≤ 100). The second line has the n page numbers (0 to 99).
Output
Three lines: faults: F, hits: H and frames: ....
Constraints
The hand always stops, because after one full turn every bit is clear.
Example
Input:
3 12
1 2 3 4 1 2 5 1 2 3 4 5
Output:
faults: 9
hits: 3
frames: 5 3 4
Why it works
A page that was used since the hand last passed has its bit set, so it survives one more turn: it gets a second chance. Pages that nobody touched are thrown out. The hand and the bits together cost far less than the time stamps true LRU needs, and give close to the same result.
Examples
3 12 1 2 3 4 1 2 5 1 2 3 4 5
faults: 9 hits: 3 frames: 5 3 4
2 5 7 7 8 7 9
faults: 3 hits: 2 frames: 9 8
Hints
Hint 1
Keep three arrays: page[f], used[f] (the reference bit) and a hand index. Empty frames hold -1.
Hint 2
Hit: set the bit of that frame to 1. Fault with an empty frame: use the lowest-numbered empty one, set its bit to 1, leave the hand where it is.
Hint 3
Fault with all frames full: while the bit at the hand is 1, clear it and move the hand on; then replace that frame, set its bit to 1 and move the hand on once more.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
int main(void) {
int f, n, page, frame[8], used[8], hand = 0, faults = 0, hits = 0;
scanf("%d %d", &f, &n);
for (int i = 0; i < f; i++) {
frame[i] = -1;
used[i] = 0;
}
for (int i = 0; i < n; i++) {
scanf("%d", &page);
int found = -1, empty = -1;
for (int j = 0; j < f; j++) {
if (frame[j] == page) found = j;
if (frame[j] < 0 && empty < 0) empty = j;
}
if (found >= 0) {
hits++;
used[found] = 1;
continue;
}
faults++;
if (empty >= 0) {
frame[empty] = page;
used[empty] = 1;
} else {
while (used[hand]) {
used[hand] = 0;
hand = (hand + 1) % f;
}
frame[hand] = page;
used[hand] = 1;
hand = (hand + 1) % f;
}
}
printf("faults: %d\n", faults);
printf("hits: %d\n", hits);
printf("frames:");
for (int i = 0; i < f; i++) {
if (frame[i] < 0) printf(" -");
else printf(" %d", frame[i]);
}
printf("\n");
return 0;
}
▶ Open this solution in the compiler, then press Visualize to watch it run step by step