Virtual memory management of operating system review

Table of contents

Table of contents

1. The concept of virtual memory

2. Request page management

3. Page Replacement Algorithm

4. Page allocation strategy

5. Jitter and working set


1. The concept of virtual memory

(1) Features of conventional storage management methods

① One-off: The job must be loaded into the memory all at once before it can start running . When the job is very large, it cannot be loaded into the memory completely, resulting in the inability to run the large job; when a large number of jobs are required to run, only a small number of jobs can run because the memory cannot accommodate all the jobs, resulting in a decrease in the concurrency of multi-programming.

②Residency: Once a job is loaded into the memory, it will remain in the memory until the job ends . In fact, in a period of time, only a small part of the data of the job needs to be accessed to run normally, and the data that resides in the memory that is temporarily unused wastes precious memory resources.

(2) The principle of locality

 ①Temporal locality : If an instruction in the program is executed, it is likely to be executed again soon; if a certain data is accessed, the data is likely to be accessed again soon. (Because there are a lot of loops in the program)

 ②Spatial locality : Once a program accesses a certain storage unit, it is very likely that its nearby storage units will also be accessed shortly thereafter. (Because a lot of data is stored continuously in memory, and the instructions of the program are also stored in memory sequentially)

Based on the above characteristics, cache technology uses time locality to store recently used instructions and data in cache memory to solve the problem of mismatch between CPU and memory speed and improve computer system performance. The virtual memory technology establishes a two-level memory structure of "internal memory and external memory" according to the local space, and realizes logical expansion of cache and memory.

(3) Definition and characteristics of virtual memory 

 Based on the principle of locality, when the program is loaded, the part of the program that will be used soon can be loaded into the memory, and the part that will not be used temporarily can be left in the external memory , so that the program can start to execute. During the execution of the program, when the accessed information is not in the internal memory, the operating system is responsible for transferring the required information from the external storage to the internal memory , and then continues to execute the program.

If the memory space is not enough, the operating system is responsible for swapping out the temporarily unused information in the memory to the external memory . From the user's point of view, it seems that there is a memory much larger than the actual memory, which is virtual memory.

The size of the virtual memory is determined by the address structure of the computer , not a simple addition of memory and external memory. Virtual memory has the following four main characteristics:

① Discreteness. That is, discontinuity, which is the premise of implementing virtual memory management technology.

②Multiple times. A job is divided into multiple calls to memory.

③ interchangeability. It is allowed to replace and swap out during the running of the job.

④ Virtuality. Can logically expand the memory capacity.

(4) Implementation of virtual memory technology

  ①Request paging storage management

  ②Request segmented storage management

  ③Request section page storage management

2. Request page management

Demand paging is based on paging storage management, which allows some jobs to be loaded into memory. At the same time, it adds the demand paging function (the page does not exist in the memory and is transferred through the paging function) and the page replacement function (swapping temporarily unused pages to external storage) to support the virtual memory function . Currently, the demand paging system is the most commonly used method for implementing virtual memory. In addition to a computer system that requires a certain capacity of memory and external storage, it also needs a page table mechanism , a page fault interrupt mechanism , and an address translation mechanism .

(1) Page table mechanism

  Different from the basic paging system, the request paging system does not require pages to be loaded into the memory at one time, so there may be cases where the page to be accessed is not in the memory. In order to find and solve this problem, four fields are added on the basis of the basic paging page table:

 (2)  Page fault interrupt mechanism

  In demand paging systems, a page fault occurs when the page to be accessed is not in memory, and is handled by the operating system's page fault handler. At this time, the page-missing process is blocked and put into the blocking queue. After paging is completed, it will be woken up and put back into the ready queue.

If there is a free block in the memory, allocate a free block for the process, load the missing page into the block, and modify the corresponding page table entry in the page table.

If there is no free block in the memory, a page is selected by the page replacement algorithm to be eliminated. If the page has been modified during the memory, it must be written back to the external memory. Unmodified pages do not need to be written back to external storage.

[Note] : Multiple page fault interrupts can be generated during one instruction; page fault interrupts are internal interrupts (note the difference between internal interrupts and external interrupts)

(3) Address translation mechanism

On the basis of basic paging management, three new steps are added to the address translation process of request paging:

  ①Request paging (judgment when page table entry is found)

 ②Page replacement (need to transfer pages, but when there is no free memory block)

 ③ It is necessary to modify the newly added items in the request page table

 3.  Page replacement algorithm

    When swapping temporarily unused information from memory to external memory, a page replacement algorithm needs to be used to determine which page to swap out

(1) Best Replacement OPT

       Choose to eliminate pages that will never be used in the future, or pages that will not be accessed for the longest time, so as to ensure the lowest page fault rate. But the operating system cannot predict which page will be accessed next, so in fact OPT cannot be realized

[eg]: Assume that the system allocates three memory blocks for a certain process, and consider the following page number reference strings (these pages will be accessed sequentially): 7,0, 1,2,0,3,0,4,2,3,0,3,2, 1,2,0,1, 7,0,1, calculate the number of page faults and page fault rate during the access process using the OPT algorithm

 (2)  First in first out page replacement FIFO

  Each time the page that enters the memory the earliest (or the one that stays in the memory for the longest time) is eliminated, the pages are sorted according to the order in which they are loaded into the memory, and the head page can be selected when replacing

[eg]: Assume that the system allocates three memory blocks for a process, and consider the following page number quotation strings: 3,2,1,0,3,2,4,3,2,1,0,4, calculate the page fault rate according to the FIFO algorithm

 (3)  The replacement LRU has not been used for the longest time recently

 Each time the page that has not been used recently and the longest is eliminated, the access field in the page table entry can be used to record the time that the page has elapsed since the last visit, and the page with the largest value among the existing pages is selected for elimination.

[eg]: Assume that the system allocates four memory blocks for a process, and consider the following page number reference strings: 1,8, 1, 7,8,2, 7,2, 1,8,3,8,2, 1,3,1, 7,1,3, 7, use the LRU algorithm to calculate the page fault rate

(4) Clock replacement CLOCK

 ① Simple Clock

  Set an access bit for each page, and then link the pages in the memory into a circular queue through the link pointer. When a page is accessed, its access bit is 1. When a page needs to be retired, just check the page's access bit. If it is 0, select the page to be swapped out; if it is 1, set it to 0, do not swap out for now, and continue to check the next page. If all pages in the first round of scanning are 1, set the access bits of these pages to 0 in turn, and then perform the second round of scanning (the simple CLOCK algorithm selects an eliminated page and it will go through two rounds of scanning at most)

 [eg]: Assume that the system allocates five memory blocks for a process, and consider the following page number reference strings: 1, 3, 4, 2, 5, 6, 3, 4, 7

 

 ②Improved Clock

Simple clock replacement algorithms only take into account whether a page has been accessed recently. But only when the eliminated pages are modified and outdated, I/O needs to be performed and written back to external memory. Therefore, in addition to considering whether a page has been visited recently, it should also consider whether the page has been modified. All other conditions are the same, pages that have not been modified should be eliminated first to avoid I/O operations. Modification bit=0, indicating that the page has not been modified; modification bit=1, indicating that the page has been modified. The status of each page can be expressed in the form of (access bit, modification bit). For example, (1, 1) indicates that a page has been visited recently and has been modified.

Arrange all pages that may be replaced in a circular queue

   The first round: Scanning from the current position to the first (0,0) frame (indicating that the page has not been accessed or modified recently, it is the best eliminated page) is used for replacement. This round of scanning does not modify any flag bits

  The second round: If the first round of scanning fails, re-scan to find the first (0,1) frame (indicating that the page has not been accessed recently, but has been modified, not a good elimination) for replacement. This round sets all scanned frame access bits to 0

  The third round: If the second round of scanning fails, re-scan to find the first (0, 0) frame for replacement. This round of scanning does not modify any flag bits

  The fourth round: if the third round of scanning fails, rescan to find the first (0,1) frame for replacement.

The improved CLOCK replacement algorithm selects a eliminated page and performs up to four rounds of scanning

4. Page allocation strategy

(1) Page allocation replacement strategy

Resident set: Request the set of physical blocks allocated to the process in paging storage management, generally smaller than the total size of the process.

If the resident set is too small, it will cause frequent page faults and affect system performance; if the resident set is too large, the concurrency of multiprogramming will decrease

①Fixed Partition Partial Replacement : According to the type of the process or the suggestion of the programmer or system administrator, allocate a fixed number of pages of memory space for each process. and remain unchanged throughout the run. When a page is missing, it will also be replaced within the scope of this process page.

②Global replacement of variable partitions : At the beginning, a certain number of physical blocks will be allocated to each process. The operating system maintains a queue of free physical blocks. When a page fault occurs in a process, a block is taken from the free physical block and allocated to the process; if there is no free physical block, an unlocked page can be selected to be swapped out of the external memory, and then the physical block is allocated to the page-missing process. The page selected to be called out may be a page in any process in the system, so the physical blocks owned by the selected process will decrease, and the page fault rate will increase.

③Partial replacement of variable partition : At the beginning, a certain number of physical blocks will be allocated to each process. When a page fault occurs in a process, only one of the process's own physical blocks is allowed to be swapped out of the external memory. If a process frequently misses pages during operation, the system will allocate several more physical blocks to the process until the trend of the process’s page fault rate is appropriate; on the contrary, if the process’s page fault rate is particularly low during operation, the physical blocks allocated to the process can be appropriately reduced.

[Note]: Variable allocation global permutation will allocate new physical blocks as long as there is a page fault; variable allocation local permutation will dynamically increase or decrease the physical block of the process according to the frequency of page faults

(2) Paging strategy

① When to call in

1) Pre-paging strategy : According to the principle of locality, it is more efficient to transfer several adjacent pages at a time than to transfer only one page at a time. However, if too many pages are loaded at one time, the utilization rate of the CPU may also decrease. Therefore, it is necessary to adopt a pre-paging strategy based on prediction, and pre-adjust pages that are expected to be accessed in the near future. At present, the success rate of this paging strategy is only 50%, so this strategy is mainly used for the first transfer of the process, and the programmer points out which pages should be transferred first.

2) Demand paging strategy : When the process finds a page fault during operation, it transfers the missing page into the memory. The pages transferred by this strategy will definitely be accessed, and it is easier to implement, but only one page is transferred at a time, and excessive I/0 overhead will be spent when there are many pages transferred in/out.

[Note]: Preloading is loading , and demand paging is loading during running as needed. Usually, two paging strategies will exist at the same time.

②Where to transfer

In the request paging system, the external memory is usually divided into two parts: the swap area and the file area. The former is used to store swap pages, and the latter is used to store files:

The problem thus consists of the following three approaches:

1) When the system has enough swap area, all pages can be transferred from the swap area (but before the process runs, it must be copied from the file area to the swap area)

 2) When the system lacks enough swap area, all unmodified files are imported from the file area, and they do not need to be written back to the disk when swapping out, and need to be transferred from the file to the next time; for those that may be modified, write back to the swap area when swapping out, and then transfer from the swap area next time.

 3) NIX mode: All pages that have not been run are transferred from the file area; and for pages that have been run but have been swapped out, they should be transferred from the swap area when they are transferred next time.

5. Jitter and working set

(1) Jitter

 For the page that has just been swapped out, it will be swapped into memory immediately , and the page just swapped in will be swapped out of memory immediately . This frequent page swapping behavior is called jitter

 The main reason for jitter is that there are too few physical blocks allocated to the process, and the number of pages frequently accessed by the process is higher than the number of available physical blocks, which causes the process to spend more time on swapping than the execution time, and the process is in a state of jitter

(2) Workset

The working set refers to the collection of pages actually accessed by the process within a certain period of time (pay attention to distinguish the resident set, which is generally not smaller than the working set). The working set is determined by the time and the window size. Assuming that the working set window size set by the system is 4, the working sets at the two moments in the following figure are:

Guess you like

Origin blog.csdn.net/weixin_46516647/article/details/125029351