Memory management in Linux environment (2/7)

To deeply understand the stack management mechanism in memory, it is not a good method to analyze it in isolation, because the stack memory is not maintained by the program itself, but is implemented by the cooperation of the operating system, compiler, CPU, and physical memory.

For programs running in the Linux environment, the starting address of the link during compilation is the same, and it is a virtual address. The Linux operating system requires the support of the CPU memory management unit to run. The Linux kernel manages memory through page tables and MMU hardware, completing functions such as conversion of virtual addresses to physical addresses, and management of memory read and write permissions. When the executable file is running, the loader loads different sections in the executable file into areas with different read and write permissions in the memory, such as code segments, data segments, .bss segments, .rodata segments, etc.

There are two main types of programs running on your computer: operating systems and applications. Each application process has a 4GB virtual address space. For the security and stability of the system, the virtual address space of 0~4GB is generally divided into two parts: user space and kernel space. The 0~3GB address space is used by applications, while the operating system generally runs in 3~4GB kernel space. Through memory permission management, applications do not have permission to access the kernel space and can only access the kernel space through interrupts or system calls. This ensures the stable operation of the core code of the operating system to a certain extent.

In the Linux environment, although all programs use the same link address when compiled, when the program is running, the same virtual address will be mapped to different physical memory areas through MMU conversion, and each executable file will be loaded into a different memory area. on the physical page. Each process has its own page table, which is used to record the mapping relationship between virtual addresses and physical addresses in each process.

Through this address management, each process can enjoy an independent and private 3GB user space. When the compiler compiles a program, it does not need to consider the address allocation of each program in actual physical memory. Through memory read and write permission management, the space of each process can be protected from being destroyed by other processes, thereby ensuring the safe operation of the system. Linux's memory management system will automatically help us complete the mapping of virtual space memory to physical memory.

The heap memory is generally behind the BSS segment. As users use malloc to apply for more and more memory, the space continues to grow towards higher addresses, and the stack space is next to the kernel space. ARM uses a full decrementing stack, and the stack pointer will It continues to grow from high addresses in user space to low addresses. In the vast space between the stacks, there is also an area called the MMAP area, and the dynamic shared library uses this address space.

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_39541632/article/details/132529546