Page replacement algorithm

Source: Internet
Author: User

Why a page replacement algorithm is required

The operating system places only "common" data and code in the application in physical memory, while infrequently used data and code are placed on storage media such as the hard disk. If your application accesses "common" data and code, the operating system is already in memory and there is no problem. But when an application accesses data or code that it believes should be in memory, a fault is generated if the data or code is not in memory.

If a page break occurs, you need to transfer the required pages from disk into memory. If the memory does not have extra space, you will need to select a page in the existing page to replace it. With different page substitution algorithms, the order of page changes will vary. If the selected page is a page that is soon to be visited, then the system will be very open again resulting in a missing pages interrupt, because the disk access speed is far from memory access speed, the cost of page break is very large. Therefore, choosing which page to displace is not a matter of casual, but a requirement.

Page replacement algorithm
  • Optimal (Optimal) page replacement algorithm
    A theoretical algorithm proposed by Belady in 1966. The opt-out page is the one that will never be used in the future, or the page that is no longer being visited in the longest coming time. With the best permutation algorithm, it is usually guaranteed to get the lowest missing pages rate. However, because the operating system can not predict how many pages an application accesses during execution, which page is no longer accessible for the longest time in the future, the algorithm cannot actually be implemented, but it is possible to evaluate other page substitution algorithms as the upper limit.

  • FIFO (First in FIFO) page replacement algorithm
    The algorithm always eliminates the first pages that go into memory, that is, the pages that have the longest dwell time in memory are selected for elimination. Simply link a page that has been paged into memory during the execution of an application into a queue in order, with the queue header pointing to the longest-staying page in memory, and the end of the queue pointing to the page that was recently paged into memory. When you need to retire a page, it's easy to find a page that needs to be retired from the queue header. The FIFO algorithm only works well when the application accesses the address space in a linear order, otherwise it is inefficient. Because the pages that are often visited often stay in memory for the longest time, they have to be displaced because they become "old". Another drawback of the FIFO algorithm is that it has an anomaly (belady phenomenon) that increases the number of page faults in the case of placing page frames on pages.

  • Two chance (Second chance) page replacement algorithm
    In order to overcome the drawback of FIFO algorithm, people have improved it. This algorithm sets an access bit in a page table entry (PTE) to indicate whether the page for this page table item is currently accessed. When the page is accessed, the MMU hardware in the CPU will access the location "1". When you need to find a page to retire, for the most "old" page, the operating system to check its access bit. If the access bit is 0, it means that the page is old and useless and should be eliminated immediately; If the access bit is 1, it means that the page has been visited, so give it another chance. Specifically, the access bit is zeroed, then the page is placed at the end of the queue, and modify its loading time, as if it had just entered the system, and then continue to search down. The essence of the two-chance algorithm is to look for a page that is older and has not been visited since the last time the fault was found. If all the pages have been accessed, it degrades to a purely FIFO algorithm.

  • Clock algorithm
    in order to improve the disadvantage of the second chance algorithm, the pioneers proposed a clock algorithm. The core idea of the clock algorithm is to arrange the page into a clock shape, which has a needle arm, and each time we need to change the page, we start the inspection from the page indicated by the pin arm. If the access bit of the current page is 0, that is, from the last check to this time, the page has not been accessed, replacing the page. Conversely, the access bit is zeroed and the pointer is moved clockwise to the next page. Repeat these steps until you find a page that accesses a bit of 0.
    For example, one of the clocks shown, the page that the pointer points to is F, so the first page to be considered for substitution is f. If the access bit for page F is 0,f will be replaced. If the access bit of F is 1, the access bit of f is zeroed and the pointer moves to page G. On the surface, it is similar to the second chance algorithm, which is to change the access bit to 0, and vice versa. However, it differs from the second chance algorithm in several ways:

    (1) Their data structure is different, the second chance uses the linked list, and the clock algorithm uses an index (an integer pointer). This way, it uses a different memory space.
    (2) The second chance requires the use of additional memory, and the clock algorithm can use the page table directly. The advantage of using a page table is that there is no additional space, and the greater benefit is that the page's access bits are automatically zeroed out periodically, which makes the time-resolved granularity of the clock algorithm higher than the second chance algorithm, resulting in better page substitution. The essence of the
    Clock algorithm is the second chance, and its disadvantage is the same as the second chance algorithm: too fair, do not take into account the different frequency of page calls, it is possible to swap out the page should not or cannot be swapped out, may also cause an infinite loop.

  • NRU (not recently used) algorithm
    As the name implies, NRU is choosing a page that has not been visited in the most recent time, which is based on the time domain of program Access. Because according to the temporal and spatial principle, a page that has not been visited recently is not likely to be accessed in the subsequent time, and NRU is implemented by using the access and modification bits of the page.
    Each page has an access bit and a modifier bit, and the access bit is set to 1 when reading and writing to the page. The modifier bit is set to 1 when the process reads and writes to the page. Depending on the status of the two bits, the page can be categorized into the following four types of pages: 1, 2, 3, 4.
      
    With this classification, the NRU algorithm looks for the pages that can be replaced in the order of the four categories of pages. If all pages are accessed and modified, then only one page can be replaced, so the NRU algorithm will always end.
    Of course, this classification is more general, in the same type of page, we have no way to tell which type of access is more recent. That is, in some cases, we may not replace a page that was not recently used.

  • LRU (least recently used) algorithm
    Compared with the NRU algorithm, the LRU algorithm not only considers whether it has been used recently, but also considers the frequency recently used. Here is the prediction of the future based on past data: If a page is accessed at a low frequency, it will probably not be available later.
    The implementation of the LRU algorithm must somehow record the number of times each page has been accessed, which is a considerable amount of work. The simplest way is to add a count field to the record entry in the page table, a page is accessed once, and the value of this counter increases by 1. So, when you need to replace a page, you just need to find the lowest count field value of the page replacement, which is the least recently used page. Another simple way to do this is to link all the pages with a linked list, the most recently used page in the list header, which has not been used recently at the end of the list. The linked list is updated every time the page is accessed so that it remains the most recently used page in the linked list header.
    Although the LRU algorithm is good, the implementation cost is high (it is necessary to distinguish which page in different pages is least recently used), and the time cost is large (every time the page access occurs, you need to update the record). Therefore, the general commercial operating system has not adopted the LRU page update algorithm.

  • Working Set algorithm
    because it is not possible to accurately determine that the page is least recently used, then simply do not spend this effort, only maintain a small amount of information so that we choose the replacement page is not likely to be used immediately and will be the page. This small amount of information is the working set information. The
    Working Set concept derives from the temporal and spatial limitations of program Access-that is, within a period of time, the pages that the program accesses are confined to a set of page collections. For example, the most recent K-access occurred on a single m page, so M is the working set with the parameter K. We use W (k,t) to indicate the number of pages involved in K-visits at time t.
    Obviously, as K grows, the value of W (k,t) increases, but when K grows to a certain value, the value of W (k,t) grows extremely slow or nearly stagnant, and remains stable for some time, as shown in the following:

    As can be seen, If the number of pages in a program is equal to or exceeds the working set size, the program can have no page breaks for a period of time. If the number of pages in memory is less than the working set, the frequency of page breaks will increase and even memory jitter occur.
    Therefore, the goal of the work algorithm is to maintain the current working set of pages in physical memory. Each time the page is replaced, look for a replacement page that is not part of the current working set. This way, we only need to separate the page into two categories when we look at the page: the current working set of pages and the outer page of the current working set. So, just find a page that flies to the current working set and replace it. The advantages of the
    Working set algorithm: Simple to implement, you only need to add a virtual time domain to each record in the page table. Moreover, this time domain does not have to be updated every time the visit occurs, but when the page needs to be changed, the page replacement algorithm modifies it, so the time cost is not very large.
    Disadvantages of the working Set algorithm: Each time a page is scanned for replacement, it may be necessary to scan the entire page table. However, not all pages are in memory, so a large part of the scanning process will be useless. In addition, because its data structure is linear, it will cause each time to be scanned in the same order, it seems unfair.

  • Working Set clock algorithm
    In view of the shortcomings of the working set algorithm, the pioneers combined the working set algorithm with the clock algorithm to design the working Set clock algorithm, that is, the principle of working set algorithm, but the scanning order of the page is organized in the form of clocks. This way, each time you need to replace a page, you start scanning from the page that the pointer is pointing to to achieve a more fair state. Moreover, the pages organized by the clock are only pages in memory, and the pages outside the memory are not placed in the clock circle, thus improving the efficiency of the implementation.
    Due to its time and space advantages, the working Set clock algorithm is adopted by most commercial operating systems.

Reference

[1] https://chyyuu.gitbooks.io/ucorebook/content/zh/chapter-3/swap_algors.html
[2] Http://www.cnblogs.com/edisonchou/p/5094066.html

Page replacement algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.