Most people know that computers have virtual memory and physical memory, but can you really tell the difference between virtual memory and physical memory? Do you know why virtual memory exists? Do you know the relationship between virtual memory and physical memory?

What can be learned in this article

Before we begin, let’s talk about what you can learn from reading this article.

  • The origin of virtual memory.
  • Relationship between virtual memory and physical memory.
  • The role of virtual memory.

If you already know these things, don’t read on. If you’re not familiar with them or don’t know much about them, read on and get the answers from the passage.

The origin of virtual memory

Why virtual memory? This goes back to the earliest operating systems, which were not as sophisticated as they are today. In the beginning, programs were loaded directly into physical memory. This leads to the following problems:

  1. Programming is difficult.
  2. Modifying memory data caused the program to crash.

1. Why is it difficult to write programs? Because, the operating system is running a lot of programs at the same time, write the program is the direct operation of physical memory, write time to consider, their own program operation memory address, whether it has been occupied by other programs, if occupied, to rewrite the program, rearrange the program operation address.

Let’s do problem number two. Because it operates directly on physical memory, this means that one program can manipulate all the addresses in memory, which could cause other programs to crash if a malicious program modifies the data in an address used by another program.

The concept of virtual memory is solved the above problems, the concept of virtual memory, the writing of the program is no longer operating physical memory directly, for each program, they are equivalent to have all of the memory space, can operate, will not have to worry about their operating memory address problems occupied by other programs. Also, because the program operates on virtual memory addresses, it does not crash other applications by modifying data in their memory addresses.

At this point, you might ask, how does the virtual memory correspond to the physical memory that the CPU operates on? That’s a good question and I’ll give you the answer below.

Physical memory and virtual memory

Here it is necessary to say the concept of physical memory and virtual memory, some readers may not be very clear.

  • Physical memory: Real memory, which is often referred to as 4G, 8GB, 16GB memory sticks.
  • Virtual memory: a concept, not actual memory. For A Linux system with 4 GB of memory, virtual memory is 4 GB of system memory and the remaining 3 GB of application memory.

Mapping between virtual memory and physical memory

The person that has computer foundation should all know, the CPU of the computer operates is the address of physical memory, but the program that writes now operates is virtual memory address, so how does virtual memory correspond with physical memory? This involves some knowledge of the operating system, virtual memory and physical memory both have the concept of paging, generally a page is 4096 bytes, the current operating system virtual address and physical address to establish a corresponding relationship is the way of page mapping.

Page mapping is a part of the virtual storage mechanism, which was born with the invention of virtual storage. Instead of loading all the data and instructions in a program into memory at once, Page mapping divides the data and instructions in memory and all the instructions on disk into several pages in “Page” units, and all subsequent loads and operations are in pages. Currently, hardware-specified page sizes are 4,096 bytes, 8,192 bytes, 2 MB, 4 MB, etc. The most common Intel IA32 processors use 4,096 bytes of pages. So 512 MB of physical memory has 512 * 1024 * 1024/4 096 = 131 072 pages.

Assuming that our 32-bit machine has 16 KB of memory and each page size is 4,096 bytes, there are four pages,

Page number address
F0 0 x00000000, 0 x00000fff
F1 0 x00001000 x00001fff – 0
F2 0 x00002fff x00002000-0
F3 0 x00003fff x00003000-0

Assuming that all the instructions and data in the program add up to 32 KB, the program is divided into eight pages. We’ll call them P0 to P7. Obviously, 16 KB of memory cannot fit 32 KB of programs at the same time, so we will follow the principle of dynamic loading. If the program starts at P0, the load manager (let’s assume that the loading process is controlled by a guy called the load manager, just like the override manager) finds that P0 is not in memory, allocates memory F0 to P0, and loads the contents of P0 into F0. After running for a while, the program needs P5, so the load manager loads P5 into F1. Thus, when the program uses P3 and P6, they are loaded into F2 and F3, respectively, and their mapping is shown in the figure below.

extension

If that’s not enough for you, let’s briefly describe how Linux loads executables. First, the operating system assigns a process to an executable program, loads the corresponding executable file and executes it. In the presence of virtual memory, the above procedure does three things:

  1. Create a separate virtual address space.
  2. Read the executable header and map the executable to the virtual space.
  3. Set the CPU instruction register to the executable file entry address, start running.

The first is to create a virtual address space. Creating a virtual space is not actually creating the space but the corresponding data structure needed to create the mapping function.

Read the executable header, and establish a virtual space and executable mapping relationship. Why do we have a mapping? Because, when a page error occurs in program execution, the operating system will allocate a physical page from physical memory, and then read the “missing page” from disk into memory, and then set the mapping between the virtual page and the physical page, so that the program can run normally. But it is clear that when the operating system catches a missing page error, it should know where in the executable the page currently required by the program is. This is the mapping between virtual Spaces and executables.

If the virtual memory address of the executable file is the same as the virtual memory address of the executable file, then there may be a problem that the virtual entry address of the two executable programs is the same. How to solve this problem? This is when a piece of hardware called the Memory Management Unit (MMU) was born. One of its functions is address translation, which translates virtual addresses into physical ones

To recap, when an operating system loads an executable program, it first creates a virtual address space, which is essentially a data structure. Then establish executable file and virtual address mapping, mapping function is to know the virtual space corresponding to the executable file which location; Finally is to the CPU’s instruction register set to executable file entry address, start executing the program, the program starts executing time is for entry address where to find data or program execution, if there are no programs or data entry address, can produce a page fault interrupt, then put the parts of the virtual address corresponding to the executable file loaded into physical memory, Then map the physical memory to the virtual memory.

conclusion

This article mainly explains the knowledge of virtual memory, a simple explanation of the mapping between virtual memory and physical memory and the process of loading procedures, I hope you can have a deeper understanding of virtual memory after reading.

This article has been published by the public id AndroidShared

Scan the code to pay attention to the public number, reply “get information” have surprise