Page replacement algorithm

Source: Internet
Author: User

When the process runs, if the page it accesses is not in memory and needs to call it in, but there is no free space in memory, it is necessary to pull out a page of program or data from memory and feed into the swap area of the disk.

The algorithm that chooses to bring up the page is called the page replacement algorithm. A good page replacement algorithm should have a lower frequency of page changes, that is, should not be accessed in the future or after a long period of time will not revisit the page first.

The following four kinds of common permutation algorithms are available.

1. Best replacement algorithm (OPT)

The best (Optimal, opt) replacement algorithm chooses the obsolete pages that will never be used in the future, or pages that are no longer accessed for the longest time, guaranteeing the lowest missing page rate. However, the algorithm cannot be implemented because people are currently unable to predict which of the thousands of pages in memory will be no longer accessible for the longest time in the future.

The best permutation algorithm can be used to evaluate other algorithms. Assume that the system allocates three physical blocks for a process, and consider having the following page number reference string:
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1

When the process runs, it first loads 7, 0, and 13 pages into memory. The process to access page 2 o'clock, resulting in a missing page break, according to the best permutation algorithm, select the 18th access is required to dial 7 to be eliminated. Then, visit page 0 o'clock, because it is already in memory so there is no need to create a fault. Visit Page 3 o'clock will also be based on the best replacement algorithm to eliminate page 1 ... And so on, 3-26 is shown. You can see what happens when the best permutation algorithm is used.

As you can see, the number of page breaks is 9 and the number of pages displaced is 6.

Visit page 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Physical Block 1 7 7 7 2 2 2 2 2 7
Physical Block 2 0 0 0 0 4 0 0 0
Physical Block 3 1 1 3 3 3 1 1
Pages No
Figure 3-26 Displacement Figure 2 when using the best permutation algorithm. First in, Out (FIFO) page replacement algorithm

Prioritize the first pages that go into memory, that is, the pages that have been in memory for the longest time. The algorithm is simple, simply put the paged into the memory of the page according to the order to link to the queue, set a pointer to the first page. However, the algorithm does not adapt to the actual operation of the process, because in the process, some pages are often accessed.

Visit page 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Physical Block 1 7 7 7 2 2 2 4 4 4 0 0 0 7 7 7
Physical Block 2 0 0 0 3 3 3 2 2 2 1 1 1 0 0
Physical Block 3 1 1 1 0 0 0 3 3 3 2 2 2 1
Pages No
Figure 3-27 Displacement Map when using FIFO permutation algorithm


Here still use the above example, to use FIFO algorithm for page replacement. Process access page 2 o'clock, the first to enter the memory of page 7 out. Then visit Page 3 o'clock, and then 2, 0, 1 in the first page into memory to swap out. As can be seen from Figure 3-27, the FIFO algorithm is used to make 12 page permutations, which is just one times more than the optimal permutation algorithm.

The FIFO algorithm also generates an anomaly that increases the number of physical blocks allocated and does not decrement the count of page faults, which was discovered by Belady in 1969, and is therefore known as the Belady exception, as shown in 3-28. Only the FIFO algorithm may have belady exceptions, and the LRU and opt algorithms will never have belady exceptions.

Visit page 1 2 3 4 1 2 5 1 2 3 4 5
Physical Block 1 1 1 1 4 4 4 5 , 5 ' 5
Physical Block 2 2 2 2 1 1 1 3 3
Physical Block 3 3 3 3 2 2 2 4
Pages No
1 1 1 5 5 5 5 4 4
Physical block 2 2 2 2 1 1 1 1 5
Physical block 3 3 3 3 2 2 2 2
Physical block 4 4 4 4 3 3 3
Pages No
Figure 3-28 Belady exception 3. Most recent unused (LRU) permutation algorithm

Select pages that have not been visited recently for the longest time, and it is considered that pages that have not been visited in the past for a period of time may not be accessed in the near future. The algorithm sets an Access field for each page to record the time that the page has gone through since it was last accessed, and to retire the page by selecting the largest value in the existing page.

In the above example, the LRU algorithm is used for page substitution, which is shown in 3-29. When a process accesses page 2 for the first time, it replaces the most recently visited page 7. Then visit Page 3 o'clock and swap out the last unused page 1.

Visit page 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Physical Block 1 7 7 7 2 2 4 4 4 0 1 1 1
Physical Block 2 0 0 0 0 0 0 3 3 3 0 0
Physical Block 3 1 1 3 3 2 2 2 2 2 7
Pages No
Figure 3-29 The displacement map of the LRU page substitution algorithm


In Figure 3-29, the first 5 permutations are the same as the best permutation algorithm, but the two algorithms are not necessarily connected. In fact, the LRU algorithm is "forward-looking" according to the previous situation of each page, and the best permutation algorithm is "backwards" depending on the usage of each page.

LRU performs better, but requires hardware support for registers and stacks. LRU is the algorithm for the Stack class. It is theoretically possible to prove that the stack class algorithm cannot have Belady exceptions. FIFO algorithm is based on queue implementation, not stack class algorithm.

4. Clock replacement algorithm

The performance of LRU algorithm is close to opt, but it is difficult and expensive to implement, FIFO algorithm is simple, but performance is poor. So the operating system designers tried a lot of algorithms, trying to use a relatively small cost close to LRU performance, such algorithms are the clock algorithm variant.

The simple clock algorithm is to associate an additional bit to each frame, called using bits. When a page is first loaded into main memory, the use bit of the frame is set to 1, and when the page is subsequently accessed, its usage bit is also placed at 1. For a page replacement algorithm, the set of candidate frames for substitution is treated as a loop buffer, and a pointer is 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 that uses bits to be set to 0. Whenever a frame with a bit of 1 is encountered, the operating system resets the bit to 0, and if at the beginning of the process, all frames in the buffer are used with a bit of 0, the first frame to be replaced is selected, and if all frames have a bit of 1, the pointer loops through the buffer for a full week, with all the used bits set to 0 and stays in the original position, replacing the page in that frame. As the algorithm loops through the situation of each page, it is called the clock algorithm, also known as the recently unused (not recently used, NRU) algorithm.

The performance of the clock algorithm is closer to LRU, which makes the clock algorithm more efficient by increasing the number of bits used. An improved clock replacement algorithm is obtained based on the use of a bit and an additional modifier bit. In this way, each frame is in one of the following four situations:

    1. has not been accessed recently and has not been modified (u=0, m=0).
    2. Recently accessed, but not modified (u=1, m=0).
    3. Recently not accessed, but was modified (u=0, m=1).
    4. Recently accessed, modified (u=1, m=1).


The algorithm performs the following steps:

    1. Scans the frame buffer, starting at the current position of the pointer. During this scan, no changes are made to the use of bits. Select the first frame encountered (U=0, m=0) to replace.
    2. If the 1th step fails, then rescan, look for frames (u=0, m=1). Select the first such frame encountered for substitution. During this scan, set the usage bit to 0 for each skipped frame.
    3. If the 2nd step fails, the pointer goes back to its original position, and all frames in the collection have a use bit of 0. Repeat the 1th step, and repeat steps 2nd if necessary. This will allow you to find the frames for replacement.


The improved clock algorithm is superior to the simple clock algorithm in that it prefers a page that does not change when it is replaced. This saves time because the modified page must be written back before it is replaced.

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.