Article title: Linux core-4. Memory management. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Original: David A Rusling
Translation: Banyan & fifa
Chapter 3 Storage Management
The storage management subsystem is one of the most important components of the operating system. In the early computing era, because the amount of memory required by people is much larger than the physical memory, people have designed a variety of strategies to solve this problem. The most successful is the virtual memory technology. It satisfies the memory space required by processes that compete with limited physical memory.
The virtual memory technology not only allows us to use more memory, but also provides the following features:
Huge addressing space
The operating system makes the system look much larger than the actual memory. The virtual memory can be many times the actual physical space in the system. Each process runs in its own virtual address space. These virtual spaces are completely isolated from each other, so they do not affect each other. At the same time, the hardware virtual memory organization can set some areas of the memory as non-writable. This protects code and data from malicious programs.
Memory ing
The memory ing technology can map image files and data files directly to the address space of the process. In memory ING, the file content is directly connected to the virtual address space of the process.
Fair physical memory allocation
The memory management subsystem allows every running process in the system to share the physical memory fairly.
Shared Virtual memory
Although the virtual memory allows the process to have its own virtual address space, it sometimes needs to share the memory between processes. For example, there may be several processes running the BASH command shell program at the same time in the system. To avoid the existence of BASH program copies in the virtual memory space of each process, A better solution is to have only one BASH copy in the physical memory of the system and share it among multiple processes. The dynamic Library is another way to share and execute code between processes. Shared memory can be used as a means of inter-process communication (IPC). multiple processes exchange information through shared memory. Linux supports the shared memory IPC mechanism of system v.
3.1 virtual memory abstract model
Before discussing how Linux supports virtual memory, it is necessary to look at a simpler abstract model.
When the processor executes a program, it needs to read it from the memory and then decode the command. Before decoding a command, it must extract or store a value to a location in the memory. Then execute this command and point to the next command in the program. In this process, the processor must frequently access the memory, either take the specified number or store data.
All addresses in the virtual memory system are virtual addresses instead of physical addresses. The processor converts a series of tables maintained by the operating system from virtual addresses to physical addresses.
To make the conversion easier, both the virtual memory and physical memory are organized on pages. The page size in different systems can be the same or different, which will cause inconvenience in management. The size of the Linux page running on the Alpha AXP processor is 8 kB, while that of the Intel X86 system is 4 kB. Each page is marked by a number (PFN ).
The virtual address in page mode is composed of two parts: the page box number and the offset value in the page. If the page size is 4 kB, the 0 bit of the virtual address indicates the virtual address offset value, and the 12-bit or above indicates the virtual page box number. The processor must complete address separation when processing virtual addresses. With the help of the page table, it converts the virtual page box number to the physical page box number, and then accesses the corresponding offset on the physical page.
. 1 provides the virtual address space of two processes X and Y. They have their own page tables. These page tables map virtual pages of various processes to physical pages in the memory. In the figure, the virtual page number 0 of process X is mapped to the physical page number 4. Theoretically, each page table entry should contain the following content:
Valid mark, indicating that the entry to the table on this page is valid
The physical page number described in the page table entry
Access control information. Which operations can be performed on this page? Include execution code?
The virtual page box number is the offset in the page table. The virtual page number 5 corresponds to the 6th units in the table (0 is the first ).
To convert a virtual address to a physical address, the processor must first obtain the virtual address page box number and page offset. Generally, the page size is set to the power of 2. Set. the page size in 1 is set to 0x2000 bytes (decimal: 8192) and an address in the virtual address space of process Y is 0x2194, the processor converts it to virtual page number 1 and the page offset 0x194.
The processor uses the virtual page box number as an index to access the processor page table and retrieve the page table entry. If the page table entry at this location is valid, the processor will obtain the physical page box number from this portal. If this entry is invalid, it means that the processor accesses a non-existent area in the virtual memory. In this case, the processor cannot perform address translation, and it must pass the control to the operating system to complete this operation.
When a process attempts to access a virtual address that the processor cannot perform a valid address translation, how does the processor transmit control to the operating system depends on a specific processor. The common practice is that the processor causes a page Failure error and falls into the core of the operating system. In this way, the operating system will get information about the invalid virtual address and the cause of the page error.
Then use. for example, if the virtual page number 1 of process Y is mapped to system physical page number 4, the starting position in physical memory is 0x8000 (4*0x2000 ). Add the 0x194 byte offset to obtain the final physical address 0x8194.
By ing virtual addresses to physical addresses, virtual memory can be mapped to system physical pages in any order. For example. in 1, the virtual page number 0 of process X is mapped to the physical page number 1, and the virtual page number 7 is mapped to the physical page number 0, although the virtual page number of the latter is higher than that of the former. In this way, the virtual memory technology brings interesting results: the pages in the virtual memory do not need to maintain a specific order in the physical memory.
3.1.1 request form feed
In systems with much smaller physical memory than virtual memory, the operating system must improve the efficiency of physical memory usage. One way to save physical memory is to load only the virtual pages that are being used by the program. For example, a database program may need to query a database. at this time, not all the content of the database must be loaded into the memory, but only those parts to be used. If the database query is a search query without adding records to the database, loading the code for adding records is meaningless. This technology loads only virtual pages to be accessed is called Request form feed.
When a process attempts to access a virtual address that is not in memory, the processor cannot find the entry of the referenced address in the page table. In. 1. for virtual page number 2, there is no entry in the page table of process X, so when process X tries to access the content of virtual page number 2, the processor cannot convert this address to a physical address. In this case, the processor notifies the operating system of a page error.
If a page error occurs, the virtual address is invalid, indicating that the process is trying to access a non-existent virtual address. This may be caused by an application error. for example, it tries to perform a random write operation on the memory. At this time, the operating system will terminate the operation of this application to protect other processes in the system from the impact of this error process.
If the error virtual address is valid but the page it points to is not in memory, the operating system must read the page from the disk image to the memory. Because the disk access takes a long time, the process must wait for a while until the page is retrieved. If there are other processes in the system, the operating system selects one of them to run when reading the page. The read-back page will be placed in an idle physical page box, and the Portal corresponding to the virtual page number will be added to the page table of the process. Finally, the process will run again from where a page error occurs. At this time, the entire virtual memory access process has come to an end. the processor can continue to convert the virtual address to the physical address, and the process can continue to run.
Linux uses request form feed to load the executable image to the virtual memory of the process. When a command is executed, the executable command file is opened and its content is mapped to the virtual memory of the process. These operations are done by modifying the data structure of the process memory image. this process is called memory ING. However, only the initial part of the image is transferred to the physical memory, and the remaining part remains on the disk. When the image is executed, it will produce a page error, so that Linux will decide which part of the disk will be transferred to the memory for further execution.
3.1.2 exchange
If a process needs to transfer a virtual page to the physical memory, but the system does not have any idle physical page, the operating system must discard some pages in the physical memory to free up space.
If the pages discarded from the physical memory come from executable files or data files on the disk, and have not been modified, you do not need to save those pages. When the process needs this page again, it can be read directly from the executable file or data file.
However, if the page has been modified, the operating system must keep the page content for further access. These pages are called dirty pages. when they are removed from the memory, they must be stored in special files called swap files. Compared with the speed of the processor and physical memory, the speed of accessing the swap file is very slow. the operating system must write these dirty pages to the disk and keep them in the memory to make a choice.
When you select an algorithm to discard a page, you often need to determine which pages are to be discarded or exchanged. if the switching algorithm is very inefficient, a "bumps" will occur. In this case, the page is constantly written to the disk and read back from the disk, so that the operating system cannot perform any other work. Take. 1 as an example. if the physical page number 1 is frequently used, it is inappropriate for the page discard algorithm to use it as the candidate for switching to the hard disk. A page set frequently used by a process is called a working set. The efficient switching policy ensures that the worksets of all processes are stored in the physical memory.
Linux uses the least recently used (LRU) page aging algorithm to fairly select the page to be discarded from the system. This policy sets an age for each page in the system. It varies with the number of page visits. The more times a page is accessed, the younger the page is. on the contrary, the older the page is. Older pages are the best candidates to switch pages.
3.1.3 Shared Virtual memory
The virtual memory allows multiple processes to easily share the memory. All memory access is performed through the page tables of each process. For two processes that share the same physical page, the corresponding page table must contain a page table entry pointing to this physical page number.
. 1. the two processes share the physical page No. 4. For process X, the corresponding virtual page box is 4 and process Y is 6. This interesting phenomenon indicates that the process sharing a physical page corresponds to the virtual memory location of the page.
3.1.4 Physical and Virtual addressing mode
It is of little significance that the operating system itself runs in the virtual memory. If the operating system is forced to maintain its own page tables, it will be a disgusting solution. Most general processors
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service