Operating System Learning Notes (v) page replacement algorithm

Source: Internet
Author: User

The operating system manages the memory according to the page, and then calls the corresponding portion of the process into memory when needed. When a page break is generated, you will need to select one of the pages to write. If the page to be swapped out is modified in memory and becomes a "dirty" page, it needs to be written to disk first. The page substitution algorithm, is to choose the most suitable one page, makes the displacement the most efficient. Page permutation algorithm has a lot of simple introduction of several, focusing on the more important LRU and its implementation algorithm.

First, the optimal page replacement algorithm

Ideally, we'll mark the page and pick a page that will be used the furthest away. Of course, such an algorithm cannot be implemented because it is not certain when a page will be used.

Second, recently unused page replacement algorithm (NRU)

The system sets two flags for each page: when the page is accessed, the R bit is set and the M bit is set when the page (modify) is written. When a page break occurs, the OS checks all pages and divides them into four categories based on their current R and M bit values:

(1)! r&! M (2)! R&m (3) r&! M (4) r&m

The smaller the number of the class, the more preferentially swapped out. That is, in a recent clock tick, it is better to retire a page that has not been accessed but has been modified than to eliminate a page that is frequently used but "clean".

Third, first-out page replacement algorithm (FIFO) and its improvement

The idea of this algorithm and the queue is the same, the OS maintains a current in memory of all the pages of the linked list, the latest entry page in the tail, the longest in the head, whenever a page break occurs, replace the table header pages and add the new page into the end of the list.

The problem with this algorithm is obviously too "fair", without taking into account the actual frequency of page usage.

A reasonable improvement is called the Second chance algorithm. That is, to add an R bit to each page, each time starting from the list header, if R set, clear R bit and put the page node to the end of the list, if R is 0, then it is old and useless to replace.

Four, clock page replacement algorithm (clock)

This algorithm is just a model like a clock, which is actually a second chance algorithm for a ring-linked list, with the needle pointing to the oldest page. When the missing pages are interrupted, perform the same operation, including checking r bits, and so on.

V. Least recently used page replacement algorithm (LRU)

When a page break occurs, the replacement of the pages that have not been used for the longest time is called LRU (least recently used).

LRU is achievable and requires maintaining a linked list of all pages, and updating the list based on real-time usage is a big price.

Therefore, it is necessary to make some improvements to this algorithm, also can be said to be simplified. Each page is associated with a counter, each clock terminal, scanning all pages, the R bit of each page is added to the counter, so that the use of each page is roughly tracked. This method is called NFU (not frequently used, least common) algorithm.

There is still a problem, that is, a long time ago, with the most recent use of the same weight.

So, once again, the counter is shifted to the right by one bit at each tick, and the R bit is added to the top. This algorithm, called the Aging (aging) algorithm, increases the proportion of the most recently used.

The aging algorithm can only use a limited number of digits, so there may be some degree of accuracy loss.

VI. Working Set algorithm

In short, the working set is the collection of pages used in the most recent K-Times memory access. The original working set algorithm is also expensive, simplifying it: A collection of pages used in a previous NMS visit to the North Village.

So, at the time of implementation, you can give each page a timer. When the page needs to be replaced, compare with the actual time, R is 1, update to the current time, R is 0, the page outside the specified threshold value can be replaced.

Similarly, the algorithm can be improved with the idea of a clock.

Vii. page replacement algorithm used by Linux

Linux distinguishes four different pages: non-recyclable, exchangeable, synchronized, and disposable.

Non-recyclable: reserved and locked in-memory pages, as well as kernel stacks.

Exchangeable: Must be written back to the swap area or the paging disk partition before recycling.

Can be synchronized: if the dirty page, you must first write back.

Can be discarded: a page that can be recycled immediately.

Linux is not as simple as we discussed the algorithm before, when the page fault is generated when pages are recycled. Linux has a daemon kswapd that compares the high and low levels of each memory area to detect if there are enough free pages to use. Only one determined number of pages is recycled each time it is run. This threshold is limited to control the I/O pressure.

Every time the recovery is performed, it is easy to recycle and then difficult to handle. The recycled pages are added to the idle list.

The algorithm is an improved LRU algorithm that maintains two sets of tags: active/Inactive and whether referenced. The first scan clears the reference bit, and if the second run is determined to be referenced, it is promoted to a state that is unlikely to be reclaimed, otherwise the page is moved to a state that is more likely to be reclaimed.

Pages that are inactive lists are the best option to remove since the last check was not referenced. Referenced but inactive pages are also considered for recycling because some pages are accessed by daemons and may not be used for a long time.

In addition, memory management also has a daemon pdflush, which wakes up periodically, writes back to dirty pages, or is awakened by the kernel after the available memory drops to a certain level.

Operating System Learning Notes (v) 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.