In-depth understanding of "page Break" and FIFO, LRU, opt of the three permutation algorithm

Source: Internet
Author: User

  Page Break (English: Page fault, aka hard error, hard interrupt, paging error, missing page, pages fault, etc.) refers to when the software tries to access a page that has been mapped in the virtual address space, but is not currently loaded in physical memory. Interrupts from the memory management unit of the CPU. Typically, the program that is used to handle this interruption is part of the operating system. If the operating system determines that the access is valid, the operating system attempts to transfer the related paging from the virtual memory file on the hard disk into memory. And if access is not allowed, then the operating system usually ends the associated process. Although it is called a "page missing" error, it is not necessarily an error in practice. And this mechanism is common and necessary for operating systems that use virtual memory to increase program free memory space, such as Microsoft Windows and various Unix-like systems. Microsoft uses the term "hard errors" (Windows Vista and above), "Hard Interrupts" (Windows 8 and above) to refer to "page misses" in the newer Windows Resource Monitor.   category   Soft soft page missing when the page is missing, the associated page has been loaded into memory, but there is no registration to the MMU. The operating system only needs to register the corresponding physical address of the relevant page in the MMU. One possibility of this happening is that a piece of physical memory is shared by two or more programs, and the operating system has loaded and registered the corresponding page for one of them, but no other program is registered. The second possibility is that the page has been removed from the working set of the CPU, but has not yet been swapped to disk. For example, a system that uses a secondary page cache, such as OpenVMS, may remove a page from the working set if the work sets are too large, but does not write to the hard disk or erase it (for example, the page has not been modified after it has been read out), but only into the free page table. Unless otherwise required, this page is assigned, otherwise the content of this page will not be modified. When the original program needs the data in the page again, if this page does not have been assigned, then the system only need to re-register the page in the MMU map.   Hard vs. soft page missing, hard page misses are cases where the page is not loaded into memory when the page is missing. At this point the operating system requires:
    1. Find a free page. or write another in-use page to disk (if it changed after the last write), and log off the record in the MMU
    2. Read the data into the selected page
    3. Register the page with the MMU
The performance loss caused by hard page misses is significant. Taking a 7200rpm mainstream mechanical hard drive for example, its average seek time is 8.5 milliseconds, and reading into memory takes 0.05 milliseconds. In contrast, the access latency for DDR3 memory is typically between dozens of and 100 nanoseconds, and the performance gap may reach 80,000 to 220,000 times times. In addition, some operating systems can improve performance by delaying part of the program until it needs to be loaded into memory execution. This feature is also achieved by capturing hard page misses. When a hard page is missing too often, it is said that a system bump occurs. Invalid page missing occurs when the program accesses a virtual address that is not present in the virtual address space. In general, this is a software problem, but it does not rule out hardware, such as a memory failure that could damage a correct pointer. Specific actions are related to the operating system used, such as Windows using an exception mechanism to report to the program, while UNIX-like systems use signaling mechanisms. If the program does not handle the problem, the operating system performs the default processing, typically by dumping memory, terminating the associated program, and then reporting to the user. Interrupt
Refers to the computer in the process of executing the program, when abnormal conditions or special requests, the computer to stop the operation of the current program, to the abnormal situation or special requests to deal with, after the end of the process to return to the current interruption of the process, continue to implement the original procedure. Number of pages interrupted
Interrupts = number of physical blocks of the process x page substitution times. Sequence of broken pages
The sequence of events at which the fault occurs is as follows: 1) The hardware is trapped in the kernel and the program counters are stored in the kernel stack. Most machines store various state information for the current instruction in a special CPU register. 2) Start a assembler code routine to save the general register and other volatile information to avoid being compromised by the operating system. This routine invokes the operating system as a function. 3) When the operating system discovers a page break, try to find out which virtual pages are needed. Usually a hardware register contains this information, if not, the operating system must retrieve the program counter, take out this instruction, software analysis This instruction, see what it is doing when the page break. 4) Once you know the virtual address of a missing pages interrupt, the operating system checks that the address is valid and checks that access and protection are consistent. If it is not consistent, send a signal to the process or kill the process. If the address is valid and no protection error occurs, the system checks for a free page box. If there is no free page box, perform a page replacement algorithm to find a page to retire. 5) If the selected page box is "dirty", schedule the page to write back to the disk, and a context switch occurs, suspending the process that caused the fault of the pages, and letting the other process run until the end of the disk transfer. In any case, the page box is marked as busy to avoid being consumed by other processes for other reasons. 6) Once the page box is "clean" (either immediately or after the disk is written back), the operating system looks for the address of the desired page on disk and loads it through disk operation. After the page is loaded, the process that generated the fault is still suspended, and if there are other user processes that can run, select another user process to run. 7) When a disk outage occurs, it indicates that the page has been loaded, the page table has been updated to reflect its location, and the page box is marked as normal. 8) Restore the previous state of the fault of the missing pages, the program counter again point to this instruction. 9) Dispatched the process that caused the fault of the fault, the operating system returns the assembly language routine that called it. 10) This routine restores registers and other state information[1]

1. Missing pages Interrupt

In the request paging system, you can determine whether the page you want to access exists in memory by querying the status bit in the page table. Whenever the page you want to access is not in memory, it will cause a page break, and the operating system will be paged into memory based on the external memory address in the page table where it is missing in external memory.
The missing pages themselves are interrupts, which, like normal interrupts, require 4 processing steps:
1. Protecting the CPU site
2. Analyze the cause of the interruption
3. Go to the missing pages interrupt handler for processing
4. Restore the CPU site and continue execution
However, when a page break is interrupted, there is a special interruption of hardware due to the absence of the pages to be accessed and memory, so there is a difference from the usual interrupts:
1. Generate and process a fault signal during instruction execution
2. An instruction may cause multiple pages to be interrupted during execution
3. When a fault is returned, the execution of the command that generated the interrupt is executed, and the next instruction is executed when the general interrupt returns.

2. Page Replacement algorithm

In the process of running, if a page break occurs, and there is no free physical block in memory, in order to be able to load the missing page into memory, the system must choose from memory to the disk swap area. However, the page should be swapped out, you need to according to a certain page replacement algorithm (page replacement algorithm) to determine.

2.1 Best Replacement (Optimal, OPT) 2.1.1 Basic ideas

Replacements are no longer accessed, or pages that are visited at the latest in the future, with the lowest page break rate. However, the algorithm needs to be based on the use of the future industry, and when a process has not been completed, it is difficult to estimate which page is not used later or in the longest time after the page will be used. Therefore, the algorithm is not possible. But the algorithm still has significance as a standard for other algorithms that are bright and good.

2.1.2 Study

Using the fixed allocation of local displacement strategy, jiading system for a process in memory allocated 3 physical blocks, page access order is 2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2. Assume that the system does not adopt a pre-paging policy, that is, no pages have been called in advance. When the process runs, 2, 3, 13 pages are paged into memory at a time, and 3 page faults occur. When the first visit to page 5 o'clock, resulting in a 4th page fault, according to the OPT algorithm, the elimination of pages 1, because it will not be used in the future, the 5th page is interrupted, the elimination of pages 2, because it in 5, 3, 23 pages, is in the future the page will be accessed at the latest page. And so on
Note: The 4th interrupt will not be accessed at the end of the 1 culling, the last access to 3 is placed in the bottom memory block, the subsequent scheduling process, the last will not be accessed or the last access to the page is always placed in the bottom of the memory block. The memory block stores the first visited page from top to bottom.
The number of interrupts is 6, and the fault rate for the pages is 6/12*100% = 50%.

P: 2 3 2 1 5 2 4 5 3 2 5 2
M=3 2 2 2 2 2 5 5 3 5 5 2 2
3 3 3 5 3 3 5 4 2 5 5
1 3 2 4 4 3 4 4 4
F=5 Y Y Y Y Y Y
2.2 FIFO replacement algorithm (first in 2.2.1, FIFO) basic idea

Displace the page that is first paged into memory, which is the page that has the longest dwell time in memory. In order to enter the memory sequence into a queue, from the end of the team to enter, removed from the team first. However, the algorithm will eliminate frequently accessed pages, not adapt to the actual running of the process of the law, is rarely used.

2.2.2 Study

The OPT study is still an example.
The number of interrupts is 6, and the fault rate for the pages is 9/12*100% = 75%.

P: 2 3 2 1 5 2 4 5 3 2 5 2
M=3 2 3 3 1 5 2 4 4 3 3 5 2
2 2 3 1 5 2 2 4 4 3 5
2 3 1 5 5 2 2 4 3
F=9 Y Y Y Y Y Y Y Y
2.2.3 Belady Anomalies

In general, the more physical blocks allocated to the process, the fewer pages should be run, and when using FIFO, there may be an opposite case, the number of pages allocated 4 physical blocks is more than 3 physical blocks of missing pages!
For example: The process access order is 0, 2, 1, 3, 0, 2, 4, 0, 2, 1, 3, 4.
When m=3, the page faults are interrupted 9 times. The fault rate of the pages 9/12*100% = 75%.

P: 0 2 1 3 0 2 4 0 2 1 3 4
M=3 0 2 1 3 0 2 4 4 4 1 3 3
0 2 1 3 0 2 2 2 4 1 1
0 2 1 3 0 0 0 2 4 4
F=9 Y Y Y Y Y Y Y Y Y

When m=4, the page faults are interrupted 10 times. The fault rate of the pages 10/12*100% = 83.3%.

P: 0 2 1 3 0 2 4 0 2 1 3 4
M=4 0 2 1 3 3 3 4 0 2 1 3 4
0 2 1 1 1 3 4 0 2 1 3
0 2 2 2 1 3 4 0 2 1
0 0 0 2 1 3 4 0 2
f=10 Y Y Y Y Y Y Y Y Y Y
2.3 The most recent unused permutation algorithm (Least recently used, LRU) 2.3.1 Basic idea

Replace pages that have not been visited for the longest time since the most recent time. Depending on the program locality principle, pages that have just been visited may be accessed immediately, and pages that have not been visited for a longer period of time may not be accessed recently.
The LRU algorithm is applied to all kinds of programs, but the system should record and update the access history of each page at all times, and the cost is too large, so the LRU algorithm must have hardware support.

2.3.2 Study

The OPT study is still an example.
The number of interrupts is 6, and the fault rate for the pages is 7/12*100% = 58.3%.

P: 2 3 2 1 5 2 4 5 3 2 5 2
M=3 2 3 2 1 5 2 4 5 3 2 5 2
2 3 2 1 5 2 4 5 3 2 5
3 2 1 5 2 4 5 3 3
F=7 Y Y Y Y Y Y Y

Stack implementation LRU:
The system uses a special stack to hold the page number of each page in memory. Whenever one page is accessed, the page number of the page being accessed is removed from the stack and then pushed into the top of the stack. Therefore, the top of the stack is always the page number of the most recently visited page, and the bottom is always the most recent page number that has not been accessed. When a page break occurs, the pages corresponding to the bottom page number are always eliminated.
  

Reference
    1. Quiet, computer operating system principle, Wuhan University Press.
Directory

    • Missing pages Interrupt
    • Page replacement algorithm
      • 1 Best Replacement optimal OPT
        • 11 Basic Ideas
        • 12 examples
      • 2 FIFO permutation algorithm first
        • 21 Basic Ideas
        • 22 examples
        • Belady exception
      • 3 Most recently unused permutation algorithm least recently used LRU
        • 31 Basic Ideas
        • 32 examples
      • Reference
        • Directory

In-depth understanding of "page Break" and FIFO, LRU, opt of the three permutation 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.