Operating system principle (ii)--memory management of the page replacement algorithm

Source: Internet
Author: User

Page replacement algorithm

1. General statement

In order to improve memory utilization, solve the problem of memory shortage, more reasonable use of memory, people create a paged memory abstraction . At the same time, there is a concept of virtual memory , refers to the memory of the temporarily unnecessary to write to the hard disk, it appears that the hard disk expansion of memory capacity, so called "virtual" memory. With virtual memory, applications can use larger memory space than actual physical memory. It can be thought that this larger memory space is on the hard disk, only to be written to the real memory when a certain part needs to be used, and then written back to the hard disk when it is no longer used. Paging memory management divides physical memory into small chunks of equal size, usually 1K, 2K, 4K, and so on, called page frames , and logical memory (memory that is expanded using virtual memory technology, which can be thought to be on a hard disk) is also divided into small chunks, called pages ; The size of the page and page frame must be the same, it is the smallest unit written to real memory and writeback hard disk.

Introduce several other concepts:

Use bits: Each page frame has a usage bit that records whether this page frame is being used.

Modify bit (dirty bit): Each page frame has a dirty bit to record whether this page frame has been changed. When real memory is paged out, the changed page frame is written back to the hard disk, and the page frame that has not been changed is thrown away, because a copy of this page frame on the hard disk is still valid.

Logical address: The address of the expanded memory using virtual memory technology.

Physical Address: The address of the real memory.

Of course, the process is loaded into real memory to run, and the process code uses a logical address, so it involves an address translation problem, translating the logical address into a physical address. The logical address can be divided into two segments, the first half of which represents the page number, the second half represents the page offset, the physical address can be divided into two segments, the first half represents the page frame number, and the second half represents the offset in the page. The method of address translation is that the page number of the logical address corresponds to the page frame number of the physical address (correspondence is recorded in a table, such as the page number is 5, corresponding to the real memory of the page frame number is 3), the page offset varies (page and page frame size is the same).

2. Introduction to the page replacement algorithm

Suppose a memory page frame is already full at some point, but then you need to write a page to physical memory, you need to swap out a page that was originally in physical memory. If the permutation is inappropriate, it can cause the page that has just been swapped out to the hard disk to be written back to memory, slowing down the system. A page replacement algorithm is a method that considers which page to exchange for good performance.

2.1 Optimal algorithm (optimal algorithm)

First, the optimal algorithm, it needs to know the future to be used in the page, and then will not be used to swap out the page memory, if all the pages will be used, the need to use the longest page from now on, to try to make bad situation late. This approach allows the system to achieve the best performance, but it is not possible ... Because it is not currently possible to know which pages to use later. However, the optimal algorithm can be used as a measure of the excellent degree of other algorithms.

2.2 FIFO (First-in first-out, FIFO) algorithm

The idea of a FIFO algorithm is simply to displace the page that is currently in memory for the longest time. FIFO algorithms run fast and do not need to consider other factors, requiring little overhead. However, because the importance of the page is not considered, the FIFO algorithm can easily swap out the important pages for memory.

2.3 Second Chance (second chance) algorithm

To avoid FIFO algorithms swapping important pages out of memory, the Second chance algorithm provides some improvements. The Second chance algorithm checks its use bit before swapping the page out of memory (as described in the previous article), if it uses a bit of 1, proves that the page has been used recently, guesses that it may also be used, and does not displace it out of memory, but uses a position of 0 and then checks the next page. This page is swapped out of memory until a page is found with a bit of 0.

2.4 Clock algorithm (clocks rotation method)

In order to save the second chance algorithm one after another to check the cost of using BITS, the clock rotation method has been improved. The clock rotation method will make all the pages a circle, the center of the pointer to the next page to be replaced, the same check the use of bits before displacement, if the use of bit 1, the same position is used as 0, and then rotate the pointer, check the next page until a page is found to use a bit of 0, the page is replaced by memory. It is easy to understand why this algorithm is called the "clock" rotation method.

Icon:

At this point, page 2nd is the next page to be swapped out of memory, and if it finds its use bit 1, it will use position 0 to rotate the pointer clockwise to check page 1th.

2.5 LRU (least recently used) algorithm

To obtain the simulation of the optimal algorithm, an LRU algorithm is proposed. Since the current time after the need to use which pages are not known in advance, so that the current time to record the use of the page, the previous use of the page will be used later. At the time of displacement, the most recently used pages are swapped out of memory. The overhead of this method is relatively large.

Operating system principle (ii)--memory management of the 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.