Transferred from: http://blog.csdn.net/zhangxinrun/article/details/5873148
When a program image starts running, the executable image must be loaded into the virtual address space of the process. If the program uses any shared library, the shared library must also be mounted into the virtual address space of the process. In fact, Linux does not load images into physical memory, whereas executables are simply linked to the virtual address space of the process (in disk space). As the program runs, the part of the referenced program is loaded into physical memory by the operating system. This method of linking an image to a process address space is called a "memory map." Executable image.
The virtual memory for each process is represented by a mm_struct structure, which we'll cover in detail in the next chapter. The structure actually contains information about the current execution of the image and contains a set of pointers to the vm_area_struct structure. As shown in 10-5, each vm_area_struct describes the start and end of a virtual memory region, the process's access to memory, and a set of operating routines for memory. An action routine set is a collection of routines that Linux uses when it operates on that memory area. For example, when the virtual memory that the process tries to access is not currently in physical memory (through page failure), Linux can take advantage of a routine in the operation set to perform the correct operation, in which case the nopage operation.
Figure 10-5 VM_AREA_STRUCT Data structure
When an executable image is mapped to the virtual address space of a process, a set of vm_area_struct structures is generated to describe the starting and ending points of the virtual memory area, each vm_struct structure represents part of the executable image, possibly executable code, It is also possible to initialize a variable or uninitialized data. As the VM_AREA_STRUCT structure is generated, the standard operating functions on the virtual memory areas described by these structures are also initialized by Linux.
When an executable image is mapped to process virtual memory and begins execution, because only a very small portion of the physical memory is loaded, the virtual memory area that has not yet been loaded into physical memory is quickly accessed. At this point, the processor will report a page failure to Linux and its corresponding cause of failure.
There are two reasons for this page failure, one is the program error, such as writing data to random physical memory, in which case, the virtual memory is invalid, Linux will send SIGSEGV signal to the program and terminate the operation of the program, in another case, the virtual address is valid, However, the corresponding page is not currently in physical memory, and the operating system must load the memory into physical memory from the disk image or swap file.
So, how does Linux determine if the virtual memory address is valid when a page failure occurs? As mentioned earlier, Linux uses the VM_AREA_STRUCT data structure to describe the virtual memory space of the process, and in order to find the location of the corresponding VM_AREA_STRUCT structure for the page failure virtual memory, the Linux kernel maintains a vm_area_struct The structure forms the AVL (adelson-velskii and Landis) tree. With the AVL tree, you can quickly find the area of the memory page where the virtual address of the page failure occurs. If the area of memory is not searched, the virtual address is invalid, otherwise the virtual address is valid.
It is also possible that a page failure occurs because the process is illegal on a virtual address, such as writing data in a read-only page. The operating system will also send a memory error signal to the process. The access control information about the page (read-only, write-only, read-write, executable code page, and so on) is included in the page table entry.
For a valid virtual address, Linux must differentiate between where the page is located, whether the page is in a swap file, or in an executable image. To do this, Linux differentiates the location of the page from the information in the page table entry. If the page table entry for the page is not valid, but is not empty, the page is in the swap file and the operating system is loading the page from the interchange file (the contents of the memory exchange are described in the next section). Otherwise, by default, Linux allocates a new physical page and establishes a valid page table entry, and for the image's memory map, it assigns a new physical page, updates the Page table Item property information, and loads the page from the image.
At this point, the required pages are loaded into physical memory, and the page table entries are updated at the same time, and the process can continue to execute. This process of loading virtual pages into physical memory only when necessary is called "demand paging."
During a page failure, the operating system chooses another process to enter the execution state because of time-consuming operations such as disk access.
Linux Memory Management 2: memory Mapping and requirements paging (English name: Demand Paging, also called: page break) "Go"