Operating system-page replacement algorithm

Page replacement algorithm

The algorithm that selects to swap out pages when there is no free space in the memory is called Page-Replacement Algorithms.
Inappropriate algorithms may cause the process to find " jitter ", that is, the page that has just been swapped out will be accessed again soon, and it needs to be reloaded. Frequent page replacement may occur, so that a process may cause a large Part of the time is spent on page replacement work. A good page replacement algorithm should have a low page replacement frequency.

Best permutation algorithm and first-in first-out permutation algorithm

The best replacement algorithm is an idealized algorithm, and the best replacement algorithm is usually used as a standard to evaluate the pros and cons of other algorithms. The first-in-first-out replacement algorithm is the most intuitive algorithm. Because it is not consistent with the regular page usage, it may be the worst-performing algorithm, so it has very few practical applications.

Best replacement algorithm.
The selected page to be eliminated will never be used in the future, perhaps the page that will not be accessed in the longest (in the future) time (that is, the next access time is the latest). With the best replacement algorithm, the lowest page fault rate can usually be guaranteed.

First-in, first-out page replacement algorithm.
The page that enters the memory first is always eliminated, that is, the page with the longest resident time in the memory is selected to be eliminated.
The performance of the FIFO replacement algorithm is poor because it is based on the time when each page is transferred into the memory, and the sequence of page transfer does not reflect the usage of the page.



The least recently used (LRU) algorithm:

The decision is made based on the usage of the page after it is loaded into the memory. That is, the page that has not been used the most recently is selected to be eliminated. The algorithm gives each page an access field to record the time t elapsed since a page was last accessed. When a page needs to be eliminated, select the existing page with the largest t value, that is, the longest unused To be eliminated.

The best replacement algorithm is based on the perspective of "looking backwards", that is, it is based on the usage of subsequent pages; while the LRU algorithm is "looking forward", that is, it is judged based on the previous usage of each page. There is no inevitable connection between the past and future trends of the page.
Hardware support for LRU replacement algorithm.

  1. register.
    Configure a shift register for each page in the memory, which can be expressed as
    Insert picture description here
    when the process accesses a physical block, the Rn-1 position of the corresponding register is set to 1. At this time, the timing signal will shift the register one bit to the right every certain time (for example, 100 ms). If we regard the number of n-bit registers as an integer, then the page corresponding to the register with the smallest value is the page that has not been used the most recently.

  2. Stack.
    A special stack can be used to save the page number of each page currently in use. Whenever a process accesses a page, the page number of the page is removed from the stack and pushed onto the top of the stack. Therefore, the top of the stack is always the number of the most recently visited page, and the bottom of the stack is the number of the most recently unused page.

The least recently used LFU replacement algorithm.

The algorithm uses a shift register method. Every time a certain page is accessed, the highest position of the shift register is 1, and then shifted to the right at regular intervals (for example, 100 ms). The page access graph of the LFU replacement algorithm is exactly the same as the access graph of the LRU replacement algorithm; in other words, using such a set of hardware can realize both the LRU algorithm and the LFU algorithm. However, the LRU algorithm finds the least recently used page based on treating n bits as an integer, while the least used page of the LFU algorithm in the most recent period of time will be the page with the smallest ∑Ri.
It should be pointed out that the LFU algorithm does not really reflect the usage of the page, because in each time interval, only one bit of the register is used to record the usage of the page. Therefore, in this time interval, a page is accessed once and 1000 visits are completely equivalent.



Clock replacement algorithm

Although the LRU algorithm is a better algorithm, because it requires more hardware support, in practical applications, most of the approximate algorithms of LRU are used. Clock algorithm is a kind of LRU approximation algorithm that is used more.

Simple Clock replacement algorithm.
Only one access bit is set for each page, and then all pages in the memory are linked into a circular queue through the link pointer. When a page is accessed, its access bit is set to 1. When the replacement algorithm selects a page to be eliminated, it only needs to check the page access bit. If it is 0, select the page to be swapped out; if it is 1, set it to 0 again, not swap out temporarily, and give the page a second chance to reside in memory, and then check the next page according to the FIFO algorithm. The algorithm checks the usage of each page cyclically. This algorithm is also called the recently unused algorithm NRU.

Improved Clock replacement algorithm:

In the improved Clock algorithm, in addition to considering the usage of the page, another factor must be added, namely the replacement cost. In this way, when selecting a page to swap out, it must be an unused page or an unmodified page. page. The pages that meet these two conditions at the same time are regarded as the first pages to be eliminated. The access bit A and modification bit M can be combined into the following four types of pages:

Type 1 (A=0, M=0): Indicates that the page has not been visited or modified recently, and is the best eliminated page.

Type 2 (A=0, M=1): It means that the page has not been visited recently, but has been modified, which is not a good elimination page.

Type 3 (A=1, M=0): Indicates that the page has been visited recently but has not been modified, and the page may be visited again.

Type 4 (A=1, M=1): Indicates that the page has been visited and modified recently, and the page may be visited again.

Each page in the memory must be one of these four types of pages. When performing page replacement, the difference from the simple Clock algorithm is that the algorithm must simultaneously check the access bit and the modified bit to determine which of the four types of page the page is. One kind. The execution process can be divided into the following three steps:

  1. Starting from the current position indicated by the pointer, scan the circular queue to find the first type of page with A=0 and M=0, and use the first page encountered as the selected eliminated page. The access bit A is not changed during the first scan.
  2. If the first step fails, that is, the first type of page is not encountered after a week of searching, the second round of scanning is started, looking for the second type of page with A=0 and M=1, and the first type of page encountered As an obsolete page. During the second round of scanning, the access bits of all scanned pages are set to 0.
  3. If the second step also fails, that is, the second type of page is not found, the pointer is returned to the starting position, and all access bits have been reset to 0. Then repeat the first step. If it still fails, repeat the second step if necessary. At this time, you will be able to find the eliminated page.
    Compared with the simple Clock algorithm, this algorithm can reduce the number of disk I/O operations. But in order to find a replaceable page, it may have to go through several rounds of scanning. In other words, the cost of implementing the algorithm itself will increase.
Page buffering algorithm PBA

Several factors affecting the efficiency of page swapping in and out

  1. Page replacement algorithm.
  2. The frequency of writing back to disk. For pages that have been modified, they should be written back to disk when they are swapped out.
    But if a linked list of modified swap-out pages has been established in the system, for each page to be swapped out (modified), the system can temporarily not write them back to disk, but hang them in the modified On the linked list of swapped out pages, only when the number of swapped out pages reaches a certain value, such as 64 pages, they are written back to the disk together, which significantly reduces the number of disk I/O operations. In other words, reduce the cost of swapping out modified pages.
  3. Frequency of reading into memory. After setting the modified swap-out page linked list, there will be a batch of pages with data on the linked list temporarily. If a process needs to access these pages again when the batch of data has not been written back to the disk, there is no need to go from outside. Save up and transfer in, and directly obtain from the linked list of modified swap-out pages, which can also reduce the frequency of reading pages from disk into memory and reduce the overhead of page swapping out.

Page buffering algorithm PBA

PBA algorithm features: ① Significantly reduces the frequency of page swapping in and out. ② It is precisely because the cost of swapping in and swapping out is greatly reduced that it can use a simpler replacement strategy, such as the first-in first-out (FIFO) algorithm.

In this system, variable allocation and partial replacement are adopted in the memory allocation strategy, and two linked lists are set in the memory at the same time:

  1. The linked list of free pages is
    actually a linked list of free physical blocks. When a process needs to read a page, it can use the first physical block in the free physical block list to load the page. When there is an unmodified page to be swapped out, the physical block in which they are located is hung at the end of the free list.
  2. The linked list
    of modified pages is a linked list formed by modified pages. When the process needs to swap out a modified page, the system does not immediately swap it out to the external memory, but hangs the physical block it is in at the end of the modified page linked list. When a certain number is reached, they are written back to disk together.

C++ language realizes LRU and LFU

Guess you like

Origin blog.csdn.net/weixin_43743711/article/details/115011084