In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
Editor to share with you the reasons why Linux needs virtual memory, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Before answering the need for virtual memory, we need to understand what virtual memory is in the operating system and what role it plays in the operating system. Like other abstractions in software engineering, virtual memory is the middle layer between the operating system's physical memory and processes. It hides the concept of physical memory for processes and provides processes with more concise and easy-to-use interfaces as well as more complex functions.
Virtual-memory-layer
Figure 1-the middle layer between the process and the operating system
If we need to design an operating system from scratch, it should be a very natural decision to let the processes in the system directly access the physical address in the main memory, and the early operating systems did the same. The process will use the physical address (Physical Address) of the target memory to access the contents of memory directly. However, modern operating systems have introduced virtual memory. The virtual address (Virtual Address) held by the process is converted to the physical address [^ 2] by the memory management unit (Memory Mangament Unit), and then the memory is accessed through the physical address:
Virtual-memory-system
Figure 2-Virtual memory system
Main storage is a relatively scarce resource. Although sequential reading is only one order of magnitude faster than disk, it can provide extremely fast random access speed. Random data reading from memory is 100000 times higher than that of disk [^ 3]. Making full use of the random access speed of memory is an effective way to improve program execution efficiency.
The operating system manages memory on a page-by-page basis, and when a process finds that the data it needs to access is not in memory, the operating system may load the data into memory as pages, which is done by the memory management unit (MMU) in the figure above. As an abstraction layer, the virtual memory of the operating system plays the following three key roles:
Virtual memory can use the disk as a cache to improve the speed at which the process accesses the specified memory.
Virtual memory can provide independent memory space for processes, simplify program linking, loading process, and share memory through dynamic libraries.
Virtual memory can control the access of processes to physical memory, isolate access rights of different processes, and improve the security of the system.
Caching
We can think of virtual memory as a piece of space on disk. When a part of this space is accessed frequently, this part of the data will be cached into main memory in pages to speed up the performance of CPU accessing data. Virtual memory uses larger disk storage as "memory" and uses the main storage cache to accelerate, making the upper layer think that the memory of the operating system is large and fast. However, disks with large areas are not fast, and fast memory is not large.
Virtual-memory-cache
Figure 3-Virtual memory, main memory, and disk
Virtual pages (Virtual Page,PP) in virtual memory may be in the following three states-unallocated (Unallocated), uncached (Uncached), and cached (Cached), where unallocated memory pages are not requested by the process, that is, free virtual memory and do not occupy any space on the virtual memory disk Uncached and cached memory pages represent memory pages that have been loaded into main memory and memory pages that are loaded only to disk, respectively. As shown in the figure above, the green virtual memory page in the figure is supported by the physical memory page (Physical Page,PP) in main memory, so it has been cached, while the yellow virtual memory page is only on disk, so it is not cached by physical memory.
When a user program accesses a virtual page that is not cached, the hardware will trigger a Page Fault,PF. In some cases, the visited page has been loaded into physical memory, but the page table (Page Table) of the user program does not have the corresponding relationship. In this case, we only need to establish the relationship between virtual memory and physical memory in the page table. In other cases, the operating system needs to load virtual pages that are not cached on disk into physical memory [^ 4].
Page-fault
Figure 4-missing Page break for Virtual memory
Because the main memory space is limited, when there is no available space in the main memory, the operating system will drive back to disk from selecting the appropriate physical memory page to make room for the new memory page. The process of selecting the page to be expelled is called Page Replacement in the operating system. Page fault interrupt and page replacement technology are part of the operating system paging algorithm (Paging). The purpose of this algorithm is to make full use of memory resources as disk cache to improve the efficiency of the program.
Memory management
Virtual memory can provide independent memory space for running processes, creating the illusion that the memory of each process is independent. On a 64-bit operating system, each process will have 256 TiB of memory space, kernel space and user space account for 128 TiB [^ 5] respectively. Some operating systems use 57-bit virtual addresses to provide 128 PiB address space [^ 6]. Because the virtual memory space of each process is completely independent, they can use all the memory from 0x0000000000000000 to 0x00007FFFFFFFFFFFF.
Virtual-memory-space
Figure 5-Virtual memory space for the operating system
Virtual memory space is just a logical structure in the operating system, and as we said above, applications eventually need to access physical memory or disk content. Because the operating system adds a middle layer of virtual memory, we also need to implement an address translator for the process to realize the conversion from virtual address to physical address. Page table is an important data structure in virtual memory system. The page table of each process stores the mapping relationship from virtual memory to physical memory page. In order to store 128 TiB virtual memory mapping data in 64-bit operating system. Linux introduced a four-tier page table to assist in the translation of virtual addresses [^ 7] in 2.6.10, and a five-tier page table structure [^ 8] in 4.11. In the future, more layers of page table structures may be introduced to support 64-bit virtual addresses.
Four-level-page-tables
Figure 6-four-tier page table structure
In the four-tier page table structure shown in the figure above, the operating system uses the lowest 12 bits as the offset of the page, and the remaining 32 bits are divided into four groups to represent the index of the current level in the upper level. all virtual addresses can be found using the above multi-tier page table.
Because there are multiple layers of page table structure that can be used to translate virtual addresses, multiple processes can share physical memory through virtual memory. Replication takes advantage of this feature of virtual memory when writing in the article why Redis snapshots use child processes. When we call fork to create a child process in Linux, we actually copy only the page table of the parent process. As shown in the following figure, the parent and child processes point to the same physical memory through different page tables:
Process-shared-memory
Figure 7-shared memory between processes
Virtual memory can not only be used to share the physical memory of processes during fork, provide a mechanism to copy when writing, but also share some common dynamic libraries to reduce the occupation of physical memory, all processes may call the same operating system kernel code, and C language programs will also call the same standard library.
In addition to being able to share memory, independent virtual memory space also simplifies the memory allocation process. When a user program requests heap memory from the operating system, the operating system can allocate several consecutive virtual pages, but these virtual pages can correspond to discontiguous pages in physical memory.
Memory protection
User programs in the operating system should not modify read-only code snippets, read or modify code and data structures in the kernel, or access the memory of private and other processes. If the memory access of user processes cannot be restricted, attackers can access and modify the memory of other processes to affect the security of the system.
If each process holds a separate virtual memory space, the page table in virtual memory can be understood as a "connection table" between the process and the physical page, where the access relationship between the process and the physical page can be stored, including read, write, and execute permissions:
Virtual-memory-permission
Figure 8-read, write, and execute permissions
The memory management unit can determine whether the current process has access to the target's physical memory, so that we finally converge all the rights management functions to the virtual memory system, reducing the possible risk of code paths.
Summary
The design method of virtual memory can be said to be a common means in software engineering, by combining the respective advantages of disk and memory, using the middle layer to schedule resources more reasonably to fully improve the utilization of resources and provide a harmonious and unified abstraction. In actual business scenarios, similar cache logic is also common.
The virtual memory of the operating system is a very complex component, and no engineer can understand all the details, but it is also valuable to understand the overall design of virtual memory, from which we can find a lot of software design methods. Let's go back to today's question-why virtual memory is needed in the Linux operating system:
Virtual memory can combine the advantages of disk and physical memory to provide processes with storage that appears to be fast enough and large enough.
Virtual memory can provide independent memory space for processes and introduce a multi-tier page table structure to translate virtual memory into physical memory. Processes can share physical memory to reduce overhead and simplify the process of linking, loading and memory allocation of programs.
Virtual memory can control the access of processes to physical memory, isolate access rights of different processes, and improve the security of the system.
These are all the contents of the article "what are the reasons why Linux needs virtual memory?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.