This article analyzes why the operating system is included in the process address space?

Let’s talk about the small matter of process address space today. When it comes to the address space of a process, everyone may know such a picture:

This picture is the so-called process address space after the Linux program is running. It includes the familiar code area, data area, heap area and stack area. Today we will not explain these areas, but focus on the top of this address space A piece of the area - the kernel, the question here is: Why should the operating system (kernel) be included in the process address space? To know the answer to this question, you need to know exactly how the operating system manages memory. Most modern operating systems use the virtual memory system to manage memory. As we can see in the figure above, this continuous memory area is actually just an illusion. Such a memory layout does not necessarily exist in the physical memory. Using the virtual memory system, some non- Contiguous blocks of memory (pages) are mapped to a contiguous address space—that is, what we see above, which is called virtual memory. The addresses we see are all virtual addresses, and the mapping relationship between physical memory and virtual memory is maintained in the page table . When the CPU executes machine instructions, it needs to convert the virtual address into a physical memory address according to the page table, but this process is difficult for the programmer. It is said to be transparent, we cannot see such a conversion process. So why does the kernel map itself into the address space of the process? We know that the CPU has a permission state when executing instructions. The x86 processor has 4 permission states. The operating system generally uses two of them. This is the so-called user state and kernel state. The programs we write run in the user state. The system runs in kernel mode.

In some scenarios, we need the help of the operating system to read and write files, send and receive network data, etc., that is, to call the services provided by the operating system. This process is the so-called system call. We have said more about system calls in previous articles As I explained last time, in the system call scenario, it involves switching from user mode to kernel mode. In addition, there are other scenarios involving switching between user mode and kernel mode, such as interrupt handling and exception handling. Now that you know that our program needs to switch between user mode and kernel mode frequently when running , the rest is simple.

 Information through train: Linux kernel source code technology learning route + video tutorial kernel source code

Learning through train: Linux kernel source code memory tuning file system process management device driver/network protocol stack

If the kernel and user mode programs are located in different address spaces, then when the user mode and kernel mode are switched, the page table switching is bound to be involved - from the user mode to the kernel mode, the page table of the user process needs to be switched to the kernel page table, However, if you exit from the kernel mode and return to the user mode, it will involve switching the kernel page table to the user process page table . Switching the page table is not a small overhead for the computer system. And if the kernel and the user mode program are located in the same address space, then the above-mentioned overhead of page table switching can be avoided, which is an important reason why the kernel maps itself to the process address space. Well, that's all for this article, I hope it will be helpful for everyone to understand the process address space.

 

 

Guess you like

Origin blog.csdn.net/youzhangjing_/article/details/130131940