Operating system core principles-5. Memory Management (bottom): Paging memory management

Source: Internet
Author: User

  In the previous article introduced several multi-channel programming memory management mode, the Exchange memory management is most flexible and advanced. But there are a lot of major problems with this strategy, and the two most important issues are space waste and program size constraints. So what's the solution to the problem of swapping memory? The answer is paging, which is the "only way" that we solve for the exchange defect.

One, paging memory management 1.1 the way to solve the problem

In order to solve the shortcomings of the switching system, the paging system turned out. The core of paging system is: The virtual memory space and physical memory space are divided into the same size of the page, such as 4KB, 8KB or 16KB, and the page as the smallest allocation unit of memory space, a program of a page can be stored in any physical page .

(1) solve the problem of space waste fragmentation

Because the virtual memory space and the physical memory space are allocated according to a certain size, we call it a page, and then the memory is allocated by the page, which overcomes the problem of external fragmentation.

(2) solve the problem of limited program size

Program growth is limited because a program needs to be fully loaded into memory to run, so the solution is to allow a program to run without having to load it all. The use of paging can also solve this problem, simply put the current needs of the page in memory, and other temporarily unused pages on the disk, so that a program consumes both memory and disk, the growth of the space is greatly increased. And, after paging, if a program needs more space, assign it a new page (without having to pour out the program to increase the efficiency of space growth).

1.2 The composition of virtual address and address translation

(1) The composition of the virtual address

Under the paging system, the virtual address emitted by a program consists of two parts: the page number and the offset value in the page, as shown in:

For example, for a system with 32-bit addressing, if the page size is 4KB, the page number is 20 bits and the page offset value is 12 bits.

(2) Address translation: virtual address → Physical address

  The core of the paging system is the translation of the page, which is the mapping from the virtual page to the physical page (Mapping). The translation process is shown in the following pseudo-code:

if (Virtual page is illegal, not in memory or protected) {    caught in the operating system error service program}else{    The virtual page number is converted to a physical page number to    produce the final physical address according to the physical page number}

While this translation process is done by the Memory Management Unit (MMU), the MMU receives the virtual address sent by the CPU, translates it to a physical address and sends it to memory. The memory snap-in follows the physical address to read or write the relevant data, as shown in:

So, how does this translation come true? The answer is a page table , for each program, the memory management unit MMU holds a page table that holds the mappings of virtual pages to physical pages. Whenever a physical page is found for a virtual page, a record is added to the page to preserve the mapping. Of course, as virtual pages move in and out of physical memory, the contents of the page table are constantly updated and changed.

1.3 Page Table

  the fundamental function of a page table is to provide a mapping from a virtual page to a physical page . Therefore, the number of record bars in a page table is the same as the number of virtual pages. In addition, the memory management unit relies on the page table to perform all the administrative activities related to the page , such as determining whether a page number is in memory, whether the page is protected, whether the page is illegal, and so on.

A record of the page table includes the contents as shown in:

Because of the special status of the page table, it is determined that it is supported directly by the hardware, that is, the page table is a hardware data structure.

1.4 Advantages and disadvantages of the paging system

Advantages:

(1) The paging system does not produce external fragments, and the memory space occupied by a process may not be contiguous, and a process's virtual page can be placed on disk when it is not needed.

(2) Paging system can share a small address, that is, page sharing. Just make a related record in the page table item corresponding to the given page.

Cons: The page table is large and takes up a lot of memory space.

1.5 Missing pages Interrupt processing

In a paging system, a virtual page can be both in physical memory and possibly on disk. if the corresponding page of the virtual address of the CPU is not in physical memory, it will result in a missing page fault, and the fault service program is responsible for locating and loading the required virtual pages into memory . The process of missing pages interrupts is as follows, omitting many of the steps in the middle, leaving only the core steps:

Second, the page replacement algorithm

If a page break occurs, you need to transfer the required pages from disk into memory. If the memory does not have extra space, you will need to select a page in the existing page to replace it. With different page substitution algorithms, the order of page changes will vary.

2.1 Page permutation target 2.2 random replacement algorithm 2.3 FIFO algorithm 2.4 Second chance algorithm 2.5 clock algorithm 2.6 Optimal replacement algorithm 2.7 NRU (recently unused) algorithm 2.8 LRU (least recently used) algorithm 2.9 working set and working set clock algorithm reference

Heng Hengming, "The philosophical principles of the operating system", mechanical industry Press

Zhou Xurong

Source: http://edisonchou.cnblogs.com

The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to give the original link.

Operating system core principles-5. Memory Management (bottom): Paging memory management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.