Virtual addresses and physical addresses and their mappings

Source: Internet
Author: User

Original: http://blog.chinaunix.net/uid-26983585-id-3364091.html

A while ago in the interview with the director, the director asked me a question such as: you, I say that the physical memory and virtual memory is exactly what. In fact, I have seen this question before, according to my understanding, at that time, so to answer the "process at run time, the operating system to allocate a 4GB address space, that is, the so-called virtual address space, in general, when our program is very large, the actual physical memory can not meet our needs at the time, This time the operating system will use disk space to do virtual memory space, the current process does not need to put the data on disk, when used, in the use of scheduling algorithm to the required data from the disk space to the memory, virtual memory is to expand the capacity of memory, Whenever we want to run a program is compiled after the formation of the only logical space, is not directly run memory space, so it also has a concept of address mapping. "At that time, I feel the answer is very general, I saw the director in the end said a sentence, you go down or put this piece of content in a good look, so today will be good to the concept of a rationale."
First I start with the most basic concept of what is the concept of physical memory, the concept of virtual memory.
physical memory, in the application, nature is the name, physically, the actual memory inserted on the board is how big is how big。 In the CPU concept, physical memory is the size of the memory space where the address line of the CPU can be addressed directly. For example, 8,086 has 20 address lines, then it's addressing space is 1MB, we say 8086 can support 1MB of physical memory, in time we installed 128M Memory bar on the board, we can only say 8086 has 1MB of physical memory space. In the same vein, most of us now use 32-bit machines, and 32-bit CPUs of more than 386 can support up to 4GB of physical memory space.
Let's talk about why there is a difference between virtual memory and physical memory. Running a process, he needs memory is likely to be larger than the sum of memory capacity, such as your memory is 256M, your program is to create a 2G data area, then not all the data can be loaded together into the memory (physical memory), there is bound to be a part of the data to be put on other media (such as hard disk), When the process needs to access that part of the data, it enters physical memory by dispatching. Sovirtual memory is the sum of all memory space when the process is running, and there may be part of it that is not in physical memory, and physical memory is the memory that we normally know. In some places, this virtual memory is also called the memory swap area. The key is not to put virtual memory with the real memory plug on the motherboard to hook up, virtual memory it is "virtual" does not exist, fake it, it is only a memory management of an abstraction.
So what is the virtual memory address and the physical memory address? Assuming your computer is 32-bit, then its address bus is 32-bit, which means it can address 0~0xffffffff (4G), but if your computer has only 256M of physical memory 0x~0x0fffffff (256M), At the same time your process produces an address that is not in the 256M address space, then what should the computer do with it? Before you answer this question, first explain the computer'sMemory Paging mechanism
The computer will produce pages (page) for the virtual memory address space (32-bit 4G), page frames for the physical memory address space (assuming 256M) paging, which is the same size as the page frame, so, here, The number of virtual memory pages is bound to be greater than the number of physical memory page frames. On the computer there is apage table, which is the mapping of virtual memory pages to physical memory pagesOfmore specifically, the map of page number to page frame number。 But the question comes, the number of virtual memory pages > The number of physical memory page frames is not the address of some virtual memory pages will never correspond to the physical memory address space. No, this is how the operating system handles it.The operating system has a page fault featureThe operating system finds a least-used page frame, invalidates it, writes it to disk, then places the page that needs to be accessed into the page frame, and modifies the mapping in the page table, so that all pages are scheduled to be possible.。 This is the procedure for handling virtual memory addresses to physical memory.
Now to answer what is the virtual memory address and the physical memory address.The virtual memory address consists of the page number (associated with the page number in the page table) and the offset。 The page number does not have to be explained, it has been said that the page number corresponds to a page frame map. So, say the offset.The offset is the size of the page (or page frame) I said above, that is, how much data the page (or page frame) can store。 For example, there is a virtual address whose page number is 4 and the offset is 20, then his addressing process is this:first to the page table to find the page number 4 corresponding to the page frame number (for example, 8), if the page is not in memory, then use the invalidation mechanism into the page, otherwise the page frame number and offset to the MMU (CPU memory management unit) constitute a physically real address, and then access to the data in the physical memory。 In summary, the size of the virtual memory address is related to the address bus, and the size of the physical memory address is related to the capacity of the physical memory bar.

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.