Operating system-virtual memory (page replacement algorithm)


During the address mapping process, if it is found in the page that the page to be accessed is not in the memory, a page fault interrupt is generated.
When a page fault occurs, if there are no free pages in the operating system's memory, the operating system must select a page in the memory to move it out of the memory to make room for the page to be loaded. The rule used to choose which page to eliminate is called the page replacement algorithm .

1. Optimal Replacement Algorithm (OPT)

  • The selected page to be eliminated will never be used in the future , perhaps a page that will no longer be visited for the longest time in the future .
  • With the best replacement algorithm, the lowest page fault rate can usually be guaranteed.
  • However, because people cannot predict which page will not be accessed for the longest time in the future, this algorithm cannot be implemented , but it can be used to evaluate other algorithms.
    Insert picture description here

2. First-in, first-out page replacement algorithm (FIFO)

  • This algorithm always eliminates the page that enters the memory first , that is, selects the page that has been in memory for the longest time to be eliminated.
  • The implementation of this algorithm is the same as the queue . The OS maintains a linked list of all pages currently in memory. The newly entered page is at the end and the oldest is at the head. Whenever a page fault occurs, it replaces the head of the table. Page and add the newly transferred page to the end of the linked list.
    Insert picture description here

3. The least recently used replacement algorithm (LRU)

  • When a page needs to be eliminated, always select the page that has not been used the most recently to be eliminated
    Insert picture description here
  • Hardware support for LRU replacement algorithm
    • register
      • In order to record the usage of each page in the memory of a certain process, a shift register must be configured for each page in the memory.
        -
    • Stack
      • 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 accessed page, and the bottom of the stack is the number of the most recently unused page.
        Insert picture description here

Fourth, the least used replacement algorithm (LFU)

  • When using this algorithm, a shift register should be set for each page in the memory to record how often the page is accessed .
  • When a page needs to be eliminated, the page with the least use in the most recent period is always selected to be eliminated .
  • The access graphs of LFU and LRU are exactly the same .

5. Clock replacement algorithm (NRU algorithm has not been used recently)

  • The performance of the LRU algorithm is close to OPT, but it is difficult to implement and expensive;
  • The FIFO algorithm is simple to implement, but the performance is poor. Therefore, the designers of the operating system have tried many algorithms, trying to approach the performance of the LRU with a relatively small overhead. Such algorithms are all variants of the CLOCK algorithm.
  • Simple CLOCK algorithm
    • Associate an additional bit to each frame, called the use bit. When a page is loaded into the main memory for the first time, the use bit of the frame is set to 1; when the page is subsequently accessed, its use bit is also set to 1.
    • For the page replacement algorithm, the set of candidate frames for replacement is regarded as a circular buffer, and there is a pointer associated with it. When a page is replaced, the pointer is set to point to the next frame in the buffer. When a page needs to be replaced, the operating system scans the buffer to find a frame where the use bit is set to 0.
    • Whenever it encounters a frame with a use bit of 1, the operating system resets the bit to 0; if at the beginning of this process, the use bits of all frames in the buffer are 0, then the first encountered Replacement of a frame; if the used bits of all frames are 1, the pointer circulates in the buffer for a complete cycle, sets all used bits to 0, and stays at the original position to replace the page in the frame.
    • Since the algorithm checks cyclically case where each page, it is called CLOCK algorithm , yet not recently used (Not Recently Used, NRU) algorithm.
      Insert picture description here
  • Improved Clock algorithm
    • The performance of the CLOCK algorithm is closer to that of the LRU, and by increasing the number of bits used, the CLOCK algorithm can be made more efficient. Add a modified bit on the basis of the used bit to get an improved CLOCK replacement algorithm. In this way, each frame is in one of the following four situations:
      • It has not been visited recently and has not been modified (u=0, m=0).
      • Recently visited, but not modified (u=1, m=0).
      • It has not been visited recently, but has been modified (u=0, m=1).
      • Recently visited and modified (u=1, m=1).
    • The algorithm performs the following steps:
      • Starting from the current position of the pointer, scan the frame buffer. During this scan, no changes are made to the used bits. Select the first frame encountered (u=0, m=0) for replacement.
      • If step 1) fails, scan again to find (u=0, m=1) frames. The first such frame encountered is selected for replacement. In this scanning process, for each skipped frame, set its use bit to 0.
      • If step 2) fails, the pointer will return to its original position, and the used bits of all frames in the set are 0. Repeat step 1, and if necessary, repeat step 2. This will find a frame for replacement.
    • The improved CLOCK algorithm is superior to the simple CLOCK algorithm in that pages that have not changed are preferred when replacing. Since modified pages must be written back before being replaced, this saves time.

Six, page buffering algorithm (PBA)

Seven, the effective time to access the memory

Guess you like

Origin blog.csdn.net/pary__for/article/details/114538839