During the address mapping process, if it is found in the page that the page to be accessed is not in the memory, a page fault interrupt will occur. When a page fault occurs, if there is no free page in the operating system's memory, the operating system must select a page in the memory and move it out of the memory to make room for the page to be transferred in. The rules used to select which pages to eliminate are called page replacement algorithms.
Common replacement algorithms
Optimal replacement algorithm (OPT)
This is an ideal page replacement algorithm, but in practice it is impossible to implement. The basic idea of ??this algorithm is: when a page fault occurs, some pages are in memory, one of which will be accessed soon (also including the page of the next instruction), while other pages may not be accessed until 10 or 100 Or it will be accessed after 1000 instructions. Each page can [1] be marked with the number of instructions to be executed before the page is accessed for the first time. The optimal page replacement algorithm simply states that the page with the largest markup should be replaced. The only problem with this algorithm is that it cannot be implemented. When a page fault occurs, the operating system has no way of knowing when each page will next be accessed. Although this algorithm is not possible to implement, the optimal page replacement algorithm can be used to measure and compare the performance of achievable algorithms.
First-in, first-out replacement algorithm (FIFO)
The simplest page replacement algorithm is the first-in, first-out (FIFO) method. The essence of this algorithm is to always choose the page that has stayed in the main memory the longest (that is, the oldest) to replace, that is, the page that enters the memory first and exits the memory first. The reason is: the earliest page transferred into memory is more likely to be no longer used than the page that was just transferred into memory. Create a FIFO queue to store all pages in memory. Replaced pages are always placed at the head of the queue. When a page is put into memory, it is inserted at the end of the queue.
This algorithm is ideal only when accessing the address space [1] in linear order, otherwise it is not efficient. Because those pages that are frequently accessed tend to stay in main memory the longest, and as a result they have to be replaced because they become "old".
Another disadvantage of FIFO is that it has an abnormal phenomenon, that is, when adding storage blocks, the page fault interrupt rate increases. Of course, the page direction that causes this anomaly is actually rare.
Most recently unused (LRU) algorithm
The main difference between the FIFO algorithm and the OPT algorithm is that the FIFO algorithm uses the length of time after the page enters the memory as a replacement The basis of the OPT algorithm is the time when the page will be used in the future. If the recent past is used as an approximation of the near future, then pages that have not been used for the longest period of time in the past can be replaced. Its essence is that when a page needs to be replaced, the page that has not been used for the longest time in the previous period of time is selected to replace it. This algorithm is called the least recently used algorithm (Least Recently Used, LRU).
The LRU algorithm is related to the last time each page was used. When a page must be replaced, the LRU algorithm selects the page that has been least used in the past period of time.
The LRU algorithm is a frequently used page replacement algorithm and is considered to be quite good, but there is a problem of how to implement it.
The LRU algorithm requires the support of actual hardware.
The problem is how to determine the order of the last use time. There are two feasible methods for this:
1. Counter.
The simplest case is to make each page table entry correspond to a usage time field and add a logical clock or counter to the CPU. This clock is incremented by 1 for each memory access. Whenever a page is accessed, the contents of the clock register are copied into the usage time field of the corresponding page table entry. This way we can always keep the "time" when each page was last visited. When replacing pages, select the page with the smallest time value. In doing so, not only must the page table be looked up, but also the time in the page table must be maintained when the page table changes (due to CPU scheduling), and the problem of clock value overflow must also be taken into consideration.
2. Stack.
Use a stack to retain page numbers. Whenever a page is accessed, it is taken out of the stack and placed on top of the stack. In this way, the most used page is always placed at the top of the stack, and the least used page is placed at the bottom of the stack. Since an item needs to be removed from the middle of the stack, a two-way chain with head and tail pointers is used to connect it. In the worst case, removing a page and placing it on top of the stack requires changing 6 pointers. There is an overhead for each modification, but the page that needs to be replaced can be obtained directly without searching, because the tail pointer points to the bottom of the stack, where the page to be replaced is.
Because the implementation of the LRU algorithm requires a large amount of hardware support, it also requires a certain amount of software overhead. So what is actually implemented is a simple and effective LRU approximation algorithm.
One LRU approximation algorithm is the Not Recently Used (NUR) algorithm.
It adds a reference bit to each entry in the storage block table, and the operating system periodically sets them to 0. When a page is accessed, this bit is set by hardware. Over time, these bits can be examined to determine which pages have been used and which pages have not been used since the last time they were set to 0. The page whose bit is 0 can be eliminated because it has not been accessed in the recent period.
4) Clock replacement algorithm (approximate implementation of LRU algorithm)
5) Least used (LFU) replacement algorithm
When using the least used replacement algorithm, it should be in Each page in memory is set up with a shift register to record how often the page is accessed. The replacement algorithm selects the page that was least used in the previous period as the eviction page.
Since the memory has a high access speed, such as 100 ns, a page may be accessed thousands of times continuously within 1 ms. Therefore, it is usually not possible to directly use a counter to record the number of times a page is accessed. times, instead using a shift register method. Each time a page is accessed, the highest bit of the shift register is set to 1, and then shifted to the right every certain time (for example, 100 ns). In this way, the page that has been least used in the recent period will be the page with the smallest ΣRi.
The page access graph of the LFU replacement algorithm is exactly the same as the access graph of the LRU replacement algorithm; in other words, using such a set of hardware can implement both the LRU algorithm and the LFU algorithm. It should be pointed out that the LFU algorithm does not really reflect the page usage, because in each time interval, only one bit of the register is used to record the page usage. Therefore, accessing once is equivalent to accessing 10,000 times.
6) Working set algorithm
7) Working set clock algorithm
8) Aging algorithm (an efficient algorithm very similar to LRU)
9) NRU (Not used recently) Algorithm
10) Second chance algorithm
The basic idea of ??the second chance algorithm is the same as FIFO, but it has been improved to avoid frequently used pages Replace it. When a replacement page is selected, its access bits are checked. If it is 0, the page is eliminated; if the access bit is 1, it is given a second chance and the next FIFO page is selected. When a page gets a second chance, its access bit is cleared to 0 and its arrival time is set to the current time. If the page has been visited during this period, position 1 is visited. Pages thus given a second chance will not be eliminated until all other pages have been eliminated (or have also been given a second chance). Therefore, if a page is used frequently, its access bit always remains 1 and it is never eliminated.
The second chance algorithm can be viewed as a circular queue. Use a pointer to indicate which page is to be eliminated next. When a block of memory is needed, the pointer advances until it finds a page whose access bit is 0. As the pointer advances, the access bit is cleared to 0. In the worst case, all access bits are 1 and the pointer goes through the entire queue for a week, giving each page a second chance. At this time, it degenerates into the FIFO algorithm.
The above is the detailed content of What is the page replacement algorithm?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Inflation is a phenomenon of general price increases, including demand-driven, cost-driven and currency super-engineering; its impacts include shrinking deposits, unable to catch up with prices, and loans becoming "favorable"; response methods include appropriate investment, increasing income sources, controlling consumption rhythms and paying attention to policy trends.

The key to writing a good resume is to clarify the goals, concise structure, and focus the content. First, the resume should be tailored for specific positions, adjust the content according to the position requirements, highlighting the matching skills and experience; second, the structure should be clear, including contact information, personal profile, experience and other modules, and distinguish them with titles and boldness to avoid large paragraphs of text; third, the experience description should be spoken with facts and data, and the verbs should be started with digital quantization and achievement-oriented methods to show actual value; finally, pay attention to the details of the layout, unify the font and spacing, use PDF format, and standardize the name of the file to improve professionalism. Repeated modifications or asking others to check will help improve the quality of your resume.

Screenshot methods vary depending on the device. Common operations are as follows: 1. Windows: PrtScn full screen, Alt PrtScn screenshots the current window, Win Shift S free selection screenshots, Win PrtScn automatically saves; 2. Mac: Shift Cmd 3 full screen screenshots, Shift Cmd 4 selection or click window screenshots; 3. iPhone: Press the power home button model with Home button, and press the power volume plus key for the full screen model with Power Volume; 4. Android: Generally, the power volume down key, and some brands support gesture screenshots; 5. Special needs can be used to scroll screenshots, screen recording functions or third-party tools such as Snagit and Lightshot. Master the commonly used shortcut keys to be familiar

The key to converting PDF to Word is to select the right tool and pay attention to the format preservation. ① Use Adobe Acrobat to directly export to .docx, which is suitable for text-type PDF and is not easy to mess with. The operation steps include opening the file, clicking "Export PDF", selecting the format and downloading and checking; ② Online tools such as Smallpdf and iLovePDF are suitable for daily simple conversion, but you need to pay attention to privacy risks and possible format confusion; ③ New version of Word supports direct import of PDF, which is suitable for situations where only small edits are required. The operation is to insert files and automatically identify content by Word; ④ Scanned files must first use OCR tools to identify text, and pay attention to details such as font changes and misalignment of column tables. It is recommended to manually adjust after conversion to ensure that

YourIPaddressisessentialforinternetconnectivityandnetworkmanagement.TocheckyourpublicIPaddress,search“WhatismyIP?”onGoogle,useasmartphonebrowser,orvisitdedicatedwebsiteslikewhatismyipaddress.com.ForyourlocalIPaddress,followthesesteps:1)OnWindows,open

The core of artificial intelligence is algorithms, especially models that can learn laws from data, such as deep learning. It trains the system through a large amount of data, allowing it to make judgments on new situations, such as face recognition and chatbots. AI is not really intelligent, but statistical methods that mimic human behavior. Common applications include voice recognition (such as Siri), image recognition (such as Alipay face-scanning payment), recommendation systems (such as TikTok and Taobao recommendations), and autonomous driving. AI's capabilities have boundaries, it can only work within the scope of training data, has no real awareness, and relies on a large number of computing resources. When looking at AI, it should be rational. It is an efficient tool but not perfect. It can be used to improve efficiency, but it also needs to be wary of its limitations.

If you want to view the graphics card driver version on your computer, you can do it by: 1. Use Device Manager to view: Win X to open the Device Manager, expand the display adapter, right-click the graphics card to select properties, and view the version and date in the driver tab; 2. View through DirectX diagnostic tool: Win R enter dxdiag to view the driver version and related graphics information in the display tab; 3. Use the official software of the graphics card manufacturer to query: such as NVIDIA's GeForceExperience, AMD's Radeon Software or Intel's Driver&Support Assistant, the main interface will display the current driver status and support updates; 4.

The key to building a website is to select the right tools and follow a clear process. 1. First, clarify the website goals and types, such as blogs, official websites, e-commerce, etc., and select adapter tools, such as WordPress, Shopify or Wix. 2. Register the domain name (recommended.com) and select a hosting platform, such as hosting, Vercel or Netlify. 3. Design the page structure, including homepage, about us, product pages, etc., to ensure that the layout is clear and easy to use. 4. After going online, continue to optimize content, check links, adapt to mobile terminals, and improve visibility through SEO. Follow the steps and the website construction can be completed within a few days.