During address ing, if you find that the page to be accessed is no longer in memory, a page disconnection occurs. When a page disconnection occurs, the operating system must select a page in the memory to remove it from the memory, so as to make room for the page to be transferred. The rule used to select which page to discard is called the page replacement algorithm. Common replacement algorithms include:
1) Optimal Replacement Algorithm (OPT) (ideal replacement algorithm)
This is an ideal page replacement algorithm, but it is not actually possible. The basic idea of this algorithm is: when a page is missing, some pages are in the memory, and one of them will be accessed soon (the page that also contains the next command ), other pages may be accessed only after 10, 100, or 1000 commands. Each page can be marked with the number of commands to be executed before the page is accessed for the first time. The best page replacement algorithm simply stipulates that the largest page marked should be replaced. The only problem with this algorithm is that it cannot be implemented. When a page is missing, the operating system cannot know when each page is accessed next time. Although this algorithm is not possible, the optimal page replacement algorithm can be used to measure and compare the performance of the algorithm.
2) FIFO algorithm (FIFO)
The simplest page replacement algorithm is the first-in-first-out (FIFO) method. The essence of this algorithm is that it always chooses a page replacement with the longest (the oldest) Stay in the primary storage, that isGo to the Memory Page and exit the memory first.. The reason is: the page that is first transferred to memory is no longer used than the page that is just transferred to memory. Create a FIFO queue and the shelter has pages in the memory. The replaced page is always on the queue header. When a page is put into memory, it is inserted at the end of the team.
This algorithm is ideal only when the address space is accessed in a linear order, otherwise the efficiency is not high. Because those pages that are frequently accessed often stay in the primary storage for the longest time, they have to be replaced because they become "old.
Another disadvantage of FIFO is that it has an exception, that is, when the storage block is added, the page-missing interruption rate increases. Of course, the page trend that causes such exceptions is rare.
3) least recently used (LRU) Algorithm
The main difference between the FIFO algorithm and the OPT algorithm is that the FIFO algorithm uses the length of time after the page enters the memory as the basis for replacement, while the OPT algorithm uses the page time in the future. If we use the recent past as an approximation of the near future, we can replace the pages that have not been used for the longest period of time in the past. Its essence is that when one page needs to be replaced,Replace the pages that have not been used for the longest time in the recent period.. This algorithm is calledAlgorithm not used for a long time(Least recently used, LRU ).
The LRU algorithm is related to the last time used on each page. When you must replace a page, the LRU algorithm selects the page that has not been used for the longest time in the past.
The LRU algorithm is a frequently used page replacement algorithm and is considered to be quite good, but there is a problem of how to implement it. The LRU algorithm requires hardware support. The problem is how to determine the sequence of last time used. There are two feasible methods:
1. counters. The simplest case is that each page table item corresponds to a time field and a logical clock or counter is added to the CPU. This clock is incremented by 1 for each storage access. When you access a page, the content of the clock register is copied to the time field used by the corresponding page. In this way, we can keep the "time" of the last access to each page ". On the Replace page, select the page with the minimum time value. In this way, we should not only check the page table, but also maintain the time in the page table when the page table changes (due to CPU scheduling) and take into account the problem of clock overflow.
2. Stack. Use a stack to retain the page number. Every time a page is accessed, it is taken out from the stack and placed on the top of the stack. In this way, the top of the stack always has the most used pages, while the bottom of the stack contains the pages that are currently the least used. To remove an item from the middle of the stack, use a bidirectional link with a head and tail pointer. In the worst case, removing a page and placing it on the top of the stack requires Six pointers to be changed. Each modification requires overhead, but the page to be replaced can be directly obtained without searching, because the tail Pointer Points to the bottom of the stack, where there is a replaced page.
The implementation of the LRU algorithm requires a large amount of hardware support and requires a certain amount of software overhead. Therefore, the actual implementation is a simple and effective LRU approximation algorithm.
The LRU approximation algorithm isAlgorithm not used recently(Not recently used, Nur ). It adds a reference bit to each item in the block table, and the operating system regularly sets them to 0. When a page is accessed, hardware places the location 1. After a while, you can check these bits to determine which pages have been used and which pages have not been used since the last 0. You can remove a page with a value of 0 because it has not been accessed in the recent period.
4) Clock replacement algorithm (approximate Implementation of LRU algorithm)
5) least use (LFU) Replacement Algorithm
When the minimum replacement algorithm is used, a shift register should be set for each page in the memory to record the Access Frequency of the page. The replacement algorithm selects the least pages used in the recent period as the elimination page. Because the memory has a high access speed, such as 100 ns, a page may be accessed for thousands of times in one Ms. Therefore, generally, you cannot directly use counters to record the number of times a page is accessed. Instead, you can use shift registers. Each time you access a page, the highest position of the shift register is 1, and then shifted right at a certain time (such as 100 ns. In this way, the least used page in the recent period will be the smallest page of Σ Ri.
The page access diagram of the lfu replacement algorithm is exactly the same as that of the LRU replacement algorithm; or, using such a set of hardware, You can implement the LRU algorithm and the LFU algorithm. It should be pointed out that the LFU algorithm does not really reflect the page usage, because at each interval, only one register is used to record the page usage. Therefore, one access is equivalent to 10 000 access requests.
Page Replacement Algorithm