I understand that Linux memory management

Source: Internet
Author: User

As we all know, memory management is the most basic and important part of the Linux kernel. Understanding the relevant principles, whether it is the understanding of memory, or for everyone to write user-state code is very helpful. Many books, many articles are written related content, but the individual always feel that the content is too complex, not too easy to understand, here want to use my own understanding of the simple way to describe, hope to be helpful. This article by the column template Bo Master original, reprint need to indicate!

allocation of memory

When you write code, you should always allocate memory, different languages, different levels, using different interfaces, regardless of which way, in the Linux system, basically will be called to the C library malloc interface, then allocate memory from malloc start.

malloc is used to allocate a piece of memory, but the memory allocated here is not physical memory, but virtual memory, there is no strict distinction between virtual address, linear address, such as the concept, only to add to the burden, do not go into the concept of physical memory and virtual memory, the book is usually a lot of space introduction, You can simply understand this:

    • Virtual memory is from the perspective of the process, the concept of logic is not actually exist;

    • The physical memory corresponds to the memory on the physical memory strip;

    • The correspondence between virtual memory and physical existence;
    • When the virtual memory is allocated, the corresponding physical memory is not allocated;

Virtual Memory-to-physical memory mapping

A mapping relationship between virtual memory and physical memory by a page table. Page table is a table in memory, can be simply as a hash table, the corresponding relationship between the virtual address and the physical address, each virtual address corresponding to a table entry, through this table, the virtual address can be converted to a physical address, you can establish virtual memory to physical memory mapping relationship.

use of page tables

The page table has, then who will use it?  It can't be the application itself, I have never seen a page table when I write code? Of course, the user sees only the virtual address (virtual memory), the other is transparent to the user ~ The CPU has a hardware unit, called the MMU (memory Management Unit), the page table is for the MMU hardware, the MMU use the page table for virtual address to physical address mapping.  In other words, the address mapping is done by hardware, and the software (including the operating system kernel itself) cares.  All say the software does not care, then we need to tell the page table?  Software simply does not use the page table and, but the creation and maintenance of the page table is the responsibility of the software (operating system kernel), that is, we (software) create virtual memory and physical memory mapping, and then by the hardware to automatically address mapping (conversion), we do not need to care about the specific conversion process.  As I said before, the page table is in memory, and the page table is created by software, and the MMU knows where the page table is. Simply put, need us (software) to tell where it is, how to tell? Of course, write registers. There is a special register (CR3) on the CPU, to which the address of the page table is written, the MMU knows, and then the hardware uses it, and we don't care.

number of page table problems

looks like a page table to complete all the address mappings?  Of course not, if so, there is no need for virtual memory.  Here again involves the new concept: the process, which is the most basic concept of the operating system, in fact, not "new." System, all tasks are running in a process mode, each process has its own independent virtual address space, as if it is complicated, simply said that each process has its own virtual memory, independent representatives, other processes do not see their own virtual memory, it means that  Each process requires a separate virtual memory-to-physical-memory mapping, meaning that each process requires its own page table. So, how many processes there are in the system, how many page tables there are.

Page Table Creation

To put it simply, the discussion of the Linux page table is the page table that discusses the Linux process: the creation and updating of the Linux page tables are included in the process creation and update. The current Linux kernel uses a copy-on-write method, when a Linux process is created, the page table of the parent process is completely copied, and the page table of the parent-child process is set to write-protect (that is, when the address is written, it generates a fault, etc.).     Then the parent-child process who writes data to the address space, produces a page fault exception, assigns new pages, and resets two pages to writable, in this way the address space of the parent-child process gradually becomes different. According to the above analysis, it is only necessary to discuss the first Process page table initialization, the copy of the page table when the process was created, and the update of the page table when the fault is missing.

What is a missing pages exception

Simply put, is a mechanism on the hardware, when the hardware detects some kind of "wrong", the active trigger, and then automatically jump to the exception handler processing. Exceptions are similar to interrupts, except that interrupts are asynchronous, triggered by peripherals, and that exceptions are synchronous and triggered by the CPU itself.

Well, the time is also relatively late, wrote this, it's bedtime, tomorrow is early again! Go ahead and write these theories tomorrow night! Learning needs to persist!

I understand that Linux 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.