Computer storage management comprehensive arrangement (a) the basic principles and requirements of memory management
After reading the chapter of the storage system in the principle of computer composition, combined with the memory management and file management in the operating system, I have a new understanding of the storage management of the entire computer, and here I will summarize it together.
First of all, I will throw out a few new understandings after reading it in general:
- Computer storage management is hierarchical, from cache to memory to external memory, the speed is reduced in turn, the size is gradually increased, and the price is gradually cheaper.
- The speed at which the CPU obtains data from these three layers gradually decreases. We have done so many operations in storage management. In fact, it is to save money at the same time, not only improve the access speed (locality principle, layer-by-layer cache), but also improve the available address space (virtual memory).
- No matter which level, the problems faced are address mapping, space allocation, and replacement strategy when space is not enough (the three are strongly correlated, such as the allocation method of paging, address mapping It is necessary to use the page table, the allocation method of request paging, and the allocation and replacement of space are also strongly correlated).
- Replacement strategies are only needed when there is insufficient space, such as cache replacement, such as request-style paged memory. Like continuous allocation or ordinary paging allocation, there is no replacement strategy. If there is not enough space, it will not be allocated, and there is no problem of which memory to temporarily replace.
- Various space management methods are not independent, but have an inherent progressive relationship. Taking memory allocation as an example, it is continuous allocation at the beginning, but it is found that continuous allocation is prone to fragmentation problems, so it is thought of discontinuous allocation. Discontinuous allocation is divided into segmentation, paging and segment paging, but note that so far, it is only a problem of memory allocation, and the entire program still needs to be completely loaded into memory at the beginning. Therefore, we put forward the concept of virtual memory. In order to match the virtual memory, we upgrade the segmentation, paging and segment page type to request type, and there will be more fields in the page table entry or segment table entry for the external memory address when the page is missing., there will be more fields for the flag when memory is replaced.
- Paging/segment and virtual memory are two different things. Paging is a way of space allocation, and virtual memory is a way to logically expand Memory Space, but virtual memory uses requested paging or segmentation.
- Continuous allocation and basic paging segmentation only have allocation strategies, such as first adaptation algorithm, near adaptation algorithm, etc., but there is no special replacement strategy, there is no replacement when the program is still running, it is all allocated at one time, and it can be allocated. Run and uninstall all at once after running. And its allocation strategy like request paging also involves replacement strategy.
- The memory access path of modern operating systems is roughly as follows: first, the program gives the relative address of virtual memory for instructions or data, then obtains the actual address of virtual memory through address relocation, and then queries whether the segment table or page table has been loaded into memory. In memory, if it is loaded into memory and in cache, it will return directly from cache. If it is in memory and no longer in cache, it will be loaded from memory and updated cache (replacement may occur). If it is not in memory, it will be loaded from page Find the external memory address in the table entry, load it into memory (replacement may occur), and then return to the CPU and update the cache No.
Process of program execution
The creation process first needs to load the program and data into memory, and convert the user source program into a program that can be executed in memory. Usually, the following steps are required:
- compile. User source code is compiled into several target modules by the editor.
Link. A linker links a compiled set of target modules and their required library functions together to form a fully loaded module. - Load. Load the load module into memory to run
There are two more points to note here: - Both absolute and relocatable loads require the program to be fully loaded into memory at the beginning, so their linking method cannot be dynamic linking at runtime.
- Absolute load and relocatable load both directly generate all physical addresses. When the job is loaded into memory, all the required Memory Space needs to be allocated. It cannot be moved in memory during the entire operation, and the absolute load logical address It is exactly the same as the physical address. The address in the static relocation program is relative to the starting address, and the address conversion is completed at one time during loading, so it can only be allocated continuously. When running dynamically, the program can be allocated to discontinuous memory.
Compile
The result of the compile process is multiple target modules, and the addresses within each module are relative addresses starting from 0.
Link
The result of the link is a complete load module, and the address in the load module is also a relative address starting from 0.
There are three ways to link the program:
Static link
Before the program runs, the first target modules and their required library function link into a complete assembly module, after no disassembly, several target modules assembled into a load module, you need to solve two problems:
- Modify the relative address. All target modules after compiling are relative addresses starting from 0. When linking into a loaded module, the relative address needs to be modified
- Transform the external call symbols used in each module into relative addresses.
Dynamic linking on load
When loading a set of target modules compiled by the user source program into memory, the method of loading and linking is adopted. Its advantage is that it is easy to modify and update, and the sharing of target modules is realized.
Runtime dynamic linking
The linking of some target modules is only carried out when the target module is needed during program execution. Any target module that is not used during execution will not be transferred into memory and linked to the load module. Its advantage is that it can speed up the loading process of the program and save a lot of Memory Space.
Loaded into
When loading the memory module into memory, there are also three ways:
Absolute load
Absolute loading is only applicable to single-channel program environments. When compiling, if you know that the program will reside in a certain location in memory, the compile program will generate the target code of the absolute address. The absolute loader loads the program and data into memory according to the address in the load module. Since the logical address in the program and the actual memory address are exactly the same, there is no need to modify the program and data.
The absolute address used in the program can be given during compilation or assembly, or it can be directly assigned by the programmer.
Relocatable loading
In a multi-program environment, the starting addresses of multiple target modules usually start from 0, and other addresses in the program are relative to the starting addresses, so the relocatable loading method can be used. According to the current situation of the memory, load the module into the appropriate location of the memory.
The modification process of instructions and data addresses in the target program during loading is called relocation, and because address conversion is usually completed at one time when the program is loaded, it is called static relocation.
When a job is loaded into memory, it must be allocated all the required Memory Space. If there is not enough memory, it cannot be loaded. In addition, once the job enters memory, it cannot move in memory during the entire run, nor can it apply for Memory Space again.
Enter during dynamic operation
Also known as dynamic relocation. If the program moves in memory, it needs to use dynamic loading mode. After the loader loads the module into memory, it does not immediately convert the relative address in the loaded module to an absolute address, but defers this address translation until the program is actually executed. Therefore, all addresses after loading into memory are still relative addresses. This method requires the support of a relocation register.
Advantages of dynamic relocation:
- Programs can be assigned to discrete storage areas.
- Before the program runs, only part of the code can be loaded and put into operation. Then during the program running, dynamically apply for memory allocation as needed.
- Facilitate the sharing of program segments.
Logical address and physical address
After compiling, each target module starts addressing from unit 0, which is called the relative address (or logical address) of the target module. When the linker links each module into a complete executable program, the linker sequence sequentially forms a unified logical address space (or virtual address space) addressed from unit 0 according to the relative addresses of each module.
When a process is running, the addresses it sees and uses are logical addresses. User programs and programmers only need to know the logical address, while the memory management mechanism is completely transparent. Different processes can have identical logical addresses because these logical addresses can be mapped to different locations in main memory.
Physical address refers to the collection of physical units in memory, which is the final address of address translation. Processes execute instructions and access data during execution, and finally access from main memory through physical addresses. When the loader loads executable code into memory, it must convert the logical address to a physical address through address translation, which is called address relocation.
Memory image of the process
Unlike executable program files stored on the hard disk, when a program is transferred into memory to run, it constitutes a process memory image. A process memory image generally has several elements:
Code segment: The binary code of the program. The code segment is read-only and can be shared by multiple processes.
Data segment: The object processed during program runtime, including global variables and static variables.
- Process Control Block (PCB): Stored in the system area. The operating system controls and manages processes through the PCB.
- Heap: Used to store dynamically allocated variables. Dynamically allocate space to high addresses by calling the malloc function.
- Stack: used to implement function calls. Grows from the largest address in user space to the lowest address.
Code and data segments are sized when the program is called into memory. The stack and heap dynamically expand and contract.
Memory protection
Ensure that each process has an independent Memory Space. Before memory allocation, it is necessary to protect the operating system from the influence of user processes, and at the same time protect user processes from the influence of other user processes. There are two methods of memory protection:
- Set a pair of upper and lower limit registers in the CPU to store the lower limit and upper limit addresses of user jobs in main memory. Whenever the CPU wants to access an address, it compares the values of the two registers respectively to determine whether there is a boundary.
- The relocation register (base address register) and the boundary address register (limited length register) are used. The relocation register contains the minimum physical address, and the boundary address register contains the maximum value of the logical address. These two registers require privileged instructions to modify.
Memory sharing
Not all processes’ Memory Space is suitable for sharing, only read-only areas can be shared. Reentrant code, also known as pure code, is a type of code that allows multiple processes to access at the same time but is not allowed to be modified.
Memory allocation and reclamation
When the operating system develops from single channel to multi-channel, the storage management method develops from single continuous allocation to fixed partition allocation. In order to meet the requirements of different size programs, it develops from fixed partition allocation to dynamic partition allocation. In order to make better use of memory, it has developed from continuous allocation to discrete allocation - page storage management.