1 Make meaningful things interesting. Yesterday, I saw the following sentence and gave it to you:
Let interesting things become meaningful, let meaningful things become interesting!
I dare not say this is a philosophy of life, but we can find the guidance when we do something from this sentence.
For example, in the process of learning Linux operating system, many boring knowledge points are not fun and interesting.
But why do we force ourselves to settle down and nibble at those big books? Because it makes sense!
So ah, if we can become more interesting in this meaningful thing, we can mobilize a lot of potential enthusiasm.
In this article, we use a simple and easy way to talk about the old things about virtual kernel and physical memory.
2 Physical memory and virtual memory 1. From the perspective of X86 hardware
On the X86 platform, the main memory (that is, the memory we are talking about) is responsible for storing instructions and data, which is second only to the CPU.
Without the memory, the CPU with the best performance cannot work.
Just like the human brain, no matter how smart you can be without memory.
Memory is divided into several storage units, numbered from 0 to the largest storage unit.
The CPU locates a memory space through the address bus, reads data from the memory or writes data to the memory through the data bus.
As we all know, the hardware of the same X86 platform can install both windows operating system and Linux operating system.
In other words, when producing hardware, it does not know what programs will run on its own hardware.
Maybe some experts will write a powerful program directly to manage various hardware resources and realize their specific functions!?
In any case, the X86 platform processor architecture defines its own set of rules to access memory.
Therefore, from the perspective of X86 platform hardware, there is only (physical) memory, which doesn't know what virtual memory is at all.
2. From the perspective of operating system
The most important function of the operating system is to shield various hardware resources from applications, provide more friendly interfaces, and make program development easier.
Therefore, the operating system will make full use of various operating mechanisms of hardware, and then abstract and package it to provide a stable environment for application developers.
For memory, the operating system manages physical memory downward and provides virtual memory for applications upward.
Virtual memory is a layer of abstraction added by the operating system between the application and physical memory. After adding this abstraction layer, there are many benefits.
3. From the perspective of application developers
A long time ago, when writing application programs on various single-chip computers and embedded MCU, we often faced the situation of very tight memory. This is to optimize our own programs and use limited hardware resources to realize some functions.
(I suddenly think of the old engineer who told me many years ago that he would want to cry in the face of such limited resources!)
With the rapid development of hardware, this shortage of hardware resources seems to be rarely encountered. It gives us the feeling that I can use as much memory as I want.
Of course, the operating system will give some restrictions, which are only at the operating system level, because it has its own considerations.
When an application requests a memory space, the operating system will "divide" a space from the virtual memory in order to meet the needs of the application, and then return the start address of the space to the application.
Therefore, from the perspective of application development, we do not care about physical memory and virtual memory.
As long as I malloc, the operating system gives me enough space! As for where this space is allocated, I don't care!
That is, applications are written for virtual memory, not for physical memory.
Of course, the final storage of data must be physical memory. As for how to establish a corresponding mapping relationship between virtual memory and physical memory, this is what the operating system cares about.
Each application can only see its own virtual memory space, which is a continuous space, so as to ensure the isolation between different applications and achieve the purpose of security.
3 Page change mechanism in Linux now, let's save a PC with at least 8g and 16g memory modules! But many years ago, this was a very, very luxurious thing, mainly due to the price factor.
I remember when I was doing my graduation project, in order to save the code, I went to Zhujiang Road, Nanjing and bought a USB flash disk, 64g, which seems to be 70 yuan.
Compared with memory, the price of hard disk is much cheaper!
Therefore, the Linux operating system makes full use of the hard disk to fool the application, making the application feel that there is never enough memory resources.
In a 32-bit system, the maximum memory space that an application can access is the 32nd power of 2, that is, 4 GB, even if the actual physical memory is not so large.
In fact, this is similar to a hotel. If a boss opens a hotel, there are 1000 rooms in total.
At the same time, the boss also has a warehouse with more space, which can put 10000 beds. The boss is very smart. He understands two facts:
Tour groups or customers can only see the front door and Hall of the hotel, and do not know the layout of the rooms in the hotel;
Tour groups do not stay together at the same time;
Therefore, when the boss publicized to the outside world, he said: my hotel is very big and has 10000 rooms. Welcome to stay!
To simplify the problem, we assume that each tour group has 100 people.
At a certain time, when the first three tour groups checked in, the boss thought the room was enough, so he arranged the 300 people in the hotel room.
After a while, the second batch of four tour groups came. At this time, the total number of accommodation became 700.
The boss began to calculate: there are only 1000 rooms in total. Now 700 rooms are consumed, and there are 300 left. It can last for a while.
Before long, the third batch of customers came to the door. This time, there were five tour groups: 500 people. Because the boss advertised that there are 10000 rooms, which is sufficient.
When the boss faced the new 500 people, he calmly transferred the first and second 700 people from the room to the warehouse.
We assume that customers are sleeping and only need a bed without consuming other resources.
Then arrange the new 500 people to stay in the room.
At this time, a total of 300 400 500 = 1200 people stayed in the hotel. Although the hotel has only 1000 rooms, there are still 500 free rooms in the hotel by borrowing the warehouse to temporarily store customers in sleep.
You can also continue to receive guests..
When it was time for the first three tour groups to check out, the boss secretly transferred the 300 people from the warehouse to the hotel room. When these guests wake up, it's still the same environment when I fell asleep. It's very good...
The above process is very similar to the page changing method of virtual memory in Linux system:
Virtual memory = 10000 rooms for external publicity;
Physical memory = actual 1000 rooms;
Hard disk = warehouse with only beds.
When the physical memory is not enough, the operating system temporarily stores the memory of some physical memory pages on a larger and cheaper hard disk, and then can recycle these physical memory and continue to use it.
How, through this analogy, do you have a more perceptual understanding of virtual memory in Linux system?
Finally, I would like to tell you about the first sentence of Amway:
When you face a meaningful but boring thing, you might as well consider adding some meaning to it; When you face something interesting but meaningless, you can also give it some meaning!
Original title: how does Linux system cheat applications with virtual memory?
The source of the article: WeChat official account: Linux yard, welcome to add attention! Please indicate the source of the article.