In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "Linux memory paging management method", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in depth, together to study and learn "Linux memory paging management method"!
Memory
Simply put, memory is a data shelf. Memory has the smallest unit of storage, most of which is one byte. Memory uses a memory address (memory address) to number the data of each byte sequentially. Therefore, the memory address indicates the location of the data in memory. The memory address starts at 0 and increases by 1 at a time. This linear increase in memory address is called linear address (linear address). For convenience, we use hexadecimal numbers to represent memory addresses, such as 0x00000003, 0x1A010CB0. The "0x" here is used to represent hexadecimal. "0x" is followed by the hexadecimal number as the memory address.
There is an upper limit to the number of memory addresses. The range of the address space is directly related to the number of bits of the address bus (address bus). CPU uses an address bus to indicate to memory the address at which you want to access data. Take Intel's 32-bit 80386 CPU, for example, which has 32 pins to transmit address information. Each pin corresponds to one. If there is a high voltage on the pin, this one is 1. If it is a low voltage, then this one is 0. The 32-bit voltage level information is transmitted to the 32 pins of memory through the address bus, and the memory can convert the voltage level information into 32-bit binary numbers, thus knowing which location of the data CPU wants. Represented in hexadecimal, the 32-bit address space is from 0x00000000 to 0xFFFFFFFF.
The memory unit of the memory adopts random read memory (RAM, Random Access Memory). The so-called "random read" means that the reading time of the memory is independent of the location of the data. In contrast, the read time of many memories is related to the location of the data. Take the tape, for example, if we want to listen to one of the songs, we have to turn the tape. If that song is the first one, it can be played immediately. If that song happens to be the last one, it will take us a long time to fast-forward to where we can play it. We already know that the process needs to call data from different locations in memory. If the data reading time is related to the location, it is very difficult for the computer to control the running time of the process. Therefore, the characteristic of random reading is the key factor for memory to become the main memory.
The storage space provided by memory can not only meet the running needs of the kernel, but also usually support running processes. Even if the process requires more space than memory, the memory space can be made up with a small amount of expansion. In other words, the storage capacity of memory is equal to the total amount of data in the running state of the computer. The disadvantage of memory is that the data cannot be kept permanently. Once the power is off, the data in memory will disappear. Therefore, even if the computer has a main memory like memory, it still needs external memory such as a hard disk to provide lasting storage space.
Virtual memory
One of the main tasks of memory is to store data related to the process. We have seen the program segments, global data, stacks and heaps in the process space, and the key role these storage structures play in the running of the process. Interestingly, although processes are so closely related to memory, processes do not have direct access to memory. Under Linux, a process cannot directly read or write data with an address of 0x1 in memory. The address that can be accessed in a process can only be a virtual memory address (virtual memory address). The operating system translates the virtual memory address into the real memory address. This kind of memory management is called virtual memory (virtual memory).
Each process has its own set of virtual memory addresses that are used to number its own process space. The data in the process space is also increased in bytes. Functionally, the virtual memory address is similar to the physical memory address, providing a location index for the data. The virtual memory addresses of processes are independent of each other. Therefore, two process spaces can have the same virtual memory address, such as 0x10001000. There is a corresponding relationship between the virtual memory address and the physical memory address, as shown in figure 1. An operation on a virtual memory address of a process is translated by CPU into an operation on a specific memory address.
Figure 1 correspondence between virtual memory address and physical memory address
As far as the application is concerned, the physical memory address is unknown. It is only possible to read and write data through virtual memory addresses. The memory address expressed in the program is also the virtual memory address. The operation of a process on a virtual memory address is translated by the operating system into an operation on a physical memory address. Because the operating system is solely responsible for the translation process, the application can know nothing about the physical memory address throughout the process. Therefore, the memory addresses expressed in C programs are all virtual memory addresses. For example, in C, you can print the variable address with the following instruction:
Int v = 0
Printf ("% p", (void*) & v)
In essence, the virtual memory address deprives the application of free access to the physical memory address. The process's access to physical memory must be reviewed by the operating system. Therefore, the operating system that controls the correspondence of memory also grasps the floodgates for applications to access memory. With the help of virtual memory address, the operating system can guarantee the independence of process space. As long as the operating system corresponds the process space of the two processes to different memory areas, it makes the two process spaces become two small kingdoms that do not interact with each other. It is impossible for two processes to tamper with each other's data, and the possibility of process errors is greatly reduced.
On the other hand, with a virtual memory address, memory sharing becomes easier. The operating system can map the same physical memory area to multiple process spaces. In this way, multiple processes can see the same data without any data replication. The mapping of the kernel and shared libraries is done in this way. In each process space, the virtual memory address of the first part corresponds to the space reserved for the kernel in physical memory. This way, all processes can share the same set of kernel data. The same is true for shared libraries. For any shared library, the computer only needs to load it into physical memory once, and it can be used by multiple processes by manipulating the corresponding relationship. Shared memory in IPO also depends on virtual memory addresses.
Memory paging
The separation of virtual memory address and physical memory address brings convenience and security to the process. However, the translation of virtual memory address and physical memory address will consume additional computer resources. In modern multitasking computers, virtual memory address has become a necessary design. In that case, the operating system must think carefully about how to efficiently translate virtual memory addresses.
The easiest way to record correspondence is to record it in a table. In order for the translation to be fast enough, the table must be loaded in memory. However, this method of recording is astonishingly wasteful. If the raspberry pie 1GB has a corresponding record for every byte of physical memory, then the correspondence alone will far exceed the memory space. Because of the large number of entries in the corresponding relationship, it takes a long time to find a corresponding relationship. In this way, raspberry pie will be paralyzed.
Therefore, Linux uses paging (paging) to record correspondence. The so-called paging is to manage memory in larger unit pages (page). In Linux, each page size is usually 4KB. To get the memory page size of the current raspberry pie, you can use the command:
$getconf PAGE_SIZE
Get the result, that is, the number of bytes of memory paging:
4096
The returned 4096 means that each memory page can hold 4096 bytes, or 4KB. Linux splits both physical memory and process space into pages.
Memory paging can greatly reduce the memory correspondence to be recorded. We have seen that there are too many corresponding records in bytes. If you divide the addresses of physical memory and process space into pages, the kernel only needs to record the correspondence of the pages, and the related workload will be greatly reduced. Because the size of each page is 4000 times that of each byte. Therefore, the total number of pages in memory is only 1/4000 of the total number of bytes. The correspondence is also reduced to 1/4000 of the original strategy. Paging makes the design of virtual memory address possible.
Whether it is a virtual page or a physical page, addresses within a page are contiguous. In this way, a virtual page corresponds to a physical page, and the data in the page can correspond one by one in order. This means that the end of the virtual memory address and the physical memory address should be exactly the same. In most cases, each page has 4096 bytes. Since 4096 is to the power of 2 to the power of 12, the correspondence of the last 12 bits of the address is natural. We call this part of the address the offset. The offset actually represents the position of the byte within the page. The first part of the address is the page number. The operating system only needs to record the correspondence of the page number.
Multi-level paging table
The key to the memory paging system is to manage the correspondence between process space pages and physical pages. The operating system records the correspondence in a paging table (page table). This correspondence separates the abstract memory of the upper layer from the physical memory of the lower layer, thus allowing Linux to manage memory flexibly. Since each process will have a set of virtual memory addresses, each process will have a paging table. In order to ensure the query speed, the paging table is also saved in memory. There are many ways to implement a paging table, and the simplest paging table is to record all correspondence in the same linear list, as shown in the "correspondence" section in figure 2.
This single continuous paging table needs to reserve a record location for each virtual page. However, for any application process, the real address used in the process space is quite limited. We also remember that the process space will have stacks and heaps. Process space reserves addresses for stack and heap growth, but stacks and heaps rarely fill up process space. This means that if you use a continuous paging table, many entries are not really used. Therefore, the paging table in Linux adopts a multi-tier data structure. A multi-tier paging table can reduce the space required.
Let's take a look at a simplified paging design to illustrate Linux's multi-tier paging table. We divide the address into two parts: the page number and the offset, and record the corresponding relationship of the page number part with a single-layer paging table. For a multi-tier paging table, the page number is further divided into two or more parts, and then the corresponding relationship is recorded with a two-or more-tiered paging table, as shown in figure 3. Figure 3 Multi-tier paging table in the example in figure 3, the page number is divided into two levels. The first level corresponds to the first eight-digit page number, represented by two hexadecimal digits. The second level corresponds to the last 12-digit page number, with 3 hexadecimal numbers. The secondary table record has a corresponding physical page, that is, the real paging record is saved. There are many second-level tables, and the first eight bits of the virtual address corresponding to the paging record of each second-level table are the same. For example, in the second-class table 0x00, the first 8 digits recorded in it are all 0x00. The process of translating an address spans two levels. We first take the first 8 bits of the address and find the corresponding record in the level table. The record tells us the location of the target secondary table in memory. In the second-level table, we find the paging record through the last 12 bits of the virtual address, and finally find the physical address.
A multi-layer paging table is like dividing a complete phone number into area codes. We put the phone numbers of the same area and the corresponding records of people's names in the same book. Then use a superior book to record the corresponding relationship between the area code and each small book. If an area code is not used, then we just need to mark the area code as empty in the superior book. Similarly, the 0x01 record in the first-level paging table is empty, indicating that the virtual address field that begins with 0x01 is not used, and the corresponding second-level table does not need to exist. It is by this means that multi-tier paging tables take up much less space than single-tier paging tables.
Multi-tier paging tables have another advantage. A single-layer paging table must exist in contiguous memory space. On the other hand, the second-level table of multi-tier paging table can walk in different locations of memory. In this way, the operating system can use fragmented space to store paging tables. It is also important to note that many of the details of the multi-tier paging table are simplified. The paging table in the latest Linux system has as many as three layers, and the memory address managed is much longer than that introduced in this chapter. However, the basic principle of multi-layer paging tables is the same.
Thank you for your reading, the above is the content of "Linux's memory paging management method". After the study of this article, I believe you have a deeper understanding of Linux's memory paging management method, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.