Short answer: The clock algorithm, or second chance, approximates LRU with a circular list of frames and one reference bit each. The clock hand walks the circle. A frame whose bit is 1 gets the bit cleared and a second chance. A frame whose bit is 0 is evicted. A hit sets the bit. The hand moves only on a miss that needs a victim, so a quiet system does no list traffic. The policy is not exact LRU and does not promise the stack property.
What the problem actually asks
Exact LRU updates a linked list on every reference. A CPU cache or a kernel frame table cannot afford that on every load. The hardware can set a bit when a page is touched. The clock scans those bits lazily, only when a victim is required, and interprets a set bit as “used since the last sweep.”
Clearing the bit and moving on is the second chance. A page that is touched often keeps finding its bit set and stays. A page that has gone cold is caught with a clear bit and leaves. The order is an approximation of recency, smeared over the time of one revolution.
What the algorithm guarantees
A victim is found after at most one full revolution if every set bit is cleared on the way, because the second pass sees the zeros the first pass left. Time per miss is O(1) when a zero bit is nearby and O(n) in the worst revolution. Extra memory is one bit and one hand index. The algorithm can be extended with a dirty bit so a clean page is preferred, which saves a write-back. That preference is a practical refinement and a further departure from pure LRU.
Because the bit is one bit of history, two pages used at different times inside the same revolution can look identical. LRU would order them. The clock will not. Belady’s anomaly is not ruled out by the stack argument, because the stack argument needed exact LRU.
The working steps
- Keep frames in a circle and a hand index. A reference sets the frame’s bit to 1.
- When a victim is required, inspect the hand. If the bit is 1, clear it and advance. If the bit is 0, evict that frame and advance the hand past it.
- Load the new page into the freed frame and set its bit to 1.
- A dirty-bit variant skips a dirty frame once, or writes it back only when a clean victim cannot be found, according to the scheduler the system chose.
- Do not update a linked-list order on a hit. The bit is the entire recency record.
Second chance is an approximation of LRU, not a proof that the victim is the least recently used page. Exact LRU belongs in a software structure that can move a node on every hit. FIFO is what remains if the reference bit is never set after admission.
Approaches that look simpler and fail
| Approach | Why it fails when a set bit is treated as a permanent exemption |
|---|---|
| Skip every frame whose bit is 1 and never clear it | After every resident page has been touched, the hand has no victim. Clearing the bit is the second chance and the progress argument. |
| Claim the evicted page is the LRU page | The bit does not order two pages that were both used since the last sweep. The victim is a page the clock found cold, not the exact minimum. |
| Update the bit only on admission | The policy collapses toward FIFO. Hits have to set the bit for the approximation to see reuse. |
| Inherit LRU’s immunity to Belady’s anomaly | That immunity was proved for the exact stack policy. The clock can disagree with LRU on the same trace. |
Exact recency is LRU with a linked list. The offline optimum is Belady’s algorithm. The clock is the reference-bit sweep used when a hit must stay cheap.
A working example
This Python fragment gives a referenced frame a second chance and clears the bit as the hand passes. Frames start clear. Requests 0, 1, 0 fill two frames and then reference 0. The next miss, page 2, finds every bit set, clears them on one revolution, and evicts the page under the hand, which is page 0.
frames = [None, None]
ref = [0, 0]
hand = 0
def access(page):
global hand
for i, p in enumerate(frames):
if p == page:
ref[i] = 1
return None
while True:
if ref[hand] == 0:
victim = frames[hand]
frames[hand] = page
ref[hand] = 1
hand = (hand + 1) % len(frames)
return victim
ref[hand] = 0
hand = (hand + 1) % len(frames)
for page in (0, 1, 0):
access(page)
victim = access(2)
assert victim == 0
print(victim)
Where it is used
The clock algorithm is the operating-system approximation of LRU: a hand sweeps reference bits instead of moving a page to the front of a list on every access. Linux page reclaim uses active and inactive lists in that spirit. A strict LRU list is what a small user-space cache can afford.
When you can drop this algorithm
Drop the clock when every reference can update a list cheaply. Exact LRU is then available. Keep the clock behind a reference bit, and always clear the bit when the hand passes.
Why this stays published
We publish this so a search for the clock algorithm returns the second-chance bit clear and the reason it is not exact LRU. The clock algorithm approximates LRU by evicting the first frame whose reference bit is clear, giving a set bit a second chance and clearing it as the hand passes.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
