The role of virtual memory in memory management

Note: Most of the content in this article is excerpted from the book "In-depth Understanding of Computer Systems", which is for record purposes.
   
    As an important concept in modern operating systems, the importance of virtual memory is self-evident. If you don't understand the concept of virtual memory, be sure to check out the relevant materials. This article does not intend to dwell on it, but only introduces the tip of the iceberg of its many functions, that is, how to use it as a tool for memory management.
    We know that disk and main memory are in the form of data blocks as transmission units, so physical memory is generally divided into blocks of fixed size, called physical pages, also called page frames. But this is not enough, because the speed of CPU and disk are not at the same level, so virtual memory can be used. For better operation, virtual memory is also divided into fixed-size blocks, so-called virtual pages. At any time, the virtual page set is divided into three disjoint subsets:
    1. Unallocated: no data association, no memory usage.
    2. Cached: Allocated pages cached in physical memory.
    3. Uncached: Allocated pages that are not cached in physical memory.

    The combination of an on-demand paging algorithm for virtual memory and a process-independent virtual address space will affect the use and management of memory in the system. In a nutshell, the following is the important role of virtual memory in memory management:
    1. Simplified linking. Separate address spaces allow each process's memory image to use the same basic format, regardless of where the code and data actually reside in physical memory. The stack occupies the highest part of the process address space and grows downward. This consistency simplifies linker design and implementation, allowing the linker to generate fully linked executables that are independent of the final location of code and data in physical memory.
    2. Simplify loading. Virtual memory makes it easy to load executable files and shared file objects into memory. The loader never actually copies any data from disk to memory, the virtual memory system automatically loads data pages as needed. By the way, the concept of memory map is mentioned here, which is a notation for mapping a set of contiguous virtual pages to any location in any file. There is a system call called mmap in Unix, which allows applications to do memory mapping by themselves.
    3. Simplify sharing. Generally speaking, each process has its own independent address space, which is achieved by the operating system by creating page tables to map the corresponding virtual pages to different physical pages. But when processes need to share code and data (such as operating system kernel code, standard library functions, etc.), just map appropriate virtual pages in different processes to the same physical page, and then arrange for multiple processes to share a copy of this part of the code , rather than including a separate copy of the kernel and standard library in each process.
    4. Simplify memory allocation. Virtual memory provides a simple mechanism for user processes to allocate additional memory. When a user process requires additional heap space, the operating system allocates appropriate k contiguous virtual memory pages and can map them to any k physical pages anywhere in physical memory, so that the pages can be randomly scattered among in physical memory.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326616825&siteId=291194637