Operating system-virtual memory


1. Overview of virtual storage

1. Characteristics and locality principles of conventional storage management methods

  • Characteristics of conventional storage management methods
    We collectively refer to the various storage management methods introduced in the previous blog as traditional storage management methods, and they all have the following two common features:
    • One-time: The job needs to be loaded into the memory all at once before running. Will cause the following problems.
    • Residency: After the job is loaded into the memory, it stays in the memory until the job is finished.
  • Problems caused by conventional storage management methods :
    • Some jobs are very large, and the required memory space is greater than the total memory capacity, making the job impossible to run.
    • There are a large number of jobs required to run, but the memory capacity is not enough to hold all the jobs, only some of them can be run first, and others are waiting in external storage.
    • Solution: Increase the memory capacity. Logically expand the memory capacity-virtual memory (swap)
  • Principle of locality
    • Refers to the locality of the program during execution, that is, within a short period of time, the execution of the program is limited to a certain part, and correspondingly, the storage space it visits is also limited to a certain area.
  • Locally manifested as
    • Time locality: due to a large number of loop operations, after a certain instruction or data is accessed, it may be accessed again soon;
    • Spatial locality: such as sequential execution, refers to the addresses accessed by the program in a period of time, which may be concentrated in a certain range.

2. Definition and characteristics of virtual storage

  • Definition of virtual storage
    • Virtual storage refers to a storage management system that only loads a part of the job into the memory to run the job .
    • It has a request transfer function and a replacement function , which can logically expand the memory capacity,
    • Its logical capacity is determined by the sum of external memory capacity and memory capacity, its operating speed is close to that of memory , and its cost is close to that of external memory .
  • Characteristics of virtual storage
    • Repetitiveness (the most basic characteristic): Repetitiveness is the most important characteristic of virtual storage . Refers to a job that is divided into multiple memory operations.
    • Exchangeability : Exchangeability refers to allowing swapping in and out during the operation of the job. Swap in and out can improve memory utilization
    • Virtuality (the most essential feature): Virtuality refers to the ability to logically expand the memory capacity so that the memory capacity seen by the user is much larger than the actual memory capacity. Virtuality is the most important feature of virtual storage, and it is also the most important goal of realizing virtual storage.
    • Note: Virtuality is based on multiple and interchangeability, and multiple and interchangeability are based on discrete allocation.

3. Implementation method of virtual memory

  • The realization of virtual memory is based on the management of discrete allocation of memory . Therefore, at present, all virtual memories are implemented using one of the following two methods.
    • Paging request system
    • Segment request system
      Insert picture description here

Two, request paging storage management method

The request paging system is based on the basic paging, and the request paging function and page replacement function are added. The corresponding basic unit of each transfer in and out is a page with a fixed length.

1. Request paging hardware support

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

  • Request page table mechanism
    • The main data structure required in the request paging system is the request page table, which is expanded on the basis of the page table.
      • Status bit P (existing bit): indicates whether the page has been loaded into the memory. Determine whether the page is missing.
      • Access field A: Record the number of times this page has been accessed in a period of time or the time it has not been accessed recently. Decide which page to eliminate according to the visited bit.
      • Modification bit M: Indicates whether the page has been modified after being loaded into the memory. If it has been modified, it needs to be rewritten to the external storage when swapping out. For reference when replacing pages.
      • External memory address: point out the address of the page on external memory.
        Insert picture description here
  • Page fault interrupt mechanism
    • In the request paging system, when the accessed page is not in the memory, a page fault interrupt is generated, requesting the OS to transfer the missing page into a free block of memory. If there is no free block, a certain page needs to be replaced and the corresponding page table is modified at the same time Item.
    • The difference between page fault interrupt and general interrupt:
      • Generate and process interrupt signals during instruction execution. The page fault interrupt should be handled immediately.
      • During the execution of an instruction, multiple page fault interrupts may occur.
  • Address conversion agency
    • The address conversion mechanism in the request paging system is formed by adding some functions on the basis of the address conversion mechanism of the paging system, such as generating and processing page fault interrupts, and swapping out a page from the memory.
      Insert picture description here

2. Request memory allocation in paging

When allocating memory for a process, three issues are involved: (1) To ensure that the process can run normally, the minimum number of physical blocks required is determined; (2) What memory allocation should be adopted when allocating physical blocks for each process Strategy, that is, whether the physical block is fixed or variable; (3) When allocating physical blocks to different processes, whether to use an even allocation algorithm or to allocate proportionally according to the size of the process.

  • Determination of the minimum 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. The process should obtain the minimum physical fast number, which is related to the hardware structure of the computer and depends on the format, function and addressing mode of the instruction.
  • Memory allocation strategy
    • In the request paging system, two memory allocation strategies can be used, namely fixed and variable allocation strategies. Two strategies can also be adopted in the replacement, namely global replacement and local replacement. So three applicable strategies can be combined:
      • Fixed allocation local replacement
      • Variable allocation global permutation
      • Variable allocation local permutation
    • Fixed allocation: The number of physical blocks allocated to the process is fixed and immutable.
    • Variable allocation: The physical speed allocated to the process is not fixed, but changes with the runtime of the process.
    • Global replacement: When a page fault occurs, one of all the free physical blocks reserved by the OS is selected and swapped in.
    • Partial replacement: When sending a page fault, only one of the n physical blocks allocated to the process can be selected to be swapped out.
  • 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:
    • Average allocation algorithm-that is, all the physical blocks available for allocation in the system are evenly allocated to each process.
    • Proportional allocation algorithm-that is, physical blocks are allocated proportionally according to the size of the process.
    • Prioritized allocation algorithm-In practical applications, in order to take care of important and urgent tasks to be completed as soon as possible, more memory space should be allocated for it. The usual method is to divide all the physical blocks available for allocation in memory into two parts: one part is allocated to each process proportionally; the other part is allocated according to the priority of each process, and the corresponding share is appropriately increased for high-priority processes. . In some systems, such as an important real-time control system, each process may be assigned its physical block completely according to priority.

3. Page loading strategy

To enable the process to run normally, the part of the program to be executed and the page where the data is located must be transferred to the memory in advance. There will be three questions at this time: (1) When should the system load the required pages (2) Where should the system load these pages (3) How to load them.

  • When to load the page
    • In order to determine when the system loads the missing page table into the memory when the process is running, the following two methods can be used:
      • Pre-paging strategy: Pre-load the pages that are expected to be accessed in the near future into the memory. The pages of the process are generally stored in a continuous area of ​​the external memory. Loading several adjacent pages at once is more efficient than loading one page at a time. However, if most of the loaded pages are not accessed, memory is wasted.
      • Request paging strategy: When a page fault occurs during the process of running, a request is made immediately, and the system will load the page fault into the memory. However, this strategy only loads one page at a time, which costs a lot of system overhead and increases the frequency of starting disk I/O.
  • Where to load the page from
    • When a page fault request occurs, where does the system transfer the page fault into the memory, it can be divided into the following three situations
      • The system has enough space in the swap area: at this time, all the required pages can be transferred from the swap area to improve the paging speed. To do this, it is necessary to copy the files related to the process from the file area to the swap area.
      • The system lacks enough space for swap area: At this time, all files that will not be modified are directly imported from the file area; and when these pages are swapped out, they do not need to be rewritten to disk because they have not been modified. (Swap out), when you import it later, you will still import it directly from the file area. But for those parts that may be modified, they must be transferred to the swap zone when they are swapped out, and then transferred from the swap zone when needed later.
      • UNIX mode: All pages that have not been run are loaded from the file area. Pages that have been run but have been swapped out are all placed in the swap area, so the next time they are loaded, they will be transferred from the swap area. At the same time, the UNIX system runs page sharing, so the page requested by some processes may already be in memory.
  • Page loading process
    • Whenever the page to be accessed by the program is not in the memory (the existence bit is "0"), a page fault interrupt is issued to the CPU. The interrupt handler first reserves the CPU environment, analyzes the cause of the interrupt, and then transfers to the page fault interrupt handler. At this time, the program finds the physical block of the page by searching, and then swaps it into the memory.
  • Page fault rate
    • Assuming that the logical space of a process is n pages, the number of physical memory blocks allocated by the system is m (m≤n). If during the running process of the process, the number of successful page access (that is, the accessed page is in memory) is S, 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 f = F / A.

Third, the page replacement algorithm

Page replacement algorithm

4. "Jitter" and working set

1. Multi-programming and "jitter"

  • Multi-programming and processor utilization
    • Since the virtual memory system can logically expand the memory, at this time, it only needs to load part of the program and data of a process to start running. Therefore, people hope to run more processes in the system, that is, increase the degree of multi-programming. In order to improve the utilization rate of the processor.
    • But the actual utilization rate of the processor is as shown in the figure: (The horizontal axis represents the number of processes, and the vertical axis represents the degree of multi-programming. The reason why the utilization rate tends to 0 in the later stages is because the system has jittered .)
      Insert picture description here
  • Causes of "jitter"
    • The root cause of "jitter" is that there are too many processes running in the system at the same time, and thus too few physical blocks are allocated to each process, which cannot meet the basic requirements of the normal operation of the process. As a result, each process is running frequently. When a page fault occurs, the system must be requested to load the missing page into memory. This will increase the number of processes in the system waiting for page transfer in/out.
    • Obviously, the effective access time to the disk has also increased sharply, causing most of the time of each process to be used for page swapping in/out, and almost no effective work can be done, resulting in processor failure. A situation where the utilization rate drops sharply and tends to zero.

2. Working set

  • Basic concepts of working set
    • The time interval at which the process page fault rate occurs is related to the number of physical blocks obtained by the process.
      Insert picture description here
    • In 1968, Denning proposed the working set theory.
  • Definition of working set
    • The working set refers to the set of pages actually visited by the process in a certain time interval ∆.
    • Denote the working set of the process at time t in a certain time interval ∆ as w(t, ∆), and the variable ∆ is called the “window size” of the working set.
    • For a given page orientation, if ∆ = 10 storage accesses, the working set at t1 is W(t1,10)=(1,2,5,6,7), and at t2, the working set is W( t2,10)=(3,4)
      Insert picture description here
  • Features of working set
    • The size of the working set varies.
    • Relatively stable phases and fast-changing phases alternate.
    • According to the principle of locality, the process will be relatively stable on the working set composed of certain pages for a period of time.
    • When the location of the local area changes, the working set size changes rapidly.
    • When the working set window slides over these pages, the working set stabilizes in a localized stage.

3. "Jitter" prevention methods

  • Take a local replacement strategy . That is, when there is a page fault, it can only be replaced in the memory allocated to itself, and it is not allowed to obtain new physical blocks from other processes, so even if the process is jittered, it will not affect other processes. But the effect is not very good. After a certain process is jittered, he will often go to the io waiting queue of the disk, which will delay the processing time of the interruption of personnel in other process areas.
  • Incorporate the working set algorithm into the processor scheduling . When the scheduler finds that the processor utilization is low, it will try to transfer a new job from the external storage into the memory to improve the utilization of the processor. After incorporating the working set algorithm, before the scheduler transfers the job from external storage, it is necessary to check whether each process has enough memory-resident pages. If there are enough, the new job can be transferred to the memory at this time, otherwise, it will not be transferred.
  • Use the "L=S" criterion to adjust the page fault rate . Denning proposed the "L=S" criterion in 1980 to adjust the degree of multi-programming, where L is the average time between page faults, and S is the average page fault service time, that 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 and S are close, both the disk and the processor can reach their maximum utilization. Both theory and practice have proved that using the "L=S" criterion is very effective for adjusting the page fault rate.
  • Select the suspended process . When the degree of multi-program is too high, the utilization of the processor has been affected. In order to prevent "jitter" from occurring, the system must reduce the number of multi-programs. At this time, a process is selected according to a certain algorithm and the process is suspended.

Five, request segmented storage management method

The request segmented storage management mode and the request paging management mode are very similar in implementation principles and required hardware support. It's just that swap-in and swap-out are performed in units of segments.

1. Request hardware support in segment

Similar to the request paging system, the hardware support required in the request segmentation system includes a segment table mechanism , a missing segment interrupt mechanism , and an address conversion mechanism .

  • Request segment table mechanism
    • The main data structure required in the request segmentation system is the request segment table, which is expanded on the basis of the segment table.
      • Access mode: Access attributes (execution, read-only, read/write allowed).
      • Access field A: record how often the segment is accessed.
      • Modification bit M: Indicates whether the segment has been modified after entering the memory.
      • Existence bit P: Indicates whether the segment is in memory.
      • Additional bit: Indicates whether the segment has been dynamically increased during operation.
      • External memory address: Indicates the starting address of the segment in external memory.
        Insert picture description here
  • Missing Interruption Agency
    • Similar to the page fault interruption mechanism, the request segmentation system uses a request segmentation strategy. Whenever it is found that the segment to be accessed by the running process has not been transferred into the memory, the missing segment interrupt mechanism generates a missing segment interrupt signal. After entering the OS, the missing segment interrupt handler will transfer the required segment into the memory.
      Insert picture description here
  • Address conversion agency
    • The address conversion mechanism in the request segmentation system is formed on the basis of the address conversion mechanism of the segmentation system. Because the accessed segments are not all in the memory, if you find that the segment you want to access is not in the memory during address conversion, you must first transfer the missing segment into the memory and modify the segment table before you can use the segment table for address conversion. . For this reason, some functions have been added to the address conversion mechanism, such as the request and processing of missing segment interrupts.
      Insert picture description here

2. Segment sharing and protection

  • Shared segment table
    In order to realize segment sharing, a shared segment table can be set up in the system. All shared segments occupy an entry in the shared segment table. The contents of the shared segment table are as follows:
    Insert picture description here
  • Allocation and recovery of shared segments
    • Allocation of shared segment: When allocating memory for the shared segment, the system allocates a physical area to the process that first requests the use of the shared segment, and then adds an entry to the shared segment table, and modifies and adds corresponding data. For subsequent processes that apply to use the shared segment, you only need to add one item to the process segment table, and add and modify the corresponding data.
    • Reclamation of the shared segment: When a process sharing this segment no longer needs the segment, the segment is released, including the cancellation of the corresponding entry in the process segment table. At the same time, modify the data of the shared segment table. When no process calls the segment, the operating system reclaims the segment.
  • Section protection
    In a section system, since each section is logically relatively independent, it is relatively easy to implement information protection. At present, the following measures are often used to ensure information security.
    • Out-of-bounds inspection
    • Access control check
    • Environmental Protection Agency

Six, summary

Insert picture description here

Guess you like

Origin blog.csdn.net/pary__for/article/details/114538801