Animate the Clock (Second Chance) cache replacement used in real operating systems. Watch the clock hand sweep and reference bits flip.
About this algorithm
Clock is the page replacement method most operating systems use to approximate LRU cheaply. Pages sit in a circle of frames, each with one reference bit, and a hand points at one frame. A hit just sets the page's bit. On a miss, the hand sweeps forward: a page with its bit set gets a second chance (the bit is cleared and the hand moves on), and the first page found with a clear bit is replaced.
Use Clock when you want behaviour close to LRU but cannot afford to reorder a list on every access. That is the situation in virtual memory: the hardware sets a page's referenced bit for free on every access, and the kernel only reads the bits when it needs a free frame. FIFO is simpler still but ignores use entirely; LFU counts uses rather than recency; MRU is for looping scans. Many operating system kernels use a variant of it.
Give every frame a page slot and one reference bit, and start the hand at frame 0.
Find the page in its frame and set its reference bit to 1. Nothing moves and the hand stays where it is.
Look at the frame under the hand. If its bit is 1, clear it to 0 and move the hand one frame on. Repeat until the hand reaches a frame whose bit is already 0. If every bit was set, the hand goes all the way round, clearing them, and stops where it started.
Put the new page in that frame, set its bit to 1, and move the hand one frame forward so the next search starts after it.
The page requests 1 2 3 4 1 2 5 1 2 3 4 5 into four frames. The first four misses fill frames 0 to 3, each with its bit set, and the hand wraps back to frame 0. Requests 1 and 2 hit, but their bits were already 1, so nothing changes. When 5 misses, all four bits are set: the hand sweeps once round clearing them, arrives back at frame 0, and replaces 1. Now 2, 3 and 4 have clear bits, so the next misses take them in turn: 1 replaces 2, 2 replaces 3, 3 replaces 4. When 4 arrives every bit is set again, another full sweep follows, and 5 is replaced. Finally 5 replaces 1. The run ends with 2 hits and 10 misses, the same as FIFO: every bit was set when it mattered, and a full sweep falls back to arrival order.
On this page's sequence Clock matches FIFO at every capacity the slider allows, so try a short one on paper: 1 2 3 4 2 5 2 with three frames. Request 4 sweeps the full circle and replaces 1, leaving the bits of 2 and 3 cleared. The hit on 2 sets its bit again. When 5 arrives the hand is on 2, clears its bit and moves on to replace 3, so the last request, 2, hits. FIFO would have evicted 2 for 5. That skipping of recently used pages is what lets Clock track LRU on real workloads.
This page sets a new page's bit to 1 when it is loaded, as the code panel shows. Some textbooks load pages with the bit at 0, so a page must be used once after loading to earn its second chance. Both are common, and they can pick different victims.
Time: a hit sets one bit, and a miss may sweep the full circle once, clearing bits as it goes, so the worst miss touches every frame; since each sweep step clears a bit that some earlier access set, the cost averages out to constant per request. Space: one slot and one bit per frame, plus the hand.