OS 11: Virtual Memory

Table of contents

1. Overview of virtual memory

(1) Characteristics and locality principles of conventional storage management methods

1.1 - Characteristics of conventional memory management methods

1.2 - Principle of locality

1.3 - Basic workings of virtual memory

(2) Definition and characteristics of virtual memory

2.1 - Definition of virtual memory

2.2 - Characteristics of virtual memory

2.3 - Implementation of virtual memory

2. Request paging storage management method

(1) Request hardware support in paging

1.1 - Request Page Table Mechanism

1.2 - Page Fault Interrupt Mechanism

1.3 - Address Translation Mechanism

(2) Request memory allocation in paging

2.1 - Determination of the minimum number of physical blocks

2.2 - Memory allocation strategy

2.3 - Physical block allocation algorithm

(3) Page loading strategy

3.1 - When should the system load the required page?

3.2 - Where should the system load these pages from?

3.3 - Page loading process

3.4 - Page Fault Rate

(4) Page replacement algorithm

4.1 - Optimal permutation algorithm

4.2 - First-In-First-Out (FIFO) Page Replacement Algorithm

4.3 - Least Recently Used (LRU) replacement algorithm

4.4 - Least Frequently Used (LFU) replacement algorithm

4.5 - Simple Clock Replacement Algorithm

4.6 - Improved Clock Replacement Algorithm

(5) Page Buffering Algorithm (PBA)

5.1 - Several factors affecting the efficiency of page switching in and out

5.2 - Page Buffer Algorithm PBA

3. Jitter and working set

(1) Multiprogramming and "jitter"

1.1 - Degree of Multiprogramming and Processor Utilization

1.2 - Causes of "jitter"

(2) Working set

2.1 - Basic concepts of working sets

2.2 - Definition of working set

(3) Prevention of "jitter"

3.1 - Partial replacement strategy

3.2 - Incorporating the working set algorithm into processor scheduling

3.3 - Using the "L=S" criterion to adjust the page fault rate

3.4 - Select suspended process

4. Request segmentation storage management method

(1) Hardware Support in Request Segmentation - Request Segment Table Mechanism

(2) Sharing and protection of segments

2.1 - Shared Segment Table

2.2 - Allocation and recovery of shared segments

2.3 - Segment protection


1. Overview of virtual memory

(1) Characteristics and locality principles of conventional storage management methods

1.1 - Characteristics of conventional memory management methods

        Traditional memory management methods have the following two common features:

  • One-time means that the job must be loaded into the memory all at once before it can start running.
  • Persistence means that after a job is loaded into memory, the entire job remains in memory, and no part of it will be swapped out until the job finishes running.

        It can be seen from this that the above-mentioned one-time and resident characteristics make many programs that are not used or temporarily used during program operation occupy a large amount of memory space, and some jobs that need to be run cannot be loaded and run. Obviously, this is You are wasting precious memory resources.

1.2 - Principle of locality

        When the program is executed, it will show locality, that is, in a short period of time, the execution of the program is limited to a certain part, and correspondingly, the storage space it accesses is also limited to a certain area.

        Reasons: First, programs are executed sequentially in most cases, and the depth of procedure calls generally does not exceed 5. Secondly, there are many loop structures in the program, and the processing of many data structures is also limited to a small range. //Supplement: The procedure call will transfer the execution area of ​​the program, and the program loop and data structure are often only a small range

        The limitations are manifested in the following two aspects:

  • time constraints . If an instruction in the program is executed, the instruction may be executed again in the near future; if a certain data is accessed, the data may be accessed again in the near future. A typical cause of time constraints is the existence of a large number of loop operations in the program .
  • Space limitations . Once a program accesses a certain storage unit, its nearby storage units will also be accessed in a short time, that is, the addresses accessed by the program within a period of time may be concentrated within a certain range, and the typical situation is the order of the program execute .

1.3 - Basic workings of virtual memory

        Based on the principle of locality, it is not necessary to load all the applications into the memory before running, but only need to load the few pages or segments that are currently running into the memory before running, and leave the rest on the disk. // Partially loaded into memory

        When the program is running, if the page it wants to access has been transferred into memory, it can continue to execute; but if the page it wants to access has not been transferred into memory (called a page fault or segment), a page fault interrupt request is issued , At this time, the OS will use the demand paging function to transfer them into memory so that the process can continue to execute. If the memory is full at this time and no new pages can be loaded, the OS must use the page replacement function to transfer the temporarily unused pages in the memory to the disk, free up enough memory space, and then replace the pages to be accessed. Called into memory, so that the program continues to execute. //page fault -> page fault interrupt -> memory full -> page replacement

        In this way, a large user program can be run in a smaller memory space, and more processes can be loaded into the memory at the same time, so that they can be executed concurrently.

(2) Definition and characteristics of virtual memory

2.1 - Definition of virtual memory

        The so-called virtual memory refers to a memory system that has the function of request transfer and replacement , and can logically expand the memory capacity. Its logical capacity is determined by the sum of memory capacity and external storage capacity, and its operating speed is close to that of memory, while its cost is close to that of external storage. //Virtual storage = request transfer + page replacement

2.2 - Characteristics of virtual memory

        Compared with traditional memory management methods, virtual memory has the following three important characteristics:

  • The program is allowed to be divided into multiple times and loaded into the memory to run. (multiple times)
  • Allows programs and data in a job to be swapped in and out during the running of the job. (commutability)
  • The memory capacity seen by the user is much larger than the actual memory capacity. (virtuality)

        Virtuality is based on multiple times and interchangeability . Only when the system allows jobs to be transferred into memory multiple times, and programs and data that are temporarily not running in memory can be swapped to disk, can virtual memory be realized. ; And multiple times and interchangeability must be established on the basis of discrete distribution . // Discrete allocation -> multiple and commutative -> virtual

2.3 - Implementation of virtual memory

        (1) Paging request system

The paging request system is a paging virtual storage system formed by adding the paging function and page replacement function         on the basis of the paging system . It allows user programs to start running with only a few pages loaded. Later, through the paging function and the page replacement function, the pages that are about to run are transferred into the memory one after another, and the pages that are not running are swapped out to the external memory at the same time.

        Pages are used as the unit for replacement. In order to implement demand paging and page replacement, the system must provide the necessary hardware support and software to implement demand paging.

  • Hardware support : page table mechanism for request paging, page fault interrupt mechanism, address translation mechanism.
  • Software Support : Software for implementing demand paging and software for implementing page replacement. With the support of the hardware, they transfer the pages needed by the program into the memory when the program is running, and then replace the temporarily unused pages in the memory from the memory to the disk. // is the algorithm

        (2) Request Segmentation System

The request segmentation system is a segmented virtual storage system formed         on the basis of the segment system, adding the functions of request adjustment and segment replacement .

  • Hardware support: segment table mechanism for segment request, interrupt mechanism for missing segment, and address conversion mechanism.
  • Software support: software used to realize segment adjustment and segment replacement.

2. Request paging storage management method

(1) Request hardware support in paging

        In order to implement request paging, the system must provide certain hardware support. In addition to requiring a certain capacity of memory and external memory, the computer system also needs a request page table mechanism, a page fault interrupt mechanism, and an address translation mechanism.

1.1 - Request Page Table Mechanism

        The main data structure required in the request paging system is the request page table, and its basic function is still to map the logical address in the user address space to the physical address in the memory space. In order to meet the needs of page swapping in and out, four more fields are added in the request page table. Thus, each page table in a demand paging system should contain the following items:

        Each field is described as follows:

  • Status bit (existence bit) P: Since only a part of the application program is transferred into the memory in the request paging system, and the other part is still on the external storage disk, an existence bit field must be added to the page table. Since this field has only one bit, it is also called a bit word. It is used to indicate whether the page has been loaded into memory for reference when the program accesses it.
  • Access field A: used to record the number of times this page has been accessed within a certain period of time , or record how long this page has not been accessed recently, and provide it to the replacement algorithm for reference when selecting pages to swap out.
  • Modification bit M: Indicates whether the page has been modified after being loaded into memory . Since each page in the memory keeps a copy on the external storage, if the page is not modified when the page is replaced, there is no need to write the page back to the external storage to reduce system overhead and start-up. The number of times on the disk; if it has been modified, the page must be rewritten to the external memory to ensure that the copy kept in the external memory is always up-to-date. In short, the M bit is for reference when replacing pages.
  • External storage address: used to point out the address of the page on the external storage, usually the physical block number , for reference when loading the page. // This item is duplicated with the second physical block number, so for now

1.2 - Page Fault Interrupt Mechanism

        As interrupts, page fault interrupts also need to go through several steps such as protecting the CPU environment, analyzing the cause of the interrupt, transferring to the page fault interrupt handler for processing, and restoring the CPU environment after the interrupt processing is completed. // Here, do not describe too much, just know that it is a kind of interrupt hardware

1.3 - Address Translation Mechanism

        The address translation mechanism in the request paging system is formed on the basis of the address translation mechanism of the paging system, and some functions are added to realize the virtual memory, such as generating and processing page fault interrupts, and swapping out a page from the memory functions and so on. // Here, do not describe too much

(2) Request memory allocation in paging

        When allocating memory for a process, three issues will be involved:

  • First, in order to ensure that the process can run normally, the determination of the minimum number of physical blocks required. //allocation quantity
  • Second, what kind of allocation strategy should be adopted when allocating physical blocks for each process, that is, whether the allocated physical blocks are fixed or variable. //Allocation
  • Thirdly, the number of physical blocks allocated to different processes should be allocated according to the average allocation algorithm or proportionally according to the size of the process. // distribution fairness

2.1 - Determination of the minimum number of physical blocks

As the physical block         allocated for each process decreases, the page fault rate of the process during execution will increase, thereby reducing the execution speed of the process. So in order for a process to work efficiently, it should be allocated a reasonable number of physical blocks.

The minimum number of physical blocks refers to the minimum number of physical blocks         required to ensure the normal operation of the process . When the number of physical blocks allocated by the system to the process is less than this value, the process will not be able to run . As for the minimum number of physical blocks that a process should obtain, it is related to the hardware structure of the computer and depends on the format, function and addressing mode of the instruction. // related to the specific computer, no in-depth description

2.2 - Memory allocation strategy

        In the demand paging system, two memory allocation strategies can be adopted, namely fixed and variable allocation strategies. When performing replacement, two strategies can also be adopted, namely global replacement and local replacement. The following three applicable strategies can then be combined:

        (1) Fixed Allocation, Local Replacement (Fixed Allocation, Local Replacement)

        The so-called fixed allocation refers to the allocation of a set of fixed number of physical blocks for each process , which will not change during the running of the process. The so-called partial replacement means that if a process finds a page fault during operation, it can only select one page to swap out from the n pages allocated to the process, and then transfer one page in to ensure the memory allocated to the process The space does not change.

        The difficulty in implementing this strategy is that it is difficult to determine how many physical blocks should be allocated to each process . If it is too small, there will be frequent page fault interrupts, which will reduce the throughput of the system. If there are too many, it will inevitably reduce the number of processes resident in the memory, which may cause the CPU or other resources to be idle, and it will take more time to realize process swapping. // Difficult to implement, or unstable performance after implementation

        (2) Variable Allocation Global Replacement (VariableAllocation, GlobalReplacement)

        The so-called variable allocation refers to assigning a certain number of physical blocks to each process first, which can be appropriately increased or decreased according to the situation during the running of the process . The so-called global replacement means that if a process finds a page fault during operation, it will take out one of the free physical blocks reserved by the OS and assign it to the process, or use all the physical blocks of all processes as the target, select a block to swap out, and then replace all the physical blocks. Load missing pages. In this way, the memory space allocated to the process increases accordingly.

        Variable allocation global permutation is the easiest physical block allocation and permutation strategy to implement and has been used in several OSs. When this strategy is adopted, any process that generates a page fault will obtain a new physical block, and only when the physical blocks in the free physical block queue are used up, the OS can select a page to call out from the memory. 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, which will lead to an increase in its page fault rate. //The disadvantage is that it will affect other programs

        (3) Variable Allocation Local Replacement (VariableAllocation, LocalReplacement)

        This strategy allocates a certain number of physical blocks to each process based on the type of process or according to the requirements of the programmer, but when a process finds a page fault, only one page is allowed to be swapped out from the memory pages of the process, so that It will not affect the operation of other processes .

        If a process frequently suffers page faults during operation, the system must allocate some additional physical blocks to the process until the page fault rate of the process is reduced to an appropriate level. Conversely, if the page fault rate of a process is particularly low during operation, the number of physical blocks allocated to the process can be appropriately reduced at this time, but the page fault rate should not cause a significant increase. // will not affect other programs

2.3 - Physical block allocation algorithm

        When using a fixed allocation strategy , how to allocate all the physical blocks available for allocation in the system to each process, the following algorithms can be used:

  • The average allocation algorithm is to allocate all available physical blocks in the system to each process equally.
  • Proportional allocation algorithm, which allocates physical blocks proportionally according to the size of the process.
  • Assignment Algorithm by Priority . In practical applications, in order to ensure that important and urgent tasks can be completed as soon as possible, more memory space should be allocated to it. The usual method is to divide all physical blocks available for allocation in memory into two parts: one part is allocated to each process in proportion; the other part is allocated according to the priority of each process, and the corresponding share is appropriately increased for high priority processes .

(3) Page loading strategy

        In order for the process to run normally, the pages where the part of the program and data to be executed must be transferred into the memory in advance, the problem now is:

  • When should the system load the required page?
  • Where should the system load these pages from?
  • How is the page loaded?

3.1 - When should the system load the required page?

        Pre-paging strategy : Using a prediction-based pre-paging strategy, pages that are expected to be accessed in the near future are pre-loaded into memory. // Pre-judgment, the disadvantage is that the prediction rate is not high

        The use of the pre-paging strategy: First, when the process is transferred into the memory for the first time, the page indicated in the program is transferred into the memory first. Then, in a system using a working set, each process has a table that records the working set at runtime, and whenever the program is scheduled to run, all pages in the working set are loaded into memory. // Use the concept of working set

        Demand paging strategy: When a process needs to access some programs and data during operation, if it finds that the page where it is located is not in the memory, it will immediately make a request, and the OS will load the required page into the memory. //Used to take calls, the disadvantage is that the I/O frequency is high

        The page determined by the demand paging policy must be accessed, and the demand paging policy is relatively easy to implement, so this policy is mostly used in the current virtual memory. But this strategy only loads one page at a time, so it takes a lot of system overhead and increases the startup frequency of disk I/O.

3.2 - Where should the system load these pages from?

The external storage in the request paging system is divided into two parts: the file area         for storing files and the swap area for storing swap pages .

        Usually, since the swap area is continuously allocated, while the file area is allocated discretely, the data access (disk I/O) speed of the swap area is higher than that of the file area. In this way, whenever a page fault request occurs, where the system should transfer the fault page into the memory can be divided into the following three situations:

  • Sufficient space in the swap area: all required pages are transferred from the swap area to improve paging speed. Therefore, before the process runs, the files related to the process must be copied from the file area to the swap area. //All transferred from the swap area
  • Insufficient space in the swap area: files that will not be modified are directly transferred from the file area; Import directly from the file area. For the parts that may be modified, they must be transferred to the swap area when they are swapped out, and then transferred from the swap area when needed later. //Distinguish between non-modification and modification
  • UNIX mode: Since the files related to the process are placed in the file area, all pages that have not been run should be transferred from the file area. As for the pages that have been operated but have been swapped out, since they are placed in the swap area, they should be transferred from the swap area when they are called in next time. Because the UNIX system allows page sharing, the page requested by a certain process may have been transferred into the memory by other processes, and there is no need to transfer it from the swap area at this time. //Distinguish whether it has been run or not

3.3 - Page loading process

        Whenever the page to be accessed by the program is not in the memory (existence bit is "0"), a page fault interrupt is sent to the CPU . The interrupt handler first reserves the CPU environment, analyzes the cause of the interrupt, and transfers to the page fault interrupt handler.

        After the program obtains the physical block of the page in the external storage by looking up the page table, if the memory can accommodate the new page at this time, it starts disk I/O, transfers the missing page into the memory, and then modifies the page table. If the memory is full, you must first select a page from the memory to be swapped out according to a certain replacement algorithm. // call or replace

        If the page has not been modified (modified bit is "0"), it is not necessary to write the page back to disk; but if the page has been modified (modified bit is "1"), it must be written back to disk, and then Then transfer the missing page into the memory, and modify the corresponding table entry in the page table, set its existence bit as "1", and write this page table entry into the fast table. //Process the modified data

        After the missing page is loaded into the memory, use the modified page table to form the physical address of the data to be accessed, and then access the memory data.

        The loading process of the entire page is transparent to the user.

3.4 - Page Fault Rate

        If during the running of the process, the number of successful page access (that is, the accessed page is in memory) is S, and the number of failed page access (that is, the accessed page is not in memory and needs to be transferred from external storage) is F, then The total number of page accesses of the process is A = S + F, then the page fault rate of the process during its operation is: //The page is not in the memory, it is a page fault

        Generally, the page fault rate is affected by the following factors:

  • page size . The larger the page division, the lower the page fault rate; otherwise, the higher the page fault rate.
  • The number of physical blocks allocated by the process . The more physical blocks allocated, the lower the page fault rate; vice versa
  • Page replacement algorithm . The quality of the algorithm determines the number of page fault interruptions during process execution, so the page fault rate is an important indicator to measure the page replacement algorithm.
  • Inherent in the program . The programming method of the program itself has an impact on the number of page faults. According to the principle of locality of program execution, the higher the degree of localization of programming, the lower the degree of page faults during execution.

        In fact, when dealing with page fault interrupts, when some pages need to be replaced to external storage due to insufficient space, the cost of replacement needs to be considered when selecting the replaced page, such as whether the page has been modified. Unmodified pages can be discarded directly, while modified pages must be saved.

(4) Page replacement algorithm

        An inappropriate algorithm may lead to "thrashing" in the process , that is, the page that has just been swapped out will be accessed soon, and it needs to be re-loaded. At this time, another page needs to be selected to be called out; The page that has just been called out is quickly accessed again, and it needs to be called in again, and the page is replaced so frequently that a process spends most of its time on page replacement work during operation . jitter" .

4.1 - Optimal permutation algorithm

The eliminated pages         selected by the optimal replacement algorithm will be pages that will never be used in the future and will no longer be accessed in the future .

        Using the best replacement algorithm usually guarantees the lowest page fault rate. However, this algorithm is an idealized algorithm, which has the best performance, but it cannot be realized in practice. The best permutation algorithm is usually used as a standard to evaluate the pros and cons of other algorithms. //Unable to achieve, only as an evaluation standard

4.2 - First-In-First-Out (FIFO) Page Replacement Algorithm

        The FIFO algorithm is the earliest permutation algorithm. The algorithm always eliminates the page that enters the memory first , that is, selects the page that resides in the memory for the longest time to be eliminated.

        The algorithm is simple to implement. It only needs to link the pages that a process has transferred into the memory into a queue in sequence, and set a pointer, called the replacement pointer, so that it always points to the oldest page. However, this algorithm does not adapt to the actual running rules of the process , because in the process, some pages are frequently accessed, such as pages containing global variables, commonly used functions, routines, etc., and the FIFO algorithm cannot guarantee that these pages will not be eliminated.

//Because it does not conform to the usage rules of the usual pages, it may be the algorithm with the worst performance, and it is rarely used in practice.

//The sequence of page loading does not reflect the usage of the page

4.3 - Least Recently Used (LRU) replacement algorithm

        The least recently used (LRU) page replacement algorithm makes a decision based on the usage of the page after it is loaded into the memory. The LRU replacement algorithm selects the page that has not been used for the longest time to be eliminated.

        The algorithm gives each page an access field, which is used to record the time t that a page has experienced since it was last accessed . When a page needs to be eliminated, the page with the largest t value among the existing pages is selected to be eliminated. // Need to record access time

4.4 - Least Frequently Used (LFU) replacement algorithm

When using the LFU algorithm, a shift register         should be set for each page in the memory to record the frequency of the page being accessed .

        The replacement algorithm selects the page that was least used in the most recent period as an evicted page. Due to the high access speed of the memory, such as 100ns, a certain page may be accessed thousands of times in 1s. Therefore, it is unrealistic to directly use the counter to record the number of times a certain page is accessed. Large intervals to record accesses to a page of memory. The shift register method is adopted in the least-used permutation algorithm. // Reasons to use shift registers

        Every time a page is accessed, the highest position of the shift register is set to 1, and then shifted to the right once at regular intervals. //Timing shift

        This algorithm cannot really reflect the usage of the page , because in each time interval, only one bit of the register is used to record the usage of the page. times are completely equivalent.

4.5 - Simple Clock Replacement Algorithm

        Although LRU is a better algorithm, but because it requires more hardware support, the cost of its implementation is relatively high, so in practical applications, the approximate algorithm of LRU is mostly used. The Clock algorithm is an LRU approximation algorithm that is widely used.

        The Clock algorithm sets an access bit for each page, and then links all pages in the memory into a circular queue through link pointers. When a page is accessed, its access bit is set to 1. // record with access bit

        When the replacement algorithm selects a page for elimination, it only needs to check the access bit of the page. If it is 0, select the page to be swapped out; if it is 1, set it to 0 again, do not swap out temporarily, give the page a second chance to reside in memory, and then check the next page according to the FIFO algorithm. When checking the last page in the queue, if its access bit is still 1, then return to the head of the queue to check the first page. //The loop is reduced to the end, there must be a page whose access bit is 0

        Because the algorithm checks the usage of each page cyclically, it is called the Clock algorithm. However, because the algorithm has only one access bit, it can only be used to indicate whether the page has been used, and the unused page is replaced when replacing, so the algorithm is also called the recently unused algorithm or NRU (Not Recently Used Algorithm or NRU ) . Recently Used) algorithm.

4.6 - Improved Clock Replacement Algorithm

        When swapping out a page, if the page has been modified, the page must be rewritten back to disk; but if the page has not been modified, it does not have to be copied back to disk. In other words, for a modified page, the cost of swapping out is greater than that of an unmodified page (large replacement cost).

        In the improved CIock algorithm, in addition to considering the usage of the page, another factor must be added— replacement cost . In this way, when the selected page is swapped out, it must be an unused page or a page that has not been modified . Take the pages that meet both conditions at the same time as the preferred eliminated pages. // Increase the consideration of replacement cost

        Compared with the simple Clock algorithm, this algorithm can reduce the number of disk I/O operations. But in order to find a replaceable page, it may have to go through several rounds of scanning . In other words, the overhead of implementing the algorithm itself will increase. //Increase the overhead of the algorithm

(5) Page Buffering Algorithm (PBA)

5.1 - Several factors affecting the efficiency of page switching in and out

  • Page replacement algorithm . A good page replacement algorithm can make the process have a lower page fault rate during operation , thereby reducing the overhead of page swapping in and out. //page fault rate
  • How often to write back to disk . For pages that have been modified, they should be written back to the disk when they are swapped out, which means that each time a page is swapped out, the disk needs to be started once. However, if a linked list of pages that have been modified and swapped out has been established in the system, then for each page (modified) to be swapped out, the system will not write them back to the disk for the time being, but hang them on the modified swap page. On the linked list of out pages, only when the number of pages being swapped out reaches a certain value, such as 64 pages, they are written back to the disk together, which significantly reduces the number of disk I/O operations. //suspended write + batch write back
  • Frequency of reading into memory . After setting the modified swapped-out page linked list, there is temporarily a batch of pages with data on the linked list. In this way, it can also reduce the frequency of reading pages from disk into memory and reduce the overhead of page swapping.

5.2 - Page Buffer Algorithm PBA

        The main features of the PBA algorithm are:

  • Significantly reduces the frequency of page swapping in and out, greatly reducing the number of disk IO operations , thus reducing the cost of page swapping in and out.
  • Because the overhead of swapping in and swapping out is greatly reduced, a simpler replacement strategy can be adopted, such as the first-in-first-out (FIFO) algorithm, which does not require special hardware support and is very simple to implement. // simple implementation

        In order to significantly reduce the frequency of page swapping in and out, the following two linked lists are set up in memory:

        (1) Free page linked list

        In fact, the linked list is a free physical block linked list, which is a free physical block mastered by the system, and is used to allocate to a process with frequent page faults to reduce the page fault rate of the process. When such a process needs to read a page, it can use the first physical block in the free physical block linked list to load the page. When there is an unmodified page to be swapped out, it is not actually swapped out to external storage, but the physical blocks where they are located are hung at the end of the free list . It should be noted that there is data in these unmodified pages hanging on the free list. If a process needs the data in these pages in the future, they can be removed from the free list, eliminating the need to read from the disk. Data operations reduce the overhead of page swapping. // Unmodified pages are not swapped out

        (2) Modify the page linked list

        It is a linked list of modified pages . The purpose of setting up this linked list is to reduce the number of times that modified pages are swapped out. When a process needs to swap out a modified page, the system does not immediately swap it out to the external memory, but hangs the physical block where it is located at the end of the modified page list. The purpose of this is to reduce the frequency of writing the modified page back to disk and reduce the frequency of reading disk content into memory. //The modified page will not be swapped out temporarily, or it will be swapped out in batches after a certain time interval

3. Jitter and working set

(1) Multiprogramming and "jitter"

1.1 - Degree of Multiprogramming and Processor Utilization

        Because the virtual memory system can logically expand the memory, at this time, only part of the program and data of a process can be loaded to start running, so people hope that more processes can be run in the system, that is, increase the degree of multi-programming, To increase processor utilization. But the actual utilization of the processor is shown by the solid line in the figure below. // page fault rate -> jitter

       The horizontal axis represents the number of multi-programs, and the vertical axis represents the utilization rate of the corresponding processor. At the beginning of the horizontal axis, as the number of processes increases, the utilization rate of the processor increases sharply; but when it reaches N1, its growth rate slows down significantly. When it reaches N(max), the utilization rate of the processor When it reaches the maximum, it starts to decline slowly. When it reaches point N2, if the number of processes continues to increase, the utilization rate will accelerate to decline and tend to 0. See point N3 in the figure. The reason why the utilization rate tends to be in the later stage The case of 0 is because "jitter" has occurred in the system . //Jitter will significantly reduce processor utilization

1.2 - Causes of "jitter"

        The root cause of "jitter" is that there are too many processes running in the system at the same time, so the physical blocks allocated to each process are too small , which cannot meet the basic requirements for the normal operation of the process, causing each process to run frequently when it is running. If there is a page fault , the system must be requested to load the missing page into memory. This increases the number of processes queued for page-in/out in the system. Obviously, the effective access time to the disk also increases sharply, causing most of the time of each process to be used for page swapping in/out, and it is almost impossible to do any effective work, resulting in processor crashes. A situation where utilization drops sharply and tends to zero. We call the process at this time in a state of "jittering" .

(2) Working set

2.1 - Basic concepts of working sets

        The time interval at which a process has a page fault rate is related to the number of physical blocks obtained by the process. The figure below shows the relationship between the page fault rate and the number of physical blocks.

        It can be seen from the figure that the page fault rate decreases significantly with the increase of the number of allocated physical blocks . When the number of physical blocks exceeds a certain number, adding another physical block to the process will not improve the page fault rate significantly. . It can be seen that there is no need to allocate more physical blocks for it at this time. Conversely, when the number of physical blocks allocated for a process is lower than a certain number, every reduction of one block will have a significant impact on the page fault rate, and more physical blocks should be allocated for the process at this time. // The page fault rate is related to the physical block

        Based on the principle of locality when the program is running, it is known that during the running of the program, the access to the page is uneven, and it is limited to a few pages in a period of time, and may be limited to other pages in another period of time. Some fewer pages are accessed. These pages are called active pages. If it is possible to predict which pages a program will access within a certain period of time and transfer them into the memory, the page fault rate will be greatly reduced , thereby significantly improving the utilization rate of the processor. //The background of the emergence of working set theory

2.2 - Definition of working set

        The so-called working set refers to the collection of pages that the process actually needs to access within a certain period of time .

        Although the program only needs a few pages in memory to run, in order to generate fewer page faults, the entire working set of the program should be loaded into memory. However, we cannot predict in advance which pages the program will access at different times, so we still have to use the behavior of the program in a certain period of time in the past as an approximation of the behavior of the program in a certain period of time in the future, just like the replacement algorithm. //The working set is just a collection of pages that estimate past behavior, and may not accurately reflect the future situation

(3) Prevention of "jitter"

        In order to ensure that the system has a large throughput, it is necessary to prevent the occurrence of "jitter". // less jitter -> high throughput

3.1 - Partial replacement strategy

        In the page allocation and replacement strategy, if the variable allocation method is adopted, a partial replacement strategy can be adopted in order to prevent "jittering" from occurring. According to this strategy, when a page fault occurs in a process, it can only be replaced in the memory space allocated to itself, and it is not allowed to obtain new physical blocks from other processes. In this way, even if the process "jitters" occurs, it will not affect other processes, so the impact caused by the process "jitters" can be limited to a smaller range . // Limit jitter to a single process

        Although this method is simple and easy to implement, the effect is not very good, because after a process "jitters", it will remain in the waiting queue for disk I/O for a long time, which will increase the length of the queue, which will prolong the lack of time for other processes. The processing time of the page interrupt is to extend the access time of other processes to the disk. //The actual effect is not good, it will still affect the overall performance

3.2 - Incorporating the working set algorithm into processor scheduling

        When the scheduler finds that the utilization of the processor is low, it will try to transfer a new job from the external storage into the memory to improve the utilization of the processor. If the working set algorithm is integrated into the scheduling, before the scheduler loads jobs from external storage, it must first check whether each process has enough resident pages in memory . If there are enough pages, new jobs can be transferred from the external storage at this time, and the page fault rate will not increase due to the transfer of new jobs; on the contrary, if the memory pages of some processes are insufficient, those pages should be replaced first. Jobs with a high page fault rate add new physical blocks, and no new jobs will be transferred at this time. //Determine whether to add the number of jobs based on the number of pages

3.3 - Using the "L=S" criterion to adjust the page fault rate

        The criterion of "L=S": where L is the average time between page faults, and S is the average fault service time, which is the time required to replace a page. If L is much larger than S, it means that page faults rarely occur, and the capacity of the disk has not been fully utilized; on the contrary, if L is smaller than S, it means that page faults occur frequently, and the speed of page faults has exceeded the processing capacity of the disk . Only when L is close to S can both disks and processors reach their maximum utilization . Both theory and practice have proved that using the "L=S" criterion is very effective in adjusting the page fault rate. // calculate page fault time

3.4 - Select suspended process

        When the degree of multi-programming is high, the utilization rate of the processor has been affected. In order to prevent "jitter", the system must reduce the number of multi-programming . At this time, you should choose to suspend some currently active processes based on a certain principle, and transfer them to the disk, so that the vacated memory space can be allocated to processes with high page fault rates. The system usually adopts a policy consistent with the scheduler, which chooses to suspend the process with the lowest priority first, and then selects the lower priority process if necessary. When the memory is still congested, you can further choose to suspend a process that is not very important but larger, so that more physical blocks can be released, or the process with the most remaining execution time can be suspended. // reduce the number of processes

//The number of processes in a system is limited, and it is impossible to increase it indefinitely. Switching between processes will increase system overhead. Too many processes will also cause system jitter, but the jitter should be resolved by the system layer

4. Request segmentation storage management method

        The demand paging virtual memory system established on the basis of paging is swapped in and out in units of pages. On the other hand , the request-segmented virtual memory system established on the basis of segments is swapped in and out in units of segments . They are very similar in terms of implementation principles and required hardware support. In the request segmentation system, before the program runs, only a few segments need to be transferred to start running. When the accessed segment is not in the memory, the OS can be requested to load the missing segment into the memory.

(1) Hardware Support in Request Segmentation - Request Segment Table Mechanism

        The main data structure required in request segment management is the request segment table. In addition to the four fields of access field A , modification bit M , existence bit P and external storage start address in the request paging mechanism , the table also adds access mode field and supplementary bit . These fields are for program reference when calling in and out.

        In the segment table entry, in addition to the segment name (number), segment length, and the starting address of the segment in memory (segment base address), the following fields are added:

  • Access method: Since the segment in the application program is a logical unit of information, it can be protected according to the attribute of the information, so the access method field is added in the segment table. If the field is two bits, the access attribute is Execute Only, Read Only, and Read/Write Allowed . // read and write mode
  • Access field A: Its meaning is the same as that of the corresponding field requesting pagination, and it is used to record the frequency of the segment being accessed. Provided to the replacement algorithm for reference when choosing to swap out pages.
  • Modification bit M: This field is used to indicate whether the page has been modified after entering the memory, for reference when replacing the page.
  • Presence bit P: This field is used to indicate whether this segment has been loaded into the memory for reference when the program accesses it.
  • Supplementary bit: This is a unique field in request segment management, which is used to indicate whether this segment has been dynamically increased during operation .
  • External storage starting address: Indicates the starting address of this segment in the external storage, that is, the starting disk block number.

// The lack of segment interrupt mechanism, and the support of hardware such as address conversion mechanism will not be repeated

(2) Sharing and protection of segments

        The advantage of segmented storage management is that it facilitates the sharing and protection of segments .

2.1 - Shared Segment Table

        In order to realize segment sharing, a shared segment table can be configured in the system, and all shared segments occupy an entry in the shared segment table. The segment number , segment length , memory start address , status (existence) bit , external storage start address , and sharing count of the shared segment are recorded above the entry, and the next step is to record the information of each process that shares this segment situation . The shared segment table is shown in the following figure:

        The descriptions of each of them are as follows:

  • Shared process count count: The non-shared segment is only required by one process. When the process no longer needs the segment, it can be released immediately, and the space occupied by the segment can be reclaimed by the system. The shared segment is required by multiple processes. To record how many processes are sharing the segment , the shared process count count must be set. When a process no longer needs it and releases it, the system does not reclaim the memory area occupied by the segment immediately, but checks whether the count is 0. If it is not 0, it means that there are processes that need it. When all processes no longer need it, the count is 0 at this time, and the system reclaims the memory area occupied by this segment.
  • Access control field: For a shared segment, different access rights should be assigned to different processes . For example, for the file master, he is usually allowed to read and write; for other processes, he may only be allowed to read, or even only be allowed to execute.
  • Segment number: For a shared segment, different processes can have different segment numbers , and each process can use its own process segment number to access the shared segment.

2.2 - Allocation and recovery of shared segments

        (1) Allocation of shared segments

        Since the shared segment is shared by multiple processes, the memory allocation method for the shared segment is different from the memory allocation method for the non-shared segment.

        When allocating memory for a shared segment, for the first process that requests to use the shared segment, the system allocates a physical area for the shared segment, then transfers the shared segment into this area, and fills the initial address of the area into the request In the corresponding item of the segment table of the process, an entry must also be added in the shared segment table, fill in the process name, segment number and access control and other relevant data requesting to use the shared segment, and set count to 1.

        When other processes need to call the shared segment, since the shared segment has been transferred into the memory, there is no need to allocate memory for the segment at this time, but only need to add an entry in the segment table of the calling process to fill in the shared segment. The physical address of the segment. Add an entry in the segment table of the shared segment, fill in the process name of the calling process, the segment number of the shared segment in this process, access control, etc., and then execute the count = count + 1 operation to indicate that there are two Processes share this segment. //add record to segment table

In the future, any process that needs to access this shared segment will add an entry in the segment table of the shared segment         as described above .

        (2) Recycling of shared segments

        When a process that shares this segment no longer needs the segment, it should release the segment, including canceling the entry corresponding to the shared segment in the segment table of the process, and executing the count = count - 1 operation. If the result is 0, the system must reclaim the physical memory of the shared segment, and cancel the entry corresponding to the segment in the shared segment table, indicating that there is no process using the segment at this time; otherwise (the result of subtracting 1 is not 0 ), just cancel the related records of the caller process in the shared segment table . // Subtract records from segment table

2.3 - Segment protection

        In the segmentation system, since each segment is logically independent, it is easier to realize information protection. At present, the following measures are often used to ensure the security of information.

        (1) Cross-border inspection

        Out-of-bounds checking is done using the address translation mechanism. For this reason, a segment table register is set up in the address translation mechanism to store the segment table start address and segment table length information. When performing address translation, first compare the segment number of the logical address space with the length of the segment table, and if the segment number is equal to or greater than the length of the segment table, an address out-of-bounds interrupt signal will be issued. In addition, a segment length field is set for each segment in the segment table. When performing address conversion, it is also necessary to check whether the address in the segment is equal to or greater than the segment length. If it is greater than the segment length, an address out-of-bounds interrupt signal will be generated to ensure Each process can only run in its own address space.

        (2) Access control check

        Access control checks are performed on a segment basis. For this reason, an "access control" field is set in each entry of the segment table, which is used to specify the access method to the segment. Common access methods are:

  • Read-only , that is, only the process is allowed to read the program or data in the segment.
  • Execute only , that is, the process is only allowed to call this segment to execute, but it is not allowed to read the content of this segment, and it is not allowed to perform write operations on this segment.
  • Read/write , which allows the process to read/write access to the segment.

        For the shared segment, access control is particularly important, so different read and write permissions should be given to different processes. At this time, it is necessary to ensure the security of information and meet the needs of operation. For example, for an enterprise's financial accounts, only accountants should be allowed to read or write, and leaders and related personnel should be allowed to read. For ordinary people, they are neither allowed to read nor write. It is worth mentioning that the access control check introduced here is implemented based on hardware, which can better ensure the security of information, because it is difficult for attackers to modify the access control fields . //The way software is also available in enterprise applications

        (3) Environmental Protection Agency

        This is a more complete protection mechanism. In this mechanism it is stipulated that lower numbered rings have higher priority . The OS core is in ring 0; certain important utilities and operating system services occupy the middle ring, and general applications are arranged on the outer ring. // user mode and kernel mode

        In the ring system, program access and calling should follow the following rules

  • A program can access data residing in the same ring or in a less privileged ring (outer ring)
  • A program can invoke services residing in the same ring or in a higher privileged ring (inner ring)

        So far, the introduction of virtual storage is over.

Guess you like

Origin blog.csdn.net/swadian2008/article/details/131569097