In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how the Linux system uses virtual memory to deceive applications". In daily operation, it is believed that many people have doubts about how Linux systems use virtual memory to deceive applications. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how Linux systems use virtual memory to deceive applications". Next, please follow the editor to study!
First, make meaningful things interesting.
Physical memory and virtual memory
III. Page changing mechanism in Linux
First, make meaningful things interesting.
Yesterday, I saw the following sentence and sent it to you:
Make interesting things meaningful, make meaningful things interesting!
Dare not say that this is a philosophy of life, but we can find the direction when we do something from this sentence.
For example: in the process of learning the Linux operating system, many boring knowledge points are not fun and interesting things.
But why do we force ourselves to settle down and nibble on those tome books? Because it makes sense!
So, ah, if we can become more interesting in this meaningful thing, then we can mobilize a lot of potential enthusiasm.
In this article, we talk about the platitudes of virtual kernels and physical memory in a simple and easy way.
Physical memory and virtual memory
1. From the perspective of x86 hardware
On x86 platforms, main memory (what we call memory) is responsible for storing instructions and data, second only to CPU.
Without memory, no matter how good the CPU is, it won't work.
Just like the human brain, if there is no memory function, no matter how smart can not be used.
Memory is divided into several storage units, numbered from 0 to the largest storage unit.
CPU locates a memory space through the address bus and reads data from or writes data to the memory through the data bus.
As we all know, the hardware of the same x86 platform can install either Windows operating system or Linux operating system.
In other words, when producing hardware, it doesn't know what kind of program it will run on its own hardware.
Perhaps there will be a master directly write a powerful program, directly to manage a variety of hardware resources, to achieve their own specific functions!
In any case, the x86 platform processor architecture defines its own set of rules to access memory.
Therefore, from an x86 platform hardware point of view, there is only (physical) memory, which has no idea what virtual memory is.
two。 From the operating system's point of view
The most important function of the operating system is that it shields all kinds of hardware resources to the application, provides a more friendly interface, and makes program development easier.
Therefore, the operating system will make full use of the various operating mechanisms of the hardware, and then abstract and package it to provide a stable environment for application developers.
For memory, the operating system manages physical memory downwards and provides virtual memory up to the application.
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 point of view of application developers
A long time ago, when writing applications on a variety of single-chip computers and embedded MCU, we were often faced with a shortage of memory, so it was necessary to optimize our programs and use limited hardware resources to achieve some functions.
I suddenly remembered that many years ago, the old engineer told me that I would want to cry in the face of such limited resources.
With the rapid development of hardware, this kind of shortage of hardware resources seems to be rarely encountered, giving us the impression that I can use memory as much as I want and malloc as much as I want.
Of course, the operating system will impose some limitations, which are only operating system-level limitations, because it has its own considerations.
When an application requests a piece of memory space, in order to meet the needs of the application, the operating system "divides" a piece of space from the virtual memory and returns the start address of the space to the application.
Therefore, from the perspective of application development, we don't care about physical memory or virtual memory.
As long as I am malloc, the operating system gives me enough space! As for where this space is allocated, I don't care!
That is, the application is written for virtual memory, not for physical memory.
Of course, in the end, it must be physical memory that stores data, and it is up to the operating system to worry about how virtual memory maps to physical memory.
Each application can only see its own virtual memory space, which is a continuous space, thus ensuring the isolation between different applications and achieving security purposes.
III. Page changing mechanism in Linux
Now, let's save a PC machine, and the memory strips are at least 8G or 16G. But many years ago, this was a very, very extravagant thing, mainly the price factor.
I remember when I was doing my graduation project, in order to save the code, I went to Zhujiang Road in Nanjing to buy a U disk, 64G, which seemed 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 it has inexhaustible memory resources.
In a 32-bit system, the maximum memory space that an application can access is 2 to the power of 32, or 4 GB, even if the actual physical memory is not that large at this time.
In fact, this is similar to a hotel, if there is an owner, open a hotel, a total of 1000 rooms.
At the same time, the boss has a warehouse with more space, which can hold 10000 beds.
The boss is very smart. He knows two facts:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Tour groups or customers can only see the door and lobby of the hotel, but do not know the layout of the rooms in the hotel.
Tour groups do not come to stay at the same time and at the same time.
So when the boss was publicizing to the outside world, he said: my hotel is very big, there are 10000 rooms, welcome to stay!
To simplify the problem, let's assume that there are 100 people in each tour group.
At some point, when the first three tour groups checked in, the boss thought the room was enough, so he put all 300 people in the hotel room.
After a while, a second batch of four tour groups came, and the total number of accommodation at this time was 700.
The boss began to calculate: there are only 1000 rooms in total. Now there are 700 rooms consumed, and there are still 300 left, which can last for a while.
Before long, the third batch of customers came to the door, this time the number of five tour groups: 500. Because the boss advertised that there are 10000 rooms, plenty of them.
When the boss was faced with the new 500 people, he calmly transferred the first and second batch of 700 people from the room to the warehouse.
We assume that customers are asleep and only need a bed and do not need to consume other resources.
Then put the new 500 people in the room.
At this time, a total of 1200 people checked in, although the hotel has only 1000 rooms, but by borrowing the warehouse to temporarily store sleeping customers, there are still 500 vacant rooms in the hotel.
You can 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 woke up, they found that they were in the same environment as when I fell asleep.
The above process is very similar to the paging of virtual memory in Linux systems:
Virtual memory = 10000 rooms advertised
Physical memory = actual 1000 rooms
Hard drive = warehouse with only beds.
When there is not enough physical memory, the operating system temporarily stores the memory of some physical memory pages on a larger and cheaper hard disk, and then can recycle the physical memory and continue to use it.
How, through this analogy, is there a more perceptual understanding of the virtual memory in the Linux system?
Finally, I would like to ask you about the sentence at the beginning of the article:
When you are faced with something meaningful but boring, you might as well consider adding some meaning to it, and when you are faced with something interesting but meaningless, you can also give it some meaning!
At this point, the study on "how the Linux system uses virtual memory to deceive applications" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.