# Clock page replacement algorithm - Already existing pages

When simulating the clock page replacement algorithm, when a reference comes in which is already in memory, does the clock hand still increment?

Here is an example:

With 4 slots, using the clock page replacement algorithm

Reference list: 1 2 3 4 1 2 5 1 3 2 4 5

Initial list would look like this:

-> [1][1]
[2][1]
[3][1]
[4][1]


The next reference to insert would be 1, then 2. Would the hand still point at 1 after 1, and after 2 ? In other words, after inserting the 5, would the clock look like this :

-> [5][1]
[2][0]
[3][0]
[4][0]


?

I think this example can clarify all your doubts.

For example:
Assumes main memory is empty at the start page reference sequence is:
3 2 3 0 8 4 2 5 0 9 8 3 2 one reference bit per frame (called the "used" bit)

  P   U   3    P   U   2   P   U   3   P   U   0   P   U   8   P   U  4
+---+---+    +---+---+   +---+---+   +---+---+   +---+---+   +---+---+
|   | 0 |*   | 3 | 1 |   | 3 | 1 |   | 3 | 1 |   | 3 | 1 |   | 3 | 1 |
+---+---+    +---+---+   +---+---+   +---+---+   +---+---+   +---+---+
|   | 0 |    |   | 0 |*  | 2 | 1 |   | 2 | 1 |   | 2 | 1 |   | 2 | 1 |
+---+---+    +---+---+   +---+---+   +---+---+   +---+---+   +---+---+
|   | 0 |    |   | 0 |   |   | 0 |*  |   | 0 |*  | 0 | 1 |   | 0 | 1 |
+---+---+    +---+---+   +---+---+   +---+---+   +---+---+   +---+---+
|   | 0 |    |   | 0 |   |   | 0 |   |   | 0 |   |   | 0 |*  | 8 | 1 |
+---+---+    +---+---+   +---+---+   +---+---+   +---+---+   +---+---+
|   | 0 |    |   | 0 |   |   | 0 |   |   | 0 |   |   | 0 |   |   | 0 |*
+---+---+    +---+---+   +---+---+   +---+---+   +---+---+   +---+----

P   U   2    P   U   5   P   U   0   P   U   9   P   U   8   P   U  3
+---+---+    +---+---+   +---+---+   +---+---+   +---+---+   +---+---+
| 3 | 1 |*   | 3 | 1 |*  | 5 | 1 |   | 5 | 1 |   | 5 | 1 |   | 5 | 1 |
+---+---+    +---+---+   +---+---+   +---+---+   +---+---+   +---+---+
| 2 | 1 |    | 2 | 1 |   | 2 | 0 |*  | 2 | 0 |*  | 9 | 1 |   | 9 | 1 |
+---+---+    +---+---+   +---+---+   +---+---+   +---+---+   +---+---+
| 0 | 1 |    | 0 | 1 |   | 0 | 0 |   | 0 | 1 |   | 0 | 1 |*  | 0 | 1 |*
+---+---+    +---+---+   +---+---+   +---+---+   +---+---+   +---+---+
| 8 | 1 |    | 8 | 1 |   | 8 | 0 |   | 8 | 0 |   | 8 | 0 |   | 8 | 1 |
+---+---+    +---+---+   +---+---+   +---+---+   +---+---+   +---+---+
| 4 | 1 |    | 4 | 1 |   | 4 | 0 |   | 4 | 0 |   | 4 | 0 |   | 4 | 0 |
+---+---+    +---+---+   +---+---+   +---+---+   +---+---+   +---+----

P   U   2    P   U
+---+---+    +---+---+
| 5 | 1 |*   | 5 | 0 |
+---+---+    +---+---+
| 9 | 1 |    | 9 | 0 |
+---+---+    +---+---+
| 0 | 0 |    | 2 | 1 |
+---+---+    +---+---+
| 8 | 0 |    | 8 | 0 |*
+---+---+    +---+---+
| 3 | 1 |    | 3 | 1 |
+---+---+    +---+---+

* = indicates the pointer which identifies the next location to scan
P = page# stored in that frame
U = used flag,
0 = not used recently
1 = referenced recently

This is called linear scanning algorithm or Second chance algorithm, used in BSD Linux.
Generally it is implemented as a circular queue.

• Could you give an explanation of what this means, in terms of text? It is a nice diagram, but such diagrams are useless when we don't know what it means. Mar 25, 2018 at 14:44

If a reference arrives for a page already in memory, then the replacement algorithm doesn't get invoked at all.

The clock replacement algorithm is trying to achieve some of the benefits of LRU replacement, but without the massive overhead of manipulating the LRU bits on every page hit.

A page can be in one of three states:

1. Present in memory and the recently-used bit is true. In this case there will be no page fault when an access happens to the page, so no bits will change.
2. Present in memory but the recently-used bit is false. In this case the page is also marked in the page table in such a way that if the page is accessed a page fault will occur. (And if the page fault occurs in this case, the only thing the page fault handler does is change the state to recently-used.)
3. The page is not present in memory. In this case we look at the clock-hand. While the clock-hand is pointing to a page with the recently-used bit set true we flip the recently-used bit to false, and then increment clock-hand to point to the next page. When we find a page with recently-used already cleared, that is the page we replace. Then we mark the new page as recently-used and increment the clock-hand to the next page.

Clock is, at heart, a probabilistic algorithm for approximating LRU. If the rate at which the page is being accessed is much higher than the rate at which the clock-hand is coming back around to the same page then the page has a high probability of being marked recently-used. If the rate at which the page is being accessed is low compared to the rate at which the clock-hand is coming back around, then the page is more likely to be in state not recently-used. The most recently-used page will never be replaced. (Why?)