Illustrate the virtual memory mechanism and memory mapping in Linux

Virtual memory can be said to be a perfect interaction between hardware exceptions, main memory, external memory, and the operating system, and even better, this mechanism is completely automatic. If we understand a little bit of the principle of virtual memory, we can understand the causes of memory errors that often occur, and we can also understand what memory mapping mmap is.
In-depth detailed explanation of the Linux kernel network structure and distribution of the
Linux kernel, the realization of the process scheduler, the complete fair scheduler CFS

1. What is the physical address space

Understanding the virtual address space has to start with the physical address space. We know that memory is like an array. Each storage unit is assigned an address. This address is the physical address, and the set of all physical addresses is the physical address space. The physical address is also the real address, corresponding to the real memory bank.

If the CPU uses a physical address to address the memory, it is as follows. The address in this instruction is the address where the data is actually stored.
Insert picture description here

Second, what is the virtual address space

After the introduction of virtual addresses, for each process, the operating system provides an illusion that makes each process feel that it has a huge continuous memory to use, and this virtual space can even be larger than the capacity of the memory. This "illusion" is the virtual address space. The virtual address is for each process, it is just an "illusion".

At this time, the CPU uses the virtual address to address the memory, and converts the virtual address to the real physical address (address translation) through the dedicated memory management unit (MMU) hardware. The operating system is responsible for maintaining the mapping relationship between the virtual address and the physical address on the page. In the table. Insert picture description here
The address in the instruction is not the address where the data is actually stored

3. Procedures and processes

When we finish writing the code, compiling, linking and generating the executable file, the thing we get is a collection of binary codes. We call this thing a program and store it on the disk. Only when we execute this file, the program will be read by the operating system into the memory to run, but note that the system does not read all the programs into the memory. We call the running program a process.

From the perspective of the process, my data and code are stored in a continuous space, and each area has a different function. Typical examples are the area where the code is stored and the area where the data is stored. Insert picture description here
However, we know that this is just an illusion. The address in the code and data is a virtual address, and an address translation is needed to get the real physical address.

Four, paging, page table and page fault exception

The mapping relationship between virtual addresses and physical addresses is based on "pages". Paging is to divide the entire virtual memory and physical memory into fixed-size blocks, with a page as the smallest unit of mapping. At runtime, the CPU requests a virtual address, and the virtual address is translated into a physical address to determine where the data is in the memory. The following page table records the mapping relationship of each page of the virtual memory of this process. Insert picture description here
Insert picture description here
When the CPU is addressing, there are three possibilities for this mapping.

Unallocated : The page where the virtual address is located has not been allocated, which means that there is no data associated with them, and this part will not occupy memory.
Uncached : The page where the virtual address is located is allocated, but it is not in memory.
Cached : The page where the virtual address is located is in memory.
When accessing an uncached area, the system will generate a page fault interrupt, and then the process is blocked, waiting for the operating system to copy the missing page from the disk to the memory. When the copy is complete, the CPU continues to execute the instruction that caused the page fault interrupt, and it will execute normally at this time. This strategy of copying pages to memory only when needed is called scheduling pages on demand. It is conceivable that when the program is loaded into the memory, only a small part of the content is put into the memory at the beginning. The program is constantly missing pages while it is running, and constantly copies the required parts into the memory.
It can also be seen from the above figure that virtual memory is actually a disk cache. The system carefully maintains the virtual address illusion of each process through the mechanism of page fault interruption.

Five, the application of virtual memory

In Linux, associate a piece of virtual memory with an object on a disk, and initialize the piece of virtual memory with the object on the disk. This mechanism is called memory mapping.

1. Simplify the sharing of resources

When we use shared library functions, such as printf(), there is no need to copy a copy of the code for each process, which is a waste of memory. We only need to map a piece of virtual memory of each process to the same object. Insert picture description here
Suppose process 1 and process 2 want to share the same file. Among them, file A has been mapped to process 2 (the file is cached in memory), and process 2 is still the file A loaded by page fault interruption.
Insert picture description here
At this time, process 1 also opens file A. Since the name of the file is unique in the system, the operating system knows that file A has been cached in memory. Therefore, the system maps the virtual memory of process 1 to the same memory to complete file sharing. Insert picture description here
2. An implementation of zero-copy technology

For read(), the most commonly used I/O function in Linux, the file is first copied to the kernel space buffer by the system, and then copied to the user space. It is obviously not what we want to read a file and need to copy twice, especially when reading a large file, sad...

Insert picture description here
Through memory mapping, we can bypass the kernel buffer and directly map file A to virtual memory. Here, a total of one copy occurs, nice~Insert picture description here

Six, summary

Virtual memory is working for us all the time, and we can work automatically without any intervention. Virtual memory can be seen as a cache to the disk, which triggers the operating system to handle the problem of accessing uncached blocks through page fault interrupts. Virtual memory can be used to deal with shared objects and reduce I/O overhead.

In short, virtual memory is very powerful. What I have introduced above is only part of the function of virtual memory. If there is an error in the article, please point it out. I hope this article is useful to you.

Linux, C/C++ technology exchange group: [960994558] I have compiled some learning books, interview questions from major companies, and technical teaching video materials that I think are better. You can add them if you need them! ~
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_52622200/article/details/113182530