OS review---virtual memory, page, segment

At the very beginning, I always understood that virtual memory is a section of space opened up in the hard disk to expand memory, but after careful understanding, I discovered that virtual memory is not like this. This is only an inevitable result of using virtual memory technology. .

Virtual memory is a technology of computer system memory management. It makes the application think that it has continuous available memory (a continuous and complete address space), but in fact, it is usually divided into multiple physical memory fragments, and some are temporarily stored on external disk storage, when needed Perform data exchange. The CPU will access the main memory through a virtual address. This virtual address will be replaced with a physical address by the MMU (Memory Management Unit) before being sent to the main memory.

Virtual memory mainly provides the following three important capabilities:

  • It treats main memory as a cache of virtual address space stored on the hard disk, and only caches active areas in main memory (on-demand caching).
  • It provides a consistent address space for each process, thereby reducing the complexity of the programmer's memory management.
  • It also protects the address space of each process from being damaged by other processes.

The virtual memory system divides the virtual memory into fixed-size virtual pages, and the size of each virtual page is fixed bytes. Similarly, the physical memory is divided into physical pages, the size is also fixed bytes.

Virtual memory is divided into three parts that you don’t want to hand over: unallocated, cached (allocated pages that are currently cached in physical memory), and uncached (the page has been mapped to disk, but has not been cached in memory yet) .)

The system will use the page table to determine whether a virtual page is cached somewhere in the memory. At this time, it can be divided into two situations:

  • Already exists in memory, then you need to determine which physical page the virtual page exists in
  • No longer in the memory, then the system must determine where the virtual page is stored on the hard disk, and select a sacrifice page in the physical memory, copy the virtual page from the hard disk to the main memory, and replace the sacrifice page.

The page table is read every time the address translation hardware converts a virtual address into a physical address.

If a page we want to find already exists in memory, it is a page hit. But if it is not in memory, it is a page fault. When a page is faulty, a sacrifice page is selected and copied to the disk, but the page we need on the disk is replaced with the original location of the sacrifice page in memory.

Paragraphs and pages

The address space of the user program is divided into a number of fixed-size areas called "pages". Correspondingly, the memory space is divided into a number of physical blocks, and the size of the page and the block are equal. Any page of the user program can be placed in any block of the memory, realizing discrete allocation.

A page is a fixed area of ​​equal length divided in the physical space of the main memory. The advantage of the paging method is that the page length is fixed, so it is easy to construct the page table, easy to manage, and there is no external fragmentation. But the disadvantage of the paging method is that the page length is not related to the logical size of the program. For example, at a certain time, a part of a subroutine may be in the main memory and another part in the auxiliary memory. This is not conducive to independence during programming, and causes trouble for operations such as swap-in and swap-out processing, storage protection, and storage sharing.

Another method of dividing the addressable storage space is called segmentation. A segment is an area whose length can be dynamically changed according to the natural boundaries of the program. Generally, programmers divide different types of data such as subroutines, operands, and constants into different sections, and each program can have multiple sections of the same type.

Paragraph

Segment-page storage organization is a storage organization method that combines segmentation and paging, which can make full use of the advantages of segmentation management and paging management.

  1. Use segmentation method to allocate and manage virtual memory. The address space of the program is divided into basically independent sections according to logical units, and each section has its own section name, and then each section is divided into several pages of fixed size.
  2. Use the paging method to allocate and manage the actual memory. That is, the entire main memory is divided into storage blocks equal to the above-mentioned page size, which can be loaded into any page of the job. The transfer of the program into or out of the memory is carried out on a page-by-page basis. But it can be shared and protected by segment.

Swap exchange area

When the physical memory of the system is not enough, part of the space in the physical memory needs to be released for use by the currently running program. The freed space may come from programs that have not operated for a long time. The information in the freed space is temporarily saved in the Swap space. When the programs are running, the saved data is restored from the Swap to the memory. in. The system always swaps when the physical memory is not enough.

Guess you like

Origin blog.csdn.net/why1092576787/article/details/114760505