Operating system principle memory (I), operating system principle memory

Source: Internet
Author: User

Operating system principle memory (I), operating system principle memory
I. memory address relocation

In assembly instructions, we sometimes see the following commands:

. Text. entry: code entry call 40-main :.......

So where does 40 point to in memory? Is it the actual address of the memory?

Obviously, if it is an actual address, our program must be loaded at the zero address in the memory, but this is definitely a problem. On the one hand, in this case, each program needs to be placed at 0 address; in addition, the memory is 0 address.

Has been occupied by the operating system.

Therefore, 40 must be a logical address (or relative address)

Then, the program needs to modify the logical address in the source code in the memory to the actual physical address (for example, when the program is loaded to memory 1000, both 40 and 300 are modified ), because the program does not always exist in the same location of the memory, it is likely not loaded to the same location when the swap out memory is swapped in again, therefore, it is not advisable to determine the actual physical address during compilation. Here, a more feasible method is to dynamically Add a base address (base) during runtime ).

 

(As shown in, the operating system performs memory scheduling, swap out some programs, and replace other programs)

So where is the base address stored? In Linux, the base address is stored in the PCB. In the operating system, each process has a data structure, which is called a PCB (Process Control Block). When we fork () A process actually creates a PCB and inherits some data from the PCB of its parent process, and inserts the data structure into the process tree of the system.

Therefore, the overall process is as follows:

Compile the program-> fork the PCB (process) -> Find the idle memory space in the memory-> assign the base address of the space to the base field in the PCB-> load the program-> assign the Source Code address to the address in the PCB when executing each program line base Address addition.

Of course, to improve the efficiency, when the operating system switches to the current process, the base field will be put into the register, so the addition is carried out in the register, in addition, the CPU itself provides hardware support for such operations-MMU (Memory Management Unit ).

Ii. Memory segmentation Mechanism

When we usually play games or write code, we think that the processes we use (such as game programs or eclipse) exist in the memory. What is actually?

If you have learned some assembly or C/C ++, you may naturally think that the process is composed of the following:

The process is divided into several segments, each of which has its own characteristics and has different purposes. In fact, as shown in, the 0 address we mentioned above does not start from 0 for the whole program, instead, each segment in the program starts from 0,

Some sections of the program are read-only (such as code segments) and some can be written (such as data segments). If the same processing is performed, it will obviously cause confusion, such as mistakenly writing program segments.

Therefore, the program is not loaded into the memory as a whole during loading, which is usually multipart loading.

Therefore, when loading segments into the memory, address relocation is different from loading the entire program into the memory:

In this case, locating an address becomes the base address of the segment + the offset within the segment. If you use the previous method of loading the entire process, you only need to store one program base address in the PCB, however, multipart loading requires

The base address of each segment is stored, so that only one base field is not satisfied. A table must be saved in the PCB to save the base address of each segment:

For example, according to the process field table, the Assembly command will be relocated during running. The process is as follows: for example, if the first mov command in the figure is executed, the base address of the DS segment is 360 k in the segment table, and then the base address of the DS segment is 100, this is

Actual address.

Each process in Linux uses a process field table (LDT table), and the operating system itself is also a process. It also uses a process field table (GDT table ).

Therefore, the overall process of process address redirection is changed:

Compile a program --> Create a PCB -->Find an idle memory space in a segment (such as a code segment) in the program --> store the memory base address to the LDT table in the PCB.--> Loading of other segments is similar.

--> Execute the program to add the logical address and the base address of the segment in the LDT table.

3. Memory partition and paging 1. Memory Partition

As mentioned above, we need to load our program to the memory first, and find the idle area in the memory during loading. Therefore, we must split the memory into different areas in advance, so how can we reasonably split the memory?

First, we may think of dividing the memory into n regions, but this is obviously unreasonable because our programs occupy different sizes and the space required for each segment is also different.

A natural idea is to allocate data based on the actual size of each segment and record the occupied space and remaining space:

 

 

 

 

In this way, when a segment requests memory, it applies for a segment of memory in the idle partition and records this process in the allocated partition table.

Memory adaptation:

When a segment requests memory, if there are many idle locations of different sizes in the memory, which one is the most reasonable?

1. First Adaptation: select the first location in the idle Partition Table (advantage: fast look-up)

2. Worst fit: select the largest idle area

3. optimal Adaptation: select a location that is the closest to the requested memory size. For example, apply for a 40 K memory, and the memory has a 50 K idle location (the best doesn't mean the best,

Because the best adaptation may cause a large amount of memory fragments)

2. Memory Paging

Although the partition method solves the problem of applying for memory, it is obvious that it will bring a lot of memory fragments, meaning that although there is still a lot of space in our memory, but all of them are scattered spaces.

An error occurred while applying for large memory. As shown in, the gray area is the memory fragment.

In order not to waste these scattered spaces, the operating system will compress the memory and move the segments in the memory to another location. However, mobile processes are obviously inefficient.

This introduces the memory paging mechanism:

As shown in, if the operating system splits the memory into multiple page boxes, and segments are allocated by page when applying for memory, a segment can waste up to one page box. In this case, you do not need to perform time-consuming memory compression operations.

As shown in, a segment is split into multiple page boxes. Obviously, our memory relocation method needs to be further changed.

 

 

 

To be continued ....

References:

Harbin Institute of Technology Operating System Open Class: http://mooc.study.163.com/course/HIT-1000003007

Linux schematic and PCB: http://blog.csdn.net/kangear/article/details/41940091

 

 

 

 

 

 

 

 

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.