Let's run Linux kernel + memory management from the perspective of memory distribution
[Copy link]
In the Linux operating system, the virtual address space is divided into two parts: kernel space and user space. For 32-bit systems, the kernel space occupies 1G and is located at the highest point, and the remaining 3G is user space. For 64-bit systems, the kernel space and user space are both 128T, occupying the highest and lowest points of the entire memory space respectively, and the remaining middle part is undefined.
The page size is generally 4KB. The memory management system mainly does three things: first, the virtual memory space is divided into pages of equal size; second, the physical memory is also divided into pages of equal size; finally, the virtual memory and physical memory are converted through a mapping relationship. When a process needs memory, the memory management system will allocate virtual memory space for it and map it to physical memory when needed. When memory is tight, some memory pages can be swapped out to the hard disk to free up physical memory for other processes to use.
Specifically, the book "Run, Linux Kernel" may explore in depth the implementation details of memory management in the Linux kernel, including how to manage virtual address space and physical memory, how to perform memory mapping and page replacement, etc. By studying and practicing these contents, readers can have a deeper understanding of the memory management mechanism of the Linux kernel, laying the foundation for subsequent Linux kernel development and optimization.
In Linux systems, memory distribution mainly involves the division of kernel space and user space. Kernel space contains kernel code and data structures, responsible for the core functions of the system; while user space is where user applications run. This division ensures the stability and security of the system.
When looking at memory management from the perspective of a process, we focus on the layout of the process in memory. In Linux, applications usually exist in the form of ELF (Executable and Linkable Format) files. ELF files contain various segments of the program, such as code segments, data segments, and uninitialized data segments. These segments are mapped to the corresponding address space when the program is loaded into memory.
The code segment stores the machine instructions of the program and is the core part of program execution. The data segment stores initialized global variables and static variables, which are visible during program execution. The uninitialized data segment is used to store uninitialized global variables and static variables, which are initialized to default values when the program is loaded.
Understanding the role and layout of these segments helps us better understand the behavior of processes in memory and how the operating system manages memory for processes. By properly allocating and managing memory resources, the operating system can ensure the normal operation of programs and improve the overall performance of the system.
Process management is a crucial part of modern operating systems, among which memory management is one of the core issues. Since all processes can theoretically access physical memory, this brings up the issue of process address space protection. Malicious programs or unintentional errors may cause one process to modify the memory data of another process, causing system instability or crash. At the same time, traditional memory management mechanisms also have challenges in terms of memory usage efficiency and program address relocation.
To solve these problems, the segmentation mechanism came into being. The segmentation mechanism ensures that the address space of each process is independent of each other by mapping the virtual addresses required by the program to different physical address spaces. When a process tries to access a virtual address that is not mapped or does not belong to it, the CPU will capture this out-of-bounds access and reject it, and notify the operating system to handle the exception. This allows the process to not care about the specific layout of physical memory, but only needs to write and access programs according to virtual addresses, thereby improving the portability of the program.
The core idea of the segmentation mechanism is to introduce the concept of virtual memory. The addresses seen by the process when it is running are virtual addresses. Through the address mapping mechanism provided by the CPU, these virtual addresses are converted into actual physical addresses. In this way, multiple processes can run at the same time, and the virtual memory space of each process is isolated from each other. The operating system only needs to maintain the mapping relationship between virtual addresses and physical addresses, thereby realizing the protection of process address space, improving memory usage efficiency, and simplifying the address relocation problem in program writing.
As an early memory management mechanism, the dynamic partitioning method can effectively manage memory space in the early stage. However, as time goes by, its inherent problems gradually emerge. First, the dynamic partitioning method easily leads to memory fragmentation. As the process is loaded, executed and unloaded, free areas of varying sizes will be formed in the memory, namely memory fragments. These fragments cannot be effectively used, resulting in a decrease in memory utilization. Secondly, when the dynamic partitioning method needs to run a new process and there is insufficient memory, it is necessary to select an existing process for swapping out. This process is not only complicated, but may also cause process interruption and data loss. In addition, in order to solve the fragmentation problem, the operating system needs to dynamically move processes to ensure that the space occupied by the process is continuous and the free space is also continuous, but process migration is an extremely time-consuming process, which seriously affects system performance.
|