Home >Operation and Maintenance >Linux Operation and Maintenance >What does linux use to implement virtual memory?
The implementation of virtual memory needs to be based on the discretely allocated memory management method. There are three implementation methods: 1. Request paging storage management method; 2. Request segmented storage management method; 3. Segment page storage management method. No matter which method is used, certain hardware support is required: 1. A certain capacity of memory and external memory; 2. Page table mechanism (or segment table mechanism), as the main data structure; 3. Interrupt mechanism, when the user program needs If the accessed part has not yet been transferred into the memory, an interrupt will occur; 4. Address conversion mechanism, conversion from logical address to physical address.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
Traditional storage management keeps multiple processes in memory at the same time to allow multi-programming. They all have the following two common characteristics:
- One-time: The job must be done onceAfter all is loaded into the memory, to start running. This will lead to two situations:
1) When the job is large and cannot be loaded into the memory, the job will be unable to run;
2) When a large number of jobs are required to be run, due to insufficient memory, For all jobs, only a few jobs can be run first, resulting in a decrease in multiprogramming.- Residency: After the job is loaded into the memory, it will always resident in the memory, and no part of it will be swapped out, Until the job runs to the end. A running process will be blocked waiting for I/O and may be in a long-term waiting state.
Therefore, many programs (data) that are not used during program operation or are not used temporarily occupy a large amount of memory space, and some jobs that need to be run cannot be loaded and run, which obviously wastes precious memory. resource.
1.1 Definition and characteristics of virtual memory
Based on the localityprinciple, when the program is loaded , you can load part of the program into the memory , and leave the rest in external memory, and you can start the program execution. During the execution of the program, when the accessed information is not in the memory, the operating system will transfer the required part into the memory , and then continue to execute the program. On the other hand, the operating system swaps out the temporarily unused content in the memory to external storage, thereby freeing up space to store the information that will be transferred into the memory. In this way, since the system provides
partial loading, request transfer and replacement functions (completely transparent to the user), the user feels as if there is a Memory that is much larger than actual physical memory is called virtual memory. The
size of virtual memory is determined by the address structure of the computer, and is not a simple sum of memory and external memory.
Virtual storage has the following three main characteristics:
Virtual memory allows a job to be transferred into the memory multiple times
.When the continuous allocation method is used, a considerable part of the memory space will be in a temporary or "permanent" idle state, resulting in a serious waste of memory resources, and it is impossible to logically expand the memory capacity.
Therefore, the implementation of virtual memory needs to be based on the discrete allocationmemory management method. There are three ways to implement virtual memory:
Request pagingA certain amount of memory and external storage.
Continuous allocation method: refers to allocating a continuous memory space for a user program.
- Fixed partition allocation: Divide the memory space into several fixed-size areas. Only one job is loaded into each partition, and multiple jobs can be executed concurrently. Lack of flexibility will produce a large amount of internal fragmentation, and memory utilization is very low.
- Dynamic partition allocation: Dynamicly allocate memory space to the process according to its actual needs. When a job is loaded into memory, the available memory is divided into a continuous area for the job, and the size of the partition is exactly suitable for the size of the job. A lot of external debris will be generated.
##Discrete allocation method: Load a process into discrete distribution To many non-adjacent partitions, you can make full use of memory.
The concept of paging storage:Pages, page frames and blocks.
- The block in the process is called page or page (Page) , with a page number; The block in the memory is called Page Frame (Page Frame, page frame = memory block = physical block = physical page ) , has a page frame number. External storage is also divided in the same unit, directly called Block. When a process is executed, it needs to apply for main memory space, which means that each page must be allocated an available page frame in the main memory, which creates a one-to-one correspondence between pages and page frames. Each page does not have to be stored consecutively and can be placed in non-adjacent page frames.
- Address structure: The first part is the page number P, and the latter part is the page offset W. The address length is 32 bits, of which bits 0~11 are the intra-page address, that is, the size of each page is 4KB; bits 12~31 are the page number, and the address space allows up to 2^20 pages.
- Page table. In order to facilitate finding the physical block corresponding to each page of the process in the memory, the system establishes a page table for each process to record the physical block number corresponding to the page in the memory, Page table Generally stored in memory. After the page table is configured, when the process is executed, the physical block number of each page in the memory can be found by looking up the table. It can be seen that the role of the page table is to implement the address mapping from the page number to the physical block number.
Request paging It is currently the most commonly used method to implement virtual memory.
The request paging system is based on thebasic paging system. In order to support the virtual memory function, the request paging function and page replacement## are added. #Function. In the request paging system, only a part of the currently required pages are required to be loaded into the memory before the job can be started.
During the execution of the job, when the page to be accessed is not in the memory, it can be brought in through the paging function. At the same time, the temporarily unused pages can also be swapped out to external memory through the replacement function to free up memory. space.
In order to implement request paging, the system must provide certain hardware support. In addition to a computer system that requires a
certain capacity of memory and external storage, it also needs a page table mechanism, page fault interrupt mechanism, and address conversion mechanism.
2.1 Page table mechanism #The page table mechanism of the request paging system is different from the basic paging system. The request paging system does not start before a job is run. All are required to be loaded into memory at once.
Therefore, during the running process of the job, there will inevitably be a situation where the page to be accessed is not in the memory. How to detect and deal with this situation are two basic problems that the request paging system must solve. To this end, four fields are added to the request page table entry, which are:
Page table entry in the request paging systemPhysical block number | Status bit P | Access field A | Modify bit M | External storage address |
7 | 0 | 1 | 2 | 0 | 3 | 0 | 4 | 2 | 3 | 0 | 3 | 2 | 1 | 2 | 0 | 1 | 7 | 0 | 1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
7 | 7 | 7 | 2 | ##2 |
2 |
##2 |
#7 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
0 |
0 |
0 | 0 |
##0 | ##Physical Block 3 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1 |
3 | 3 |
##3 |
|
1 |
##1 | √ |
√ |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
##√ | ##√ | ## It can be seen that the number of page fault interruptions is 9, and the number of page replacements is 6. 3.2 First-in-first-out (FIFO) page replacement algorithm The page that enters the memory earliest is eliminated first, that is, The page that has resided longest in memory. The algorithm is simple to implement. You only need to link the pages transferred into the memory into a queue according to the order, and set a pointer to always point to the earliest page. However, this algorithm is not suitable for the actual running time of the process, because some pages are frequently accessed during the process.
|
The above is the detailed content of What does linux use to implement virtual memory?. For more information, please follow other related articles on the PHP Chinese website!