Operating System (2)-Memory Management

1. Introduction to memory management

Memory is a very important resource for computers. In general computer storage structures, there is usually a three-tier structure of cpu-cache-main memory. The memory management of the operating system is mainly responsible for the allocation and recovery of memory, because Java relies on the virtual machine to have Automatic allocation and recovery, but there are malloc allocation functions and free memory release functions in C/C++.

In addition, in the early memory management, there is no memory abstraction, that is, the program directly accesses and manipulates the physical memory, which causes any program to access the memory, and it is difficult to run multiple programs. In the back, there is address abstraction, and the memory Management needs to convert the logical address to a physical address

Two, common memory management mechanism

Common memory management methods include continuous allocation management and non-continuous allocation management. Continuous management refers to the allocation of a continuous memory space for a user program. The continuous management allocation methods include single continuous allocation and fixed partition allocation, which is block management. And so on, and continuous allocation will produce a lot of fragments, so there seems to be non-continuous allocation management such as page management, segment management, and segment page management

  • 块式管理(连续): Divide the memory into one piece. If the program needs to use the memory, the operating system will allocate one piece to it. If the running program is very small, a large part of the allocated memory is wasted, which is easy to generate fragments.
  • 页式管理: Divide the main memory into the same size and fixed page one page, the page is relatively small, and then divide the program process into a piece of logical address space into pages, and then use the page table to match the page number of the program to the main memory Real physical address in
  • 段式管理: Page management manages memory according to pages (fixed size), while segment management is divided into segments according to the content of the program code or process functions, and then the virtual address is converted to the actual memory physical address through the address mapping mechanism
  • 段页式管理: Segment and page management The advantages of centralized segment and page management. Simply put, the program is first divided into segments, and then each segment is subdivided into one page and one page, which further improves the utilization of memory

The similarities and differences between paging and segmentation:

  • The paging mechanism and the segmentation mechanism are both to improve memory utilization and reduce fragmentation. Both use discrete allocation methods, so it is necessary to maintain an address translation mechanism.
  • The page size is fixed, but the segment is not fixed. Paging only meets the needs of operating system memory management. Since the segmentation is segmented according to the program code, the logic of the program is better reflected and meets the needs of users.

Three, fast table and multi-level page table

When allocating memory management, there are two important points:

  • The transformation from virtual address to physical address should be fast
  • The page table (the mapping table between virtual addresses and logical addresses) may be very large and occupy space

The fast table and multi-level page table solve the above two problems respectively

Quick table

The page table exists in memory, which allows the CPU to pass through the memory twice when accessing data:

  1. Get the physical address of the corresponding access data from the page table in the memory
  2. Really access data from physical address

This causes the processing speed of the CPU to be reduced by 1/2. In order to increase the address conversion speed, a fast table is proposed, and a fast table entry is added to the internal cache of the CPU. The content is the same as the content in the page table. Then the CPU can find the physical address of the corresponding data directly inside the CPU, and the speed will be greatly increased. The following is the process of the CPU address conversion after the quick table:

  • Find the physical address in the fast table according to the page number of the virtual address
  • If it hits, take out the physical address directly from the fast table
  • If it does not hit, access the page table in the memory, and at the same time need to write the page table entry back to the fast table in the CPU

In fact, the fast table is the caching strategy we often use

Multi-level page table

Multi-level page table is to prevent all page table entries from being placed on the same page table, which causes the memory occupied by the page table to be too high. Multi-level page table is a typical space for time.

Four, CPU addressing mode

Instruction: Opcode operand

There are seven addressing modes for 8088/8086CPU:

  • 立即寻址: The address of the operand in the memory is directly stored in the instruction, immediately after the opcode, it is stored in the code as a part of the instruction, such as MOV AL,66H

  • 直接寻址: The address of the operand in the memory is the 16-bit offset of the operand address plus the segment base address mov ax,[4000h]

  • 寄存器寻址: The address of the operand in the memory is the content of the register, and the operand needs to be fetched from the register mov ax, bx

  • 寄存器间接寻址: The address of the operand in the memory is the offset address of the register plus the base address to form mov ax, [bx]

  • 寄存器相对寻址: The address of the operand in the memory is composed of the contents of the register plus a given offset

  • 基址-变址寻址: The address of the operand in the memory is added by the contents of a base address register and the contents and base address of an index register

Five, virtual memory technology

A 20G game can run on an 8G computer. The principle used is the virtual memory technology. The virtual memory technology is not loaded into the memory for the data that is not used or temporarily not used during the running of the program, and waits until it is needed. Load, and the principle of locality is the basis of virtual memory technology

  • 时间局部性: Once an instruction in the program is executed, the instruction may be executed again soon, if a certain data is accessed, the data may be accessed again soon
  • 空间局部性: Once the program accesses a memory unit, a nearby memory unit will also be accessed soon

The virtual memory technology is based on the discrete memory management mechanism. According to the three methods of discrete memory management: page management, segment management, and segment page management, there are also three types of virtual machine memory technologies, focusing on the request page Management

  • 请求页式管理: Based on paging, in order to support virtual memory technology, additional functions of request paging and page replacement are added. Request paging is currently the most commonly used method to implement virtual storage. Before the job runs, request paging storage management only loads the pages currently needed. If the required page is found not in the memory during the program running, it will be generated. When a request is interrupted, the CPU uses the page replacement algorithm to load the corresponding page into the memory, and at the same time, it also calls out the unnecessary pages into the memory.

Six, page replacement algorithm

Because the virtual memory technology only transfers part of the currently needed pages into the memory, but if the page to be accessed is not in the memory during the running of the program, but the memory has no available space, it is necessary to transfer the required pages, Unnecessary pages are called up, so there is the following page replacement algorithm

  • 最佳置换算法: First of all, this is an ideal algorithm. The best replacement algorithm eliminates pages that will never be used or will not be accessed for the longest time, but the CPU cannot predict which page will not be used for the longest time in the future, so this The algorithm is just an ideal algorithm
  • 先进先出算法: This is a simple algorithm that always eliminates the page that enters the memory first, that is, selects the page with the longest resident time in the memory for elimination
  • 最近最久未使用算法(LRU): The best replacement algorithm is to judge the nearest future, and the least recently used algorithm is to judge the recent past. The LRU algorithm needs to record the elapsed time T for each page since the last time it was accessed. When the page needs to be eliminated, select Eliminate the largest T value

Guess you like

Origin blog.csdn.net/weixin_44706647/article/details/115284350