The page replacement algorithm of the operating system

Source: Internet
Author: User

The following principle source: http://c.biancheng.net/cpp/html/2614.html

In the process of address mapping, if you find that the page you want to access is not in memory, a page fault is generated. When a page break occurs, if there is no free page in the operating system memory, the operating system must select one of the pages in memory to move it out of memory to make room for the page to be paged in. The rule used to choose which page to retire is called the page replacement algorithm.

1. Best permutation algorithm (OPT)(ideal replacement algorithm): Removes pages that are never needed from main memory, or pages that are not needed for the longest time if no such page exists. The selected eliminated page will be a page that is never used in the future, or is not accessed for the longest time, thus guaranteeing the lowest possible paging rate.

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

2. FIFO: The simplest algorithm for page substitution. The basic idea of this algorithm is: when the need to eliminate a page, always choose to reside in main memory for the longest page to retire, that is, the Advanced Master Save the page first eliminated. The reason for this is: the first to transfer into main memory page is no longer the most likely to be used.

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

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
Note: the "oldest" page in the memory page will be overwritten by the new page, rather than the "oldest" page first, then the new page is queued from the end of the team.

3. The most recent unused (LRU) algorithm : The basic idea of this algorithm is to speculate on future behavior based on the local principle and the past page access history of a job during execution. It believes that pages that have not been visited in the past for some time may not be visited in the near future. Therefore, the essence of this algorithm is: when the need to eliminate a page, always choose the most recent period of time the longest unused page to be eliminated.

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

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.

Examples:

* Using the LRU page replacement algorithm on 5 page boxes, when the page box is initially empty, the reference sequence is 0, 1, 7, 8, 6, 2, 3, 7, 2, 9, 8, 1, 0, 2, and the system will occur (C) page faults

A, B, C, one D, 8

Parsing: A 5-page box resides in memory:

Visit page 0 1 7 8 6 2 3 7 2 9 8 1 0 2
Page Box 1 0 0 0 0 0 2 2 2 2 2 2 2 2 2
Page Box 2 1 1 1 1 1 3 3 3 3 3 1 1 1
Page Box 3 7 7 7 7 7 7 7 7 7 7 0 0
Page Box 4 8 8 8 8 8 8 9 9 9 9 9
Page Box 5 6 6 6 6 6 6 8 8 8 8
is missing pages Y Y Y Y Y Y (page change) Y (page change) N N Y (page change) Y (page change) Y (page change) Y (page change) N

LRU is the algorithm of the Stack class, and the last page visited is placed at the top of the stack and can be answered by C.

Programming Ideas:

1, the structure member records the order of access, the change page when the times the largest one to replace.

struct LRU {int data;

int times;}; Record Access Order

struct queue{LRU *p; int front; int rear

}QE;

(1) When the queue is not full, add the newly visited page in turn and qe.p[i++].times++

(2) The queue is full of a, the newly accessed page is in the queue, the Times is set to 0, before the lru.times++ in front of it

b, the newly visited page is not in the queue, need to replace the times the largest page, and set a new page times=0, the other pages in the column times++

2, using the position in the queue to indicate the last access time (with a linear table involving a large number of elements moving, with the linked list better)

When the queue is not full, press in, and the queue is full, see if there are any in the column, if it exists, move it to the tail of the team, if it does not exist, delete the first page and add a new page at the end of the team.

C + + code implementation:

The page replacement algorithm of the operating system

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.