Relationship between physical memory and linear address

Source: Internet
Author: User
If you do not know what logical address, linear address, physical address, and virtual address are, please first look:
Http://linux.chinaunix.net/bbs/viewthread.php? Tid = 919019 & extra = Page % 3d1% 26amp % 3 bfilter % 3 ddigest

In the view of hardware engineers and common users, the memory is the memory stick inserted or solidified on the motherboard. They have a certain capacity-for example, 64
MB. But in the eyes of application programmers, they don't care too much about the memory size inserted on the motherboard, but the memory space they can use-they can develop a memory that needs to occupy 1
A program with GB memory and allows it to run on the OS platform, even if this host only has 128
MB physical memory. For OS developers, there is a gap between the two. They both need to know the details of the physical memory and also need to provide a mechanism to provide another memory space for application programmers.
There is no relationship between the memory size and the actual physical memory size.
We define the memory space provided by the physical memory stick on the motherboard as the physical memory space.
The memory space seen by application programmers is defined as linear space. The physical memory size can be different on different hosts. The physical memory size varies with the physical memory size inserted on the motherboard, but it is used by application programmers.
The provided linear space is fixed and will not change with the physical memory, so as to ensure the portability of applications. Although the size of the physical memory can affect the performance of the application program
There is a minimum requirement on the size of the physical memory, but these factors are only for the normal operation of an OS.
The size of the linear space is 4 on the 32-bit platform.
The fixed size of GB is the same for every process (an application can be a multi-process, in the eyes of the OS, it is a process ). In other words, linear space is not shared by processes, but isolated by processes.
Processes all have 4 of the same size
GB linear space. A process's access to a memory address never conflicts with other processes's access to the same memory address. For example, a process can read a linear space address 1234abcdh.
Returns an integer 8, while another process reads the linear space address 1234abcdh to read the integer 20, depending on the process's own logic.
At any time
Only one process is running on one CPU. So for this CPU, at this moment, the entire system has only one linear space, which is oriented to this process. When a process is switched,
Linear Space also follows switching. Therefore, the conclusion is that each process has its own linear space, and its linear space is known only when the process is running. At other times, its linear space is
CPU is unknown. Therefore, each process can have four
GB linear space, but in the eyes of the CPU, there is only one linear space. Linear Space changes with process switching.
Although there is no relationship between the size of the linear space and the size of the physical memory, applications using the linear space will eventually run in the physical memory. Any linear address given by the application must be converted.
Is a physical address to truly access the physical memory. Therefore, the linear memory space must be mapped to the physical memory space. This ing relationship needs to be established by using the data structure specified by the hardware architecture.
We may call it a ing table first. The content of a ing table is the ing between a linear memory space and a physical memory space. OS
Once the kernel tells a CPU the location of a ing table, when the CPU needs to access a linear space address, it converts the linear space address to a physical address based on the content of the ing table.
Space address, and send the physical address to the address line. After all, the address line only knows the physical address.
Therefore, we can easily draw a conclusion that if we give different ing tables,
The physical address that the CPU converts to a certain level-1 space address is also different. Therefore, we create a ing table for each process to map the linear space of each process to the physical space as needed. Both
However, only one application can be running on a certain CPU at a time. When the task is switched, replace the ing table with the response ing table, so that each process has its own linear space and does not affect each other.
. Therefore, at any time, a CPU only needs a ing table to convert the linear space of the current process to the physical space.
--------------------------------------------------------------------------------
2. OS kernel space & process space
Because OS
The kernel must exist in the memory at any time, but the process can be switched. Therefore, there are two parts in the memory at any time.
Kernel and user process. At any time, there is only one linear space for a CPU, so this linear space must be divided into two parts, one for the OS
The other part is used by the user process. Since OS
The kernel occupies a part of the linear space at any time, so for the linear space of all processes, they are OS
The linear space provided by kernel can be exactly the same. That is to say, their respective ing tables are also divided into two parts, one part is the process private ing part, and for OS
The contents of the kernel ing part are identical.
In this sense, we can think that for all processes, they share the OS
The linear space used by the kernel, and each process has its own private linear space. Assume that
Split the GB linear space into 1 gb OS kernel space and 3
In the process space section of GB, the 4 GB linear space of all processes is 1 GB OS
The kernel space is shared, while the remaining 3
The GB process space is private to each process. In Linux, while in Windows NT
The kernel and process each use 2 GB linear space.
--------------------------------------------------------------------------------
3. segment mapping & page Mapping
Only when the content of all linear spaces is placed in the physical memory can they be truly run and operated. Therefore, even though the OS
Both the kernel and process are placed in the linear space, but they must eventually be placed in the physical memory. So OS
Kernel and all processes eventually share the physical memory. At this stage, the physical memory is far smaller than the linear space-the linear space is 4
But the physical memory space is usually only several hundred megabytes, or even smaller. In addition, even if the physical memory has 4
GB, but since each process can have 3 GB linear space (if the private linear space of the process is 3
If the linear space of all processes is put in the physical memory, it is obviously unrealistic. So OS
The kernel must temporarily put some data or code that is not used by some processes out of the physical memory, and provide limited memory to the most needed processes. In addition
Kernel may run at any time, so the OS
It is best to keep the kernel in the physical memory forever. We only exchange process data.
Slave
The ing between a linear space and a physical space requires a ing table. The ing table maps a linear space to a physical memory space of the same size. Theoretically, we can use two ing methods: variable-length ing, and
Fixed Length ing. Variable-length ing refers to ing a variable-length segment to the physical memory according to different needs. The format can be as follows (Linear Space Segment start address, physical space segment start address, and segment length ). Assume that
A process has three segments: 10 m data segments, 5 m code segments, and 8 K stack segments. You can create three contents in the ing table, each of which is for one segment. This seems no problem. However, if
Our actual memory is only 32 MB, of which 10 MB is occupied by the kernel and the physical space left for the process is only 22 MB. when the process is running, it occupies 10 m + 5 m + 8 K of memory space. When
When a process is switched, if another process has the same memory requirements, the remaining 22 m-(10 m + 5 m + 8 K) is obviously not enough, at this time, only some segments of the original process can be swapped out, and
It must be a replacement of the entire segment. This means that we have to replace at least one 10 m data segment, and the cost is very high, because we have to copy the 10 m content to the disk, and the disk I/O is very slow.
Therefore, the result of using a variable-length field ing is that a segment is either completely swapped in or out. However, in reality, not all code and data in a program can be accessed frequently.
The question is only part of all the code data, or even a small part. Therefore, a more effective strategy is to replace only those that are not frequently used and retain those that are frequently used. Instead of the entire segment
Switch in and out. In this way, you can avoid large slow disk operations.
This is the fixed-length ing policy. We divide the memory space into several fixed-length blocks. Each fixed-length block is called
Page. The basic format of the ing table is (the starting address of the physical space page). Because the page is fixed, you do not need to specify its length. In addition, we do not need to specify a linear address in the ing table.
As an index, the corresponding physical address is retrieved from the ing table. When using pages, the policy is as follows: when switching out, we only change out of those inactive pages that are not frequently used, while retaining those active pages
Page. During the conversion, only the pages requested for access are swapped in. pages not requested for access will never be swapped in to the physical memory. This is the request page (Demand
Page) the core idea of algorithms.
This leads to a page size problem: First, we cannot take bytes as the unit, so that the ing table size is the same as the linear space size --
If the entire linear space is mapped, it is impossible to use all linear spaces to store this ing table. As a result, we can also know that the smaller the page, the larger the capacity of the ing table. But we cannot make the ing table
It occupies too much space. However, if the page is too large, it will face the same problem as the uncertain length field ing. Each time a page is swapped out, a large number of disk operations are required. In addition, the minimum unit of memory allocated to a process is
If the page size is 4 MB, even if a process only needs to use 4
The memory size of KB also has to occupy the entire 4
MB page, which is a big waste. Therefore, we must make a compromise between the two. Generally, the page size specified by the platform is 1.
KB to 8 KB, the page size specified by the IA-32 is 4 kb. (IA-32 also supports 4
MB page, you can choose based on your OS usage, usually 4 kb page ).
--------------------------------------------------------------------------------
4. page table
If a 4 kb page is used
For the linear space of GB, 1,048,576 page table entities are required. Each table item occupies 4 bytes, and 4,194,304 bytes are required. Only page tables occupy 4
MB space, which is a huge demand. However, if a process needs to use all linear spaces
MB page table space investment is also necessary.
However, in reality, few programs need to use such a large space. Generally, the process is very small, from several kb to several Mb. Using such a large page table is a waste. What should we do?
One strategy is to create a variable-length page table-we only create a page table with the required length. However, this policy imposes a lot of restrictions and will still cause a large waste of space. Because the page table mechanism uses linear addresses as the cable
To the page table for retrieval. So if we want to make the OS
Kernel uses C0000000h-FFFFFFFFh, that is, 3G
Http://www.xrss.cn/Dev/PC/2007111117550.Html

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.