Operating System Notes - Page Replacement Algorithm

Table of contents

1--page replacement algorithm

2--Partial page replacement algorithm

2-1-- Optimal Page Replacement Algorithm

2-2--First in first out algorithm

2-3--The least recently used algorithm

2-4--Clock page replacement algorithm

2-5--Second Chance Algorithm

2-6--Least commonly used algorithm

2-7--Belady phenomenon

2-8--Comparison of LRU, FIFO and Clock algorithms

3--Working set and resident set model

4--Global replacement algorithm

4-1--Working Set Page Replacement Algorithm

4-2--page replacement algorithm for page fault rate

4-3--jitter problem


1--page replacement algorithm

        When a page fault occurs and a new page needs to be loaded and the memory is full, select which physical page in the memory to be replaced according to the page replacement algorithm;

        Measure the pros and cons of the page replacement algorithm: minimize the number of pages swapped in and out (that is, the number of page fault interruptions); try to swap out pages that are no longer used in the future or that are less used in the short term;

2--Partial page replacement algorithm

2-1-- Optimal Page Replacement Algorithm

        When a page fault interrupt occurs, for each logical page stored in the memory, calculate the next access time, and select the page with the longest time as the replacement page;

        The optimal page replacement algorithm is an ideal situation, which cannot be realized in the actual system, because the operating system cannot calculate the time when each page is accessed next time;

2-2--First in first out algorithm

        The first-in-first-out algorithm (FIFO) selects the page that has the longest resident time in the memory for replacement; the operating system maintains a linked list that records all logical pages in the memory. The residence time of the page is the shortest; when a page break occurs, the first page of the chain will be eliminated, and a new page will be added to the tail of the chain;

        Generally speaking, the performance of the first-in-first-out algorithm is poor, and the page that is called out may be a page that is frequently accessed, and it will cause the Belady phenomenon (that is, lead to more subsequent page faults);

2-3--The least recently used algorithm

        The least recently used algorithm (Least Recently Used, LRU) will select the page that has not been used for the longest time to replace when a page fault occurs;

        The least recently used algorithm is an approximation to the optimal replacement algorithm. It is based on the locality principle of the program, that is, if some pages are frequently accessed in a short period of time, there is a high probability that they will be frequently accessed in a short period of time in the future. access; on the contrary, if some pages have not been accessed for a long time, they may not be accessed for a long time;

        The LRU algorithm needs to record the order of the usage time of each page, and its overhead is relatively large. There are two methods for specific implementation:

        ① The system maintains a linked list of pages, with the most recently used page as the first node, and the longest unused page as the last node; each time the memory is accessed, find the corresponding page, remove it from the current position of the page chat table, and then move it Go to the first position; when a page fault occurs, replace the end node of the page list;

        ② The system sets up an active page stack. When accessing a certain page, push the page number onto the top of the stack, and check whether there is a page number identical to this page in the stack, and if so, pull it out and place it on the top of the stack; , replace the page at the bottom of the stack;

2-4--Clock page replacement algorithm

        The Clock page replacement algorithm is an approximation of the LRU algorithm and an improvement on the FIFO algorithm. It needs to use the access bit in the page table entry. When a page is loaded into memory, the access bit is initialized to 0. When the page is accessed (read/ write) set the access location to 1;

        Organize each page into a circular linked list (similar to a clock face), and point the pointer to the most recent page; when a page fault occurs, first check the page pointed to by the pointer, and replace it if the access bit is 0; if access If the bit is 1, first set the access position to 0, and then move the pointer down one space until the page with the access bit of 0 is encountered for replacement;

2-5--Second Chance Algorithm

        The second chance method (Enhanced Clock algorithm) is an improvement to the clock page replacement algorithm. It has two bits similar to the access bits of the Clock algorithm, called used bit and dirty bit, corresponding to read and write; when a page It will be replaced when both the used bit and the dirty bit are 0, that is, for a  page whose used bit and dirty bit are both 1, it needs to be cycled at least twice before it can be replaced;

        When the page is written, the dirty bit and used bit will be set to 1; when the page is read, only the used bit will be set to 1; the second chance method can keep the frequently written pages as much as possible;

2-6--Least commonly used algorithm

        The least frequently used algorithm (Least Frequently Used, LFU) will select the page with the least number of access times to swap out when a page fault occurs. It sets an access counter for each page; when a page is accessed, the page's The access counter will increase by 1; when a page fault occurs, the page with the smallest count value will be replaced;

        The problem that may exist in the least commonly used algorithm is: a page is frequently used at the beginning of the process and is not used in subsequent programs, but its access count value is extremely large; one way to solve this problem is: regularly access The register of the counter is shifted one bit to the right to reduce the impact of time;

2-7--Belady phenomenon

        When using the FIFO algorithm, with the increase of allocated physical pages, sometimes there will be an abnormal phenomenon that the page fault rate will increase instead; normally, with the increase of allocated physical pages, the page fault rate should be reduced;

2-8--Comparison of LRU, FIFO and Clock algorithms

        Both the LRU algorithm and the FIFO algorithm are essentially first-in, first-out ideas. The LRU algorithm sorts pages based on the latest access time (the order of the pages is dynamically adjusted every time a page is accessed), while the FIFO algorithm sorts the pages when they enter the memory. Sorting, the sequence is fixed; when a page is not accessed after entering the memory, its latest access time is equal to the time of entering the memory; when all pages in the memory are not accessed, the LRU algorithm will degenerate into a FIFO algorithm;

        The performance of the LRU algorithm is better, but the overhead of the system is larger; the overhead of the FIFO algorithm is smaller, but the Belady phenomenon may occur; therefore, the compromise algorithm is the Clock algorithm;

3--Working set and resident set model

        The prerequisites for various page replacement algorithms to work are: the principle of locality of the program;

        ​​​​​​​​Working set: a collection of logical pages currently in use by a process;

        Resident set: the set of pages that the process actually resides in the memory at the current moment;

4--Global replacement algorithm

4-1--Working Set Page Replacement Algorithm

        Working set page replacement algorithm: With the execution of the program, the working set window is constantly moving; when a page is not in the working set window, the page will be swapped out from the memory;

        Not only when a page fault occurs, the working set page replacement algorithm will swap out the page to the external memory;

        The working set page replacement algorithm can effectively retain the most recently accessed pages, swap out the physical pages in memory in advance, and provide more memory space for other programs, so as to achieve the purpose of dynamically allocating memory space; but the working set page replacement algorithm will increase Page fault interrupt;

4-2--page replacement algorithm for page fault rate

        The working set window of the page replacement algorithm of the page fault rate is changed. When the page fault rate increases, the working set window will be increased, and more pages will be kept in memory (more pages are in the window, and the pages will not be Swap out); when the page fault rate decreases, it means that the number of physical pages provided is sufficient, and the working set window can be appropriately reduced;

        ​​​​​​​​Page fault rate = number of page faults/number of memory accesses

4-3--jitter problem

        When too few physical pages are allocated to a process, many page faults will occur in the process, and the operating system needs to frequently swap in and out replacement pages between the internal memory and the external memory, causing the execution of the program to stall and the process to run slowly. This state is called jitter;

 

Guess you like

Origin blog.csdn.net/weixin_43863869/article/details/130349252