Because the two implementations of the LRU(least recently used) algorithm are cumbersome and expensive, the NFU(infrequently used) algorithm that simulates the LRU algorithm with software is proposed, but NFU There are some problems with the algorithm, such as a page that is frequently used in the first scan in a multiple-scan compiler, and the counter value may still be high when the program enters the second scan. In fact, if the first scan is executed exactly the longest in each scan, the counter with pages that contain the subsequent scan code may always be smaller than the page containing the first scan code, and the result is that the operating system will delete the useful pages instead of the pages that are no longer in use.
Therefore, in order to make the NFU algorithm can better simulate the LRU algorithm, it needs to be modified, the modification is divided into two parts: the first is that the counter is shifted right one bit before the R bit is added; the second is R The bit is added to the leftmost side of the counter instead of the far right. This is the aging algorithm.
The result of this modification of the aging algorithm is obvious, because each time the counter is shifted, which is equivalent to "clear" the last time the count value, but this value is not completely removed but saved in the next bit, and then by adding R to the high To determine which pages need to be eliminated, thus ensuring that the minimum amount of the counter value required to retire the page is definitely the least recently accessed page.
| |
Clock cycle 0   |
Clock cycle 1    |
Clock cycle 2   |
Clock cycle 3 |
Clock cycle 4 |
| 0 |
10000000   |
11000000 |
11100000    |
11110000 |
01111000 |
| 1 |
00000000    |
10000000 |
11000000   |
01100000 |
10110000 |
| 2 |
10000000   |
01000000 |
00100000   |
00010000 |
10001000 |
| 3 |
00000000   |
00000000 |
10000000   |
01000000 |
00100000 |
| 4 |
10000000   |
11000000 |
01100000    |
10110000 |
01011000 |
Page 5 |
10000000 |
01000000 |
10100000 |
01010000 |
00101000 |
Using software to simulate LRU 's aging algorithm 6 pages in 5 clock Cycle counter situation
as you can see from the table above, all counters have a value of 0before they begin, each with a clock cycle first moving the counter one bit to the right, and then adding the new R bit to the leftmost side of the counter, the page with the lowest counter value when the elimination occurs.
  in 5 clock cycle has a selection problem, page 3 and 5 have not been accessed for two consecutive cycles, and they have been visited during a period prior to two cycles. According to lru , if a page has to be eliminated, we should select one of the two pages. However the problem now is that we do not know in the clock cycle 1 to Clock cycle 2 during which of these two pages is accessed, only one record in each clock cycle makes it impossible to distinguish between earlier and later accesses in a cycle, all we can do is eliminate the page 3 because page 5 The is also accessed in the forward cycle and the page 3 is not.
Another problem with aging algorithms is that the counters always have only a finite number of bits, as shown in the table above, with only 8 -bit counters, which means that the initial access data is flushed out, and if the count of two pages happens to be the same, The only way to do this is to randomly eliminate one of the two pages.
Operating System: Aging algorithm