Operating system---------memory management

Hierarchical management of storage

First, let's look at the storage hierarchy in the computer system. As shown in the figure below, the storage hierarchy should have at least three levels: CPU registers, main storage, and auxiliary storage. It can also be subdivided into registers, caches, main memory, disk caches, fixed disks, and removable storage media according to specific functions. In the storage hierarchy, the higher the level (closer to the CPU), the faster the access speed of the storage medium, the higher the price, and the smaller the capacity configured in the relative computer.
Insert picture description here

  1. Main storage: This is also the main object of storage management (main storage in the figure above);
  2. Cache: In order to obtain better processing performance, some main memory programs and data (especially key performance data) are transferred to the cache, and storage management needs to manage them, even including the management of Lenovo memory (fast table);
  3. Auxiliary storage: In order to obtain a larger virtual address space, storage management needs to manage virtual storage files stored on hard disks and removable storage media.

Memory allocation can be roughly divided into several types:

  1. Partition allocation
  2. Buddy system
  3. Virtual storage (paged storage mode, segmented storage mode, segmented storage mode)

Preface to Virtual Storage:

First of all, we have to understand what is the premise of the establishment of virtual storage? It should be because the program presents a local law in the execution process , that is, in a continuous period of time, the execution of the entire program is limited to a certain part of the program. Correspondingly, the storage space accessed by execution is also limited to a certain memory space.

The principle of locality and were as follows: temporal locality and spatial locality .

  1. Time locality means that once an instruction in the program is executed, the instruction may be executed again soon (loop statement);
  2. Spatial locality means that once a program accesses a certain storage unit, the nearby storage unit will also be accessed soon.

Characteristics of virtual storage

  1. Repeatability (the most basic feature ): It is the most important feature of virtual storage. Refers to a job that is divided into multiple memory operations.
  2. Exchangeability: Refers to allowing exchange in and out during operation. Swap in and out can improve memory utilization
  3. Virtuality (the most essential feature): 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 an 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.


(First understand the knowledge points of paging storage, please click! )

Request paging storage management method

(Here is mainly about this paging storage, the difference between segmentation is not big, you can understand it yourself)

Request hardware support in paging

Request page table mechanism:

Four fields have been added to the request page table
Insert picture description here

  1. Status bit (existing bit) P: Indicates whether the page has been loaded into the memory.
  2. Visited field A: Record the number of visits to this page in a period of time, or how long it has not been visited recently, to provide a reference for the replacement algorithm to select a replacement page;
  3. Modification bit M: Indicates whether the page has been modified after being loaded into the memory.
  4. External memory address: indicates the address of the page in external memory, usually the physical block number.

Page fault interruption mechanism:
In the request paging system, whenever the page to be accessed is not in the memory, a page fault interrupt is generated, requesting the OS to load the missing page into the memory. 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. But the page fault interrupt is a special type of interrupt. Compared with the general interrupt, it has obvious differences, mainly in the following two aspects:

  1. Generate and process interrupt signals during instruction execution. That is, it is generated and processed when it is found that the instruction or data to be accessed is not in the memory during the execution of the instruction.
  2. During the execution of an instruction, multiple page fault interrupts may occur. And to ensure that it can finally return to the instruction that generated the page fault interrupt before the interrupt to continue execution.

Address conversion agency
Insert picture description here
(Specifically, when it is implemented, it is actually realized by using the page table, because the page table records the conversion from page number to block number)

When retrieving the block table and page table, the modification position must be set to "1" for the write command.

Memory allocation in request paging

Determination of the minimum number of physical blocks:

The minimum number of physical blocks mentioned here refers to the minimum number of physical blocks required to ensure the normal operation of the process. The minimum number of physical blocks that a process should obtain is related to the computer's hardware structure 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 adopted, namely fixed and variable allocation strategies. When performing permutation, two strategies can also be adopted, namely global permutation and local permutation. Therefore, the following three applicable strategies can be combined.

  1. Fixed allocation of local replacement.
    The so-called fixed allocation refers to the allocation of a certain number of physical blocks for each process, which will not change during the entire running period. The so-called partial replacement means that if a process finds a page fault during operation, only one page can be selected from the n pages allocated to the process to be swapped out, and then another page is transferred to ensure the memory allocated to the process The space remains unchanged. The difficulty in implementing this strategy is that it is difficult to determine how many physical blocks should be allocated to each process.
  2. Variable allocation global replacement.
    The so-called variable allocation refers to the first allocation of a certain number of physical blocks for each process in the system. During the process of operation, it can be appropriately increased or decreased according to the situation. The so-called global replacement means that when a process finds a page fault, it will take out a free physical block reserved by the OS (generally organized as a free physical block queue) and allocate it to the process, or use all physical blocks as the target and select one for replacement , And then load the missing page.
    When this strategy is adopted, all processes that generate page faults (interrupts) will obtain new physical blocks. Only when the physical blocks in the free physical queue are used up, the OS can select a page from the memory to be called out, and The physical block owned by the process to which the page selected to be called belongs will decrease, resulting in an increase in its page fault rate.
  3. Variable allocation local replacement.
    This strategy allocates a certain number of physical blocks for each process, but when a process finds a page fault, it only allows one page 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 has page fault interrupts during operation, the system must allocate a number of additional physical blocks for the process until the page fault rate of the process is reduced to an appropriate level; on the contrary, if a process is running The page rate is particularly low, then the number of physical blocks allocated to the process can be appropriately reduced at this time, but it should not cause a significant increase in its page fault rate.

Physical block allocation algorithm
When using a fixed allocation strategy, how to allocate physical blocks uses the following algorithm:

  1. Even distribution algorithm.
  2. Proportional allocation algorithm, that is, an algorithm that allocates physical blocks proportionally according to the size of the process.
  3. Consider the priority allocation algorithm. 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.

Page loading strategy

When to load the page:

  1. Pre-paging strategy. Load several adjacent pages at once.
    ①It can be used for the first transfer of the process. ②Use in a system that uses working sets.
  2. Request paging strategy.
    Called in when the process requests a page fault during operation. This strategy only loads one page at a time, so it costs a lot of system overhead and increases the startup frequency of disk I/O.

Where to load the page from:

  1. The system has enough space in the swap area. At this time, all required pages can be transferred from the swap area to increase the paging speed. For this reason, before the process runs, the files related to the process must be copied from the file area to the swap area.
  2. The system lacks enough space in the swap area. At this time, all files that will not be modified are directly imported from the file area; but for those parts that may be modified, they must be transferred to the swap area when they are swapped out. When needed later, transfer in from the swap area.
  3. The UNIX way. All pages that have not been run should be imported from the file area. As for the pages that have been run but have been swapped out, they should be transferred from the swap area the next time they are transferred in because they are placed in the swap area.

Normally, since the swap area uses a continuous allocation method and the file area uses a discrete allocation method, the disk I/O speed of the swap area is higher than that of the file area.

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. 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, the disk I/O is started to load the missing page into the memory, and then the page table is modified. If the memory is full, you must first select a page from the memory to be swapped out according to a certain replacement algorithm; if the page has not been modified (the modification bit is "0"), you do not need to write the page back to the disk; but If this page has been modified (the modification bit is "1"), it must be written back to the disk, and then the missing page is loaded into the memory, and the corresponding entry in the page table is modified, and its existence bit is set to " 1", and write this page table entry into the fast table. After the page fault is transferred into the memory, the modified page table is used to form the physical address of the data to be accessed, and then the memory data is accessed. The loading process of the entire page is transparent to the user.

Page fault rate:

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 visits of the process is A = S + F, then the page fault rate of the process during its operation is
Insert picture description here

The page fault rate is affected by the following factors:

  1. Page size.
  2. The number of physical blocks allocated by the process.
  3. Page replacement algorithm. Therefore, the page fault rate is an important indicator to measure the page replacement algorithm.
  4. The inherent characteristics of the program.

In fact, when the page is missing, the replacement cost should be considered when selecting the page to be replaced, such as whether the page has been modified.


Page replacement algorithm


Jitter

Jitter: In virtual memory, pages are frequently scheduled between memory and external memory, so that the time required to schedule pages is more than the actual running time of the process. At this time, the system efficiency drops sharply, and even causes the system to crash. This phenomenon is called turbulence or jitter.

Reasons for jitter:

  1. The page elimination algorithm is unreasonable
  2. The number of physical pages allocated to the process is too small

Guess you like

Origin blog.csdn.net/weixin_43743711/article/details/115008967