Memory management is essential to an operating system. and. an important part. linux is successful. it is closely related to its excellent memory management. because a system's high sexual desire stability often depends on its memory management mechanism. many of us have suffered 640 k dos attacks.
We have introduced the 386 protection mode. from today on, we will analyze the virtual storage management of linux, for every programmer. they all want to have a fast memory of infinity. However, it is impossible at this stage. Moreover, there may be conflicts between infinity and speed.
To solve infinity. linux introduces a virtual storage system. To solve the problem quickly, linux introduces cache and switching mechanisms so that the storage system is close to the hard disk in capacity and the speed is close to the cache. (Of course, I think this is the actual purpose of the storage system ).
Linux memory management adopts the paging mechanism. it is designed for time-sharing and multi-task. linux can process 256 tasks at the same time (this should be defined with a variable and cannot be thought of at the moment ). at the same time, it uses two levels of saturation mechanism to separate kernel processes and user processes.
In the 0-4G linear virtual address in the 386 protection mode, 3-4G is left to the kernel process. 0-3G is allocated to the user process. the addressing of the kernel space is different from that of the user process in the user space. because the kernel is loaded into the memory at startup. it can be used to directly map the address to 3 GB or above. if you want to access the kernel, you are not allowed to obtain the page table through the pointer in swapper_pg_div.
On the contrary, the addressing of user processes in user space is to get the user's page table through the pointer in the user's page Directory, and direct the pointer to the corresponding physical memory through the page table.
The implementation of Linux virtual memory requires several different mechanisms:
Address ing mechanism
Memory Allocation and recovery
Page Mechanism
Exchange Mechanism
Memory Sharing Mechanism
Before reading the source code. first, based on the operating system knowledge we have learned before. and C language. let's take a look at how these mechanisms are implemented. now you can design it yourself. looking at how others implement it. you can't think of it. or where the efficiency space is lossy. this will make progress. I have said more than once. the implementation of a part of the operating system is very simple. the difficulty is how to integrate a large number of functions into a kernerl.
Address ing is a bridge between virtual memory and physical memory. what it should do may be through several different tables. converts a virtual address to a physical address and a physical address to a virtual address.
As we have said before, because there are systems and users, it must have different data structures. To solve speed and other problems, it will have a hardware buffer.
For its data structure, we can first think about it, such as the virtual address information, the virtual address in that region, and so on.
For the page mechanism, better understanding. because linux is a page-based storage. therefore, there will inevitably be blank pages and use pages. this is a page. page overflow is inevitable. the page is invalid (is it true that similar errors often occur under win98? Of course, the linux memory management cannot be the same as windows, but the basic principle is the same ). therefore. an error occurred on each page. or the page cannot store any excess data. requires the kernel to allocate a new page
At the same time, when fork () is used to generate a new process, we also need to allocate a new leaf surface. This part is about how the process is going inside and how many pages it needs to describe.
After learning <Data Structure>, we learned a lot about memory allocation methods, such as the first proposed and. best and worst. but we can imagine. linux probably won't use them. it must be a partner system. therefore, we can allocate and revoke Basic Algorithms for partner systems. recall. in this way, some of the reader's source code will return imperceivable gains.
As for the exchange mechanism. we can also think about it now. memory and many users' pages. if all the pages have been used up. some pages must be released during redistribution. consider releasing those pages. if the page is not used recently. you can consider using fewer pages in the near future.
This algorithm is probably the page used in the computing memory. When can it be changed. to put it bluntly, it is to calculate a "right" for all pages, and this "right" determines when the page will be released for content such as it. you need to think about pages that are frequently used. you can put it into cahe. (although this part is transparent to programmers, we should understand its principles ).
The last part of shared memory is the same as the shared memory in process communication when I was a beginner in linux programming. that is, add a tag in its data structure that allows access by different processes.
The above is just our speculation about the linux memory management mechanism. What we need to do is read the source code. correct incorrect guesses and learn other people's actual ideas.
Starting from the next diary, we will explain the implementation of these parts separately.
Min Yu Si is cautious in his work