I. Briefly introduce the early memory implementation: (skipped)
1. In early computers, running a program was characterized:
(1) All these programs will be loaded into the memory,
(2) The program runs directly in the memory, that is, the memory address accessed in the program is the actual physical memory address.
2. Problems with early memory implementation:
(1) When the computer runs multiple programs at the same time, make sure that the total memory used by these programs is smaller than the actual physical memory size of the computer.
(2) The process address space is not isolated. Data can be modified between processes.
(3) low memory usage. To run a process, you must allocate the actual process size space in the memory (no matter whether the current space is used or not)
(4) The program running address is unknown. When allocating memory, you just need to find a sufficient space from the memory space to meet the requirements of the process, so the address of the process running is random.
Ii. Virtual Address Technology
To solve the problem above, some experts have proposed the so-called virtual address technology. That is, an intermediate layer (Virtual Address) is added between the user process and the actual physical memory, so that the user process can only access the virtual address, and the implementation of the physical address conversion is handed over to the operating system.
Features of virtual addresses:
1. When a process is created,
(1) The OS allocates a 4G virtual address space for each process, and each process has 3G user space and 1g kernel space. However, the content in the last 1 GB kernel space is the same for different processes (the reason is the 4 GB size, because in a 32-bit operating system, the size of a pointer is 4 btyes, therefore, the accessible address space is 0x00000000 ~ 0 xffffffff = 4G ).
(2) The 4G virtual address space is virtualized by the operating system and does not actually exist.
(3) Each process can only access its own virtual address and cannot access the virtual address of another process.
(4) Although the virtual addresses of each process are 4 GB in size, not the 4 GB addresses can be used by users at will. In Linux, the virtual address is divided into two parts (as shown in ):
Note: 1. At any time, only one process is running on one CPU. Therefore, for this CPU, there is only one 4 GB virtual address space in the system at this time, and this virtual address space is for this process.
When a process is switched, the virtual address space also changes. Each process's virtual address space is known to its CPU only when it is running. At other times, its virtual address space is unknown to the CPU. Therefore, each process can have four
GB virtual address space, but in the eyes of the CPU, only one virtual address space exists. Changes in the virtual address space as the process switches.
2. The ing between kernel space and physical memory space and between user space and physical memory space are different:
Although the kernel space occupies a maximum of 1 GB in each virtual space, the ing to physical memory always starts from the lowest address (0x00000000, it is used to establish a simple linear ing relationship between the kernel space and the physical memory. 3 GB (0xc0000000) is the displacement between the physical address and the virtual address. It is called page_offset in Linux code.
Let's take a look at the description and definition of address ing in the kernel space in the include/ASM/i386/page. h header file:
# DEFINE _ page_offset (0xc0000000)
......
# Define page_offset (unsigned long) _ page_offset)
# DEFINE _ Pa (x) (unsigned long) (x)-page_offset)
# DEFINE _ VA (x) (void *) (unsigned long) (x) + page_offset ))
For kernel space, given a virtual address X, its physical address is "X-
Page_offset. Given a physical address X, its virtual address is "x + page_offset ".
It is explained again that macro _ Pa () only maps the virtual address of a kernel space to the physical address,
This method is never applicable to a user space. The address ing of a user space is much more complex. It is done through the paging mechanism.
(Figure 1) Overall structure of 4G virtual address space
(Figure 2) kernel space distribution chart
(Figure 3 address distribution chart)
2. What is opposite to the virtual address is the actual size of our memory. Take my PC as an example: I use 2 GB memory, so the actual physical address space is: 0x00000000 ~ 0x1fffffff
Iii. Segmentation and paging
1. The new problem after adopting the virtual address technology is the ing between the virtual address and the physical address. The solution proposed by the experts is segmentation, paging, and segment page. We will use an example to demonstrate the specific implementation:
For example, if there are two processes a and B, the memory size required for process a is 10 m, the virtual address space is distributed in 0x00000000 to 0x00a00000, and the memory required for process B is 100 m, its virtual address space is 0x00000000 to 0x06400000.
(1) segmented
Although this piecewise ing method solves the problems (2) and problems (4) above, it does not solve the problem (1) (3), that is, the memory usage efficiency. In the multipart ing method, the entire program is used every time the memory is swapped in and out,
This will cause a large number of disk access operations, resulting in low efficiency. Therefore, this ing method is slightly rough and has a large granularity.
(2) Paging
The basic method of paging is to divide the address space into many pages. The size of each page is determined by the CPU, and then the size of the page is selected by the operating system. At present, the Inter series CPU supports 4 kb or 4 MB page size, while the PC currently uses 4 kb. In this case, 4 GB virtual address space can be divided into 1048576 pages, and 512 MB of physical memory can be divided into 131072 pages.
The idea of paging is that the memory is allocated for the page used when the program runs, and unused pages are temporarily retained on the hard disk.
4. Here we only provide a brief and general introduction to segmentation and paging. If you want to have a deeper understanding of the underlying implementation mechanism, please refer to the next article.
Management of virtual addresses, physical addresses, and memory in Linux (2)