Process address space (Linux virtual memory mechanism)

Insert image description here

The theory in this chapter is based on the version kernel 32of the bit platformLinuxkernel 2.6.32

1. Structure of Linux process address space

  • to ensure thatmemory safety, modern operating systemsnot allowedApplications (processes) directly access physical memory space

  • Generally speaking,Operating systems and compilerswill giveevery processset upA separate virtual memory spaceanda mapping page table, the virtual memory space is called the process address space,virtual memory addressThrough a specific mapping relationship withphysical memory addressbuild connection

  • LinuxThe process address space is 0x00000000 ~ 0xffffffffnumbered and has the following structure:Insert image description here

  • Instructions in the process want to accessphysical memory, it is necessary to rely onvirtual memory addressmapping, processVirtual address and physical memorypasspage tableTo map:Insert image description here

  • From an application layer perspective,application codeneutralVariables and functions(data) memory address andinstruction itselfmemory addressAll are virtual memory addresses!!!All of thesevirtual memory addressas well asTheir mapping relationship to physical memory addressesThis is determined when the application is compiled, soProcess virtual memory mechanismEssentially, it is a memory management mechanism formed by the cooperation of the operating system and the compiler.

    • When the CPU executes instructions internally, the memory address used is also a virtual memory address.

2. How Linux manages process address space

  • every processEach has its own independentprocess address spaceandmapped page table
  • LinuxIn the kernel, the process address space struct mm_structis maintained by one, and PCBthe structure (process control block) of each process points to a struct mm_structstructure:Insert image description here
  • struct mm_structStructure maintenanceprocess address spaceThe specific method is similar toUse pointers to maintain address ranges:Insert image description here
  • When multiple processes are loaded in the memory, there will be multiple struct mm_structstructures andmapped page table,thereforeLinuxprocess address spaceThe essence is struct mm_structthe structure andmapped page tableconstitutedKernel data structures

3. The model of Linux process using physical memory

  • LinuxThe existence form of process PCB( task_struct) and process address space ( ):mm_struct
    Insert image description here
  • How the process instruction set accesses physical memory:
    Insert image description here

4. The significance of the existence of process address space

  1. With the help ofmapped page tableThe operating system canDeny requests for illegal memory access by application processes, ensuring the memory security of the computer system
  2. In the sense of operating system design,virtual memory mechanismmake the operating systemMemory management moduleandProcess management moduleComplete understanding of coupling whenMemory management algorithmWhen adjustments are needed, it will not affectProcess management moduledesign (just modify the physical address in the mapped page table), similarly, whenProcess management moduleWhen redesign is required, it will not affectMemory management module
  3. virtual memory mechanismIn fact, it is a kind ofDelayed allocation strategy for physical memory, when the process PBCandInstruction SetAfter being loaded into memory, the operating system will not allocate physical memory to the objects in the process immediately (even if each object in the process has a virtual address), only when the process assignsPerform read and write operationsThe operating system will allocate physical memory to the object through page table mapping. This delayed memory allocation strategy improvesDuring the concurrent execution of each processComputer memory usage efficiency
  4. becauseprocess virtual address spaceexistence, from the perspective of the application,The memory address of each objectThey are all arranged in an orderly and compact manner, allowing developers toMore convenient memory operations, at the same time the operating system can also more convenientlyMonitor and manage memory usage by processes, to achieve instant allocation and release of physical memory.
    Insert image description here

Guess you like

Origin blog.csdn.net/weixin_73470348/article/details/132819628