Operating System Notes-Chapter 7 Virtual Memory Management Algorithm

Page replacement algorithm


Algorithm for partial pages, for a running program.

1. Functions and goals

Page fault interruption : The common occurrence of page fault interruption is: there is a process abcde in the current memory, and now needs to execute f, then a certain page in abcde needs to be moved out.

Function : When a page fault interrupt occurs and a new page needs to be loaded and the memory is full, select which page in the memory is replaced.

Goal : Minimize the number of page swaps in and out (that is, the number of page fault interruptions). Specifically, swapping out pages that are no longer used in the future or that are rarely used in the short term can usually only be predicted based on past statistical data under the guidance of the principle of locality.

Page locking (frame locking) : Used to describe critical parts of the operating system or time-critical application processes that must be resident in memory. The implementation method is: add the lock flag bit (lock bit) in the page table

2. Experimental settings and evaluation methods

  • Record a track of page access by a process
    • Example: (virtual) address tracking (page number, displacement)...
    • Generate page trajectory
  • Simulate the behavior of a page replacement and record the number of page missing
    • Fewer missing, better performance

3. Partial page replacement algorithm

Optimal page replacement algorithm (OPT, optimal)
  • The basic idea: When a page fault interrupt occurs, for each logical page stored in the memory, calculate how long it needs to wait for the next access. Choose the one with the longest waiting time as the page to be replaced.
  • This is just an ideal situation and cannot be achieved in an actual system, because the operating system cannot know how long it takes to wait for each page to be accessed.
  • Can be used as a basis for performance evaluation of other algorithms (run a program on a simulator and record each page access. The optimal algorithm can be used in the second run)
First-in-first-out algorithm (FIFO)
  • The basic idea: select the page with the longest resident time in memory and eliminate it. Specifically, the system maintains a linked list that records all logical pages located in memory. From the arrangement order of the linked list, the residence time of the first page of the chain is the longest, and the residence time of the page at the end of the chain is the shortest. When a page fault occurs, the first page of the chain is eliminated, and the new page is added to the end of the linked list.
  • The performance is poor, and the page that is called is likely to be a page that is frequently accessed, and there is a Belady phenomenon, and the FIFO algorithm is rarely used alone.
Least Recently Used Algorithm (LRU, Least Recently Used)
  • Basic idea: When a page fault interruption occurs, select the page that has not been used the longest and eliminate it.

  • Similar to the optimal page replacement algorithm, the page to be replaced is also inferred based on past records. It is an approximation of the optimal page replacement algorithm. It is based on the principle of program locality, that is, if some pages are frequently accessed in the last short period of time (the last few instructions), then a short period of time in the future Inside, they may be accessed mundanely again. On the other hand, if some pages have not been accessed for a long time in the past, they may not be accessed for a long time in the future.

  • The LRU algorithm needs to record the order of use time of each page.

  • The overhead is relatively large.

  • Two possible implementations:

    • The system maintains a linked list of pages: the most recently used page is used as the first node, and the least used page is used as the last node. Every time the memory is accessed, the corresponding page is found, removed from the linked list, and moved to the top of the linked list. Every time a page fault interrupt occurs, the page at the end of the table is eliminated.
    • Set up an active page stack: When accessing a page, push the page number to the top of the stack, and then check whether there is the same page number as this page in the stack, and extract it if there is. When a page needs to be eliminated, the page at the bottom of the stack is always selected, that is, the one that has not been used the longest.
Clock page replacement algorithm (Clock, second chance algorithm)
  • Clock page replacement algorithm: an approximation of LRU, an improvement to FIFO
  • The basic idea:
    • Need to use the access bit in the page table entry. When a page is loaded into the memory, the bit is initialized to 0. Then if the page is accessed (read/write), the position is changed to 1
    • Organize the pages into a circular linked list (similar to a clock surface), and point the pointer to the oldest page (the most advanced one)
    • When a page fault interrupt occurs, the oldest page pointed to by the pointer is inspected, and if its access bit is 0, it is immediately eliminated; if the accessed page is 1, the position is set to 0, and then the pointer moves one space down. Continue to do so, knowing to find the eliminated page, and then move the pointer to its next grid.
  • Modify the Clock algorithm so that dirty pages are always retained in a clock head scan
    • Dirty pages are represented by dirty bits. When the page is written, it is set to 1, and when only read is performed, it is kept at 0.
    • If only the read operation is performed, then the storage and removal do not need to be processed on the hard disk, which is more trouble-free and less expensive
    • When a page fault interrupt occurs, check the oldest page pointed to by the pointer. If its access bit is 00, it will be eliminated immediately; if the accessed page is 11/10/01, set the used bit to 0 first, and then the pointer Move down one space. Go on like this, knowing to find the page that was eliminated 00, and then move the pointer to its next grid.
  • The conversion of used bit and dirty bit is done by hardware
Least Frequently Used (LFU, Least Frequently Used)
  • Basic idea: When a page fault interruption occurs, select the page with the least number of visits and eliminate it
  • Implementation method: set an access counter for each page. Whenever a page is accessed, the access counter of the page is incremented by 1. In the event of a page fault interrupt, the page with the smallest count value is eliminated
  • The difference between LRU and LFU: How long does the LRU have not visited during the investigation; LFU examines the number and frequency of visits
  • Disadvantages: only frequency information is considered, not time information
Belady phenomenon
  • When using the FIFO algorithm, sometimes the number of allocated physical pages increases, and the page fault rate increases instead.
  • Reason: The replacement feature of the FIFO algorithm is inconsistent with the dynamic feature of the process accessing memory, and is inconsistent with the goal of the replacement algorithm (that is, to replace less used pages), so the pages replaced by it are not necessarily the process that will not be accessed. of
  • You can learn more about the stack algorithm
Comparison of LRU, FIFO and Clock
  • Both the LRU algorithm and the FIFO algorithm are essentially first-in-first-out ideas, but LRU is based on the most recent access time of the page to sort, so it is necessary to dynamically adjust the order of each page every time the page is accessed (there are The last access time of a page has changed); and FIFO is sorted according to the time when the page enters the memory. This time is fixed, so the sequence of pages is fixed. If a page has not been accessed after entering the memory, then its most recent access time is the time it entered the memory. If all the pages that enter the memory have not been accessed, then the LRU degenerates into a FIFO.
  • LRU performance is better, but the system overhead is high; FIFO algorithm overhead is small, but belady phenomenon may occur. Therefore, Clock is a compromise choice. At each page visit, it does not need to dynamically adjust the order of the page in the linked list, but just make a mark, and then wait until a page fault interrupt occurs, and then move it to the end of the linked list. For those pages in the memory that have not been accessed, the Clock algorithm performs as well as the LRU algorithm; and for those pages that are accessed, the clock algorithm cannot remember their exact locations like LRU.

Global page replacement algorithm


For multiple running programs.

Because the process's demand for physical page frames is dynamically variable , assigning a fixed physical page frame to each process limits flexibility. Therefore, the size of the physical page frame can be dynamically allocated according to the running stage of the program, that is, the global page replacement algorithm.

1. Working set model

  • The various page replacement algorithms introduced above are based on a premise: the principle of program locality . If the principle of program locality does not hold, there is no difference in the effects of various page replacement algorithms. If the principle of locality is valid, how to prove its existence and how to analyze it quantitatively? The working set model.
  • Working set: A collection of logical pages currently in use by a process. It can be represented by a binary function W(t, deita)
    • t represents the current execution time
    • deita is called the working-set window, which is the time window for a fixed-length page access
    • W (t, deita) = the set of all pages in the deita time window before the current time t (the set is constantly changing with the change of t)
    • |W (t, deita) | refers to the size of the working set, that is, the number of pages
  • Changes in the size of the working set: After the process starts to execute, a more stable working set is gradually established as new pages are accessed. When the location of the local area of ​​memory access is approximately stable, the size of the working set is approximately stable; when the location of the local area changes, the working set rapidly expands and contracts and transitions to the next stable value.
  • **Resident Set:** Refers to the set of pages where the process actually resides in the memory at the current moment
    • The working set is an inherent property of the process during its operation, and the resident set depends on the number of physical pages allocated to the process by the system and the page replacement algorithm used
    • If the entire working set of a process is in memory, that is, the working set is a subset of the resident set, then the process will proceed smoothly without causing too many page faults (until the working set changes drastically, thus Transition to another state)
    • When the size of the process resident set reaches a certain number, and then allocate more physical pages to it, the page fault rate will not drop significantly

2. Working set page replacement algorithm

  • The basic idea: discard pages that are not in the working set window, not necessarily when an interruption occurs. When the number of pages is greater than the size of the working set window, even if it is less than the size of the resident set, they will be discarded.
  • The page placed in the physical memory depends on whether the page is in the working set window, and the number of page replacements can be reduced at the overall system level.

3. Page fault rate replacement algorithm

  • The size of the resident set in the working set page replacement algorithm is fixed
  • Variable allocation strategy: The size of the resident set is variable. For example: when each process first runs, it first allocates a certain number of physical pages according to the size of the program, and then dynamically adjusts the size of the resident set while the process is running.
    • A global page replacement method can be used. When a page fault interrupt occurs, the replaced page can be the physical page used in competition among other processes and the concurrent processes.
    • Advantages and disadvantages: better performance, but increased system overhead
    • Specific implementation: The page fault frequency algorithm (PFF, page fault frequency) can be used to dynamically adjust the size of the resident set
  • Page fault rate: means "number of page faults/number of memory accesses" or "reciprocal of the average time interval of page faults". Influencing factors:
    • Page replacement algorithm
    • The number of physical pages allocated to the process
    • The size of the page itself
    • Programming method
  • If the page fault rate of the running program is too high, more physical pages are allocated by increasing the working set ; if the page fault rate of the running program is too low, the number of physical pages is reduced by reducing the working set . Try to keep the page fault rate of each program running in a reasonable range.
  • Algorithm: Keep track of the probability of missing occurrences. Set the threshold T, record the time tn from the last page missing, and compare |tn-t(n-1)| with T. If greater than the threshold, remove pages not referenced in (t(n-1),tn) in the working set; less than or equal to the threshold, only add missing pages to the working set

4. Thrashing

  • If the physical pages allocated to a process are too few to include the entire working set, that is, the resident set is a subset of the working set, then the process will cause a lot of page fault interruptions, and frequent replacement between memory and external memory is required Page, so that the process is running very slowly, this state is called "jitter".
  • The reason for jitter: As the number of processes that reside in memory increases, the physical pages allocated to each process continue to decrease, and the page fault rate continues to rise. Therefore, the OS has to select an appropriate number of processes and the number of frames required by the processes in order to achieve a balance between the concurrency level and the page fault rate.
  • The jitter problem may be improved by the local page replacement: adjust the MPL so that the mean time between page faults MTBF (mean time between page faults) = page fault service time PFST (page fault service time)

Guess you like

Origin blog.csdn.net/MaoziYa/article/details/106187730