Operating System~Memory Management Coverage and Exchange, Continuous Memory Allocation

What is memory and what does it do

Memory is the hardware used to store data. Before the program is executed, it needs to be placed in the memory before it can be processed by the CPU.

In a multi-program environment, there will be multiple programs executed concurrently in the system, which means that the data of multiple programs needs to be placed in the memory at the same time. So, how to distinguish where the data of each program is placed?

Solution: Address the storage unit of the memory

Insert picture description here
It is worth noting that the number of bits of each computer is different, there are 16 bits, 32 bits, then the storage space corresponding to each address is different, and the number of bits of the CPU processing instructions at a time is also different.

How the process works-instructions

Insert picture description here
Insert picture description here
It can be seen that the code we write needs to be translated into instructions that CPu can recognize. These instructions will tell the CPU which address in the memory should be used to store/fetch data, and what kind of processing this data should be done. In this example, the actual storage address (physical address) of the variable x is directly given in the instruction, but when the machine instruction is actually generated, it is not known where the data of the process will be placed. Therefore, the logical address (relative address) is generally used in the instructions generated by the compilation

Logical address VS physical address

Four people in the dormitory travel together, and the student numbers of the four people are 0, 1, 2, and 3 respectively.
When staying at the hotel, the hotel has arranged 4 rooms with connecting room numbers for you.
Four people will stay in the room in ascending order of student number.
For example, students No. 0, 1, 2 and 3 stayed in rooms 5, 6, 7, and 8 respectively.
The numbers 0, 1, 2, and 3 of the four people are actually a "relative location", and the room number they are staying in is an "absolute location".
As long as you know that student О lives in a room with room number N, then student M’s room number must be N+M.
In other words, as long as you know the "relative position" and "starting room number" of each student, you can definitely calculate the "absolute position" of all students

The address in the instruction can also adopt this idea. The instructions generated during compilation only care about the "relative address". When actually put in the memory, find a way to get the "absolute address" according to the starting position

Eg: When compiling, you only need to make sure that the relative address of variable x is 100 (that is, the address relative to the starting address of the process in memory). If the CPU wants to find the actual storage location of x in the memory, it only needs to use the starting address of the process +100.

Relative addresses are also called logical addresses, and absolute addresses are also called physical addresses.

Memory management

The operating system, as the manager of system resources, of course also needs to manage memory. What should be managed?

1. The operating system is responsible for the allocation and recovery of memory space
2. The operating system needs to provide some technology to logically expand the memory space
3. The operating system needs to provide an address conversion function, which is responsible for the conversion between the logical address and the physical address of the program
4. The operating system needs to provide memory protection. Ensure that each process runs in its own storage space without interfering with each other

Memory protection

There are two methods for memory protection:
Method 1: Set up a pair of upper and lower limit registers in CPu to store the upper and lower limit addresses of the process. When the instruction of the process wants to access a certain address, CPu checks whether it is out of range.
Insert picture description here
Method 2: Use the relocation register (also known as the base address register) and the boundary address register (also known as the limited length register) for cross-border inspection . What is stored in the relocation register is the starting physical address of the process. The maximum logical address of the process is stored in the boundary address register.
Insert picture description here

Memory coverage

Early computer memory was very small. For example, the first PC launched by IBM only supported a maximum of 1MB of memory. Therefore, the memory size is often insufficient.
Insert picture description here

Later, people introduced overlay technology to solve the problem of "program size exceeds the sum of physical memory"

The idea of ​​covering technology : divide the program into multiple segments (multiple modules). Frequently used segments are resident in memory, and infrequently used segments are transferred to memory when needed. The memory is divided into a "fixed area" and several "coverage areas".

The segments that need resident memory are placed in the "fixed area", and will not be recalled after being imported (unless the operation is over). The
infrequently used segments are placed in the "overlay area", and they are transferred to the memory when they are needed. To call out the memory, the
Insert picture description here
programmer must declare the coverage structure, and the operating system will automatically cover it. Disadvantages: It is opaque to users, which increases the burden of programming. The overlay technology was only used in the early operating systems and is now a thing of the past.

Memory swap technology

The design idea of ​​swap (swap) technology: When the memory space is tight, the system temporarily swaps out some processes in the memory from the external memory, and swaps some processes in the external memory that have the running conditions into the memory (the process is dynamic between the memory and the disk Scheduling)
The state of the process that temporarily swaps out the external memory and waits is the suspended state (suspended state, suspend). The
suspended state can be further subdivided into ready suspension and blocking suspension
Insert picture description here
. The design idea of ​​the exchange (swap) technology : When the memory space is tight, the system temporarily swaps some processes in the memory out of the external memory, and swaps out some processes in the external memory that have running conditions into the memory (processes are dynamically scheduled between the memory and the disk)

But there will be the following problems

1. Where should the swapped out process be stored in the external memory (disk)?
2. When should
it be swapped out ? 3. Which processes should be swapped out?

Insert picture description here

1. In the operating system with swap function, the disk space is usually divided into two parts: file area and swap area .

The file area is mainly used to store files, and the main purpose is to pursue the utilization of storage space. Therefore, the management of the file area space adopts a discrete allocation method; the swap area space only occupies a small part of the disk space, and the process data that is swapped out is stored in the Change zone. Since the speed of swapping directly affects the overall speed of the system, the management of the swap zone space mainly pursues the speed of swapping in and out, so the swap zone usually adopts a continuous allocation method. In short, the I/o speed of the swap area is faster than that of the file area.

2. Swap is usually performed when many processes are running and memory is tight, and the system load is reduced and then paused. For example: When it is found that many processes are running, page faults often occur, it means that the memory is tight, and some processes can be swapped out at this time; if the page fault rate drops significantly, swap out can be suspended.
3. Blocking processes can be swapped out first; low-priority processes can be swapped out; in order to prevent low-priority processes from being swapped out soon after being transferred into memory, some systems will also consider the resident process in memory Time...
(Note: PCB will be resident in memory and will not be swapped out of external memory)

Memory allocation

Single continuous allocation

In a single continuous allocation method, the memory is divided into a system area and a user area. The system area is usually located in the low address part of the memory and is used to store operating system-related data; the user area is used to store user process-related data.

There can only be one user program in the memory, and the user program exclusively occupies the entire user area.

Advantages: simple implementation; no external fragments; memory can be expanded by overlay technology; memory protection is not necessarily required (eg: the early PC operating system MS-DOS).

Disadvantages: Can only be used in single-user, single-task operating systems; there are internal fragments; memory utilization is extremely low.
Insert picture description here

Fixed partition allocation

In the 1960s, a system supporting multiple programs appeared. In order to load multiple programs in the memory without interfering with each other, the entire user space was divided into several fixed-size partitions. Only one job is loaded into each partition, thus forming the earliest and simplest memory management method that can run multiple programs.

Fixed partition allocation

  1. The partitions are equal in size
  2. Partition sizes vary

The partition size is equal : lack of flexibility, but it is very suitable for the occasion of using a computer to control multiple identical objects (for example: a steel plant has n identical steelmaking furnaces, and the memory can be divided into n equal-sized areas Store n steel-making furnace control programs) Different
partition sizes : Increased flexibility and can meet the needs of processes of different sizes. Divide according to the size of jobs that are often run in the system (for example, divide multiple small partitions, a moderate amount of medium partitions, and a few large partitions).
Insert picture description here
Advantages : Simple implementation and no external fragmentation.
Disadvantages : a. When the user program is too large, all the partitions may not be able to meet the demand. At this time, the overlay technology has to be used to solve the problem, but this will reduce the performance;
b. Internal fragmentation will occur and the memory utilization rate will be low.

Dynamic partition allocation

Dynamic partition allocation is also called variable partition allocation. This allocation method does not divide the memory partition in advance, but dynamically establishes the partition according to the size of the process when the process is loaded into the memory, and makes the size of the partition just suitable for the needs of the process. Therefore, the size and number of system partitions are variable.
Insert picture description here
Insert picture description here

Dynamic partition allocation has no internal fragmentation, but external fragmentation.
Internal fragmentation, in the memory area allocated to a process, if some parts are not used.
External fragmentation means that some free partitions in the memory are too small to be used.
If the total amount of free space in the memory could meet the requirements of a process, but because the process needs a whole piece of contiguous memory space, these "fragments" cannot meet the needs of the process.

The external fragmentation can be solved by compact (patchwork, Compaction) technology.

Dynamic allocation algorithm

First adaptation algorithm
Algorithm idea: Start searching from the low address every time and find the first free partition that can meet the size.
How to achieve: Free partitions are arranged in ascending order of address. Each time memory is allocated, the free partition chain (or free partition table) is sequentially searched, and the first free partition whose size can meet the requirements is found.
Advantages: Simple to implement
Disadvantages: There will be memory fragmentation problems, and it is inefficient to find a suitable location every time

Optimal adaptation algorithm
Algorithm idea: Since dynamic partition allocation is a continuous allocation method, the space allocated for each process must be a continuous whole area.
Therefore, in order to ensure that there is a continuous large area when the "large process" arrives, as much free area as possible can be left, that is, the smaller free area is preferentially used.
How to achieve: Idle partitions are linked in increasing order of capacity. Each time memory is allocated, the free partition chain (or free partition table) is searched sequentially, and the first free partition whose size can meet the requirements is found.
Insert picture description here
Disadvantages: Every time the smallest partition is selected for allocation, more and more small, difficult-to-use memory blocks will be left. Therefore, this method will generate a lot of external debris.

Worst fit algorithm,
also known as the largest fit algorithm (Largest Fit)
algorithm idea: in order to solve the problem of the best fit algorithm-that is, leaving too many small fragments that are difficult to use, you can give priority to using the largest continuous free area in each allocation , So that the remaining free area after allocation will not be too small, which is more convenient to use.
How to achieve: Idle partitions are linked in decreasing order of capacity. Each time memory is allocated, the free partition chain (or free partition table) is searched sequentially, and the first free partition whose size can meet the requirements is found.

Disadvantages: Each time the largest partition is selected for allocation. Although the free area left after allocation can be made larger and more usable, this method will cause the larger continuous free area to be quickly used up. If a "big process" arrives later, there will be no memory partition available.

Proximity adaptation algorithm
Algorithm idea: The first adaptation algorithm searches from the beginning of the chain every time. This may cause many small free partitions in the lower address part, and these partitions must be passed through each time a search is allocated, thus increasing the cost of the search. If you start the search from the position where the last search ended every time, the above problem can be solved.
How to achieve: Free partitions are arranged in increasing order of addresses (can be arranged in a circular linked list). Each time memory is allocated, the free partition chain (or free partition table) is searched from the position where the last search ended, and the first free partition whose size can meet the requirements is found.

Insert picture description here
The first adaptation algorithm must search from the beginning each time, and each time it needs to retrieve a small partition with a low address. But this rule also determines that when there are smaller partitions in the lower address part that can meet the demand, it will be more likely to use the small partition in the lower address part, and it will be more likely to keep the large partition in the high address part (most Advantages of the best adaptive algorithm)

The rules of the proximity adaptation algorithm may lead to the same probability of being used regardless of the free partitions of the low address and high address parts, which leads to the large partition of the high address part being more likely to be used, divided into small partitions, and finally resulting in no large partition. Partitions are available (the shortcomings of the maximum adaptation algorithm). In general, among the four algorithms, the effect of the first adaptation algorithm is better

Guess you like

Origin blog.csdn.net/Shangxingya/article/details/113802996