OS Review 3.1.0 - Memory

Memory

The program can only be processed by putting the memory into the CPU. The external memory and internal memory have been mentioned above. The I/O speed of the external memory is very slow, while the memory I/O speed is fast, and the CPU I/O speed is also fast.
Therefore, the memory is used to alleviate the difference in I/O speed between the external memory and the CPU
. In order to distinguish where the program data is stored in a concurrent environment, the storage unit of the memory is addressed. The unit is that the storage unit is
addressed by byte, and each storage unit is 1 Byte, 1B, 8bit
is addressed by word, each storage unit is 1 word, 16bit

2^10 = 1 k
2^20 = 1 M
2^30 = 1 G

Basic principles of process operation

command structure

insert image description here
Operation code + fetch data address + store data address, the actual physical address is given here, it should actually be a logical address

Logical address and physical address

The absolute address is the physical address, and the relative address is the logical address. If the starting point is 101110, 101111 is the physical address of the next unit, and the relative address is written as N+1

write program to program run

Compile –> Link –> Load

compile

The compiler compiles the user source code into several object modules (high-level language –> machine language)

Link

The linker links the object module with the required library functions to form a complete load module

load / mount

The loader loads the load module into memory to run

program loaded into memory

The address of the instruction in the loaded module becomes a relative address. There are three loading methods: absolute loading, static relocation, and dynamic relocation

absolutely loaded

Knowing that the program is placed in the memory location, editing the program generates the object code of the absolute address, but it is actually impossible because this is only applicable to the single-program environment

static relocation

After compiling and linking, the address of the loaded module starts from 0, and the address is a logical address. When loading, the address is relocated, and the logical address is converted into a physical address. But the disadvantage is that the memory must have a fully loaded job
. Space, and can no longer move and apply for memory space during operation

dynamic relocation

Loading during dynamic runtime, the address of the loaded module after compilation and linking starts from 0, and the address translation is performed when the program is actually executed. Since the address when loading the memory is a logical address, this requires a relocation Register, used to record the starting address of the module storage

Before the program runs, only part of the code needs to be loaded, and the program can be allocated to discontinuous storage areas, and the memory can be allocated dynamically during runtime, allowing the program to move in the memory

Three Ways to Link

Static link: connect all modules and corresponding library functions into a complete executable module before loading
Dynamic link at load time: link while loading each target module into memory
Runtime dynamic link: required at execution time Link only if target module

memory management

Memory space allocation and recovery

The operating system is responsible for memory space allocation and recovery, records which memory areas are allocated, and determines which memory can be recovered and how to recover it

single contiguous allocation

insert image description here

In this mode, the memory is divided into system area and user area.
The system area is located in the low address part of the memory and is used to store operating system related data. The user area is located in the high address part of the memory. The user program exclusively occupies
the entire user area space.
a user program

Advantages: simple implementation, no external memory fragmentation, overlay technology can be used to expand memory, memory protection is not necessarily required
Disadvantages: it can only be used in single-user, single-task operating systems, there will be memory fragmentation, and the memory utilization rate is not high

fixed allocation

insert image description here
Proportional partitioning, several fixed-size partitions, each partition can only load one job, but the memory utilization rate is still not high. The system
uses the partition description table to record the allocation status of the partitions for management .
Advantages: simple implementation, no external fragments
Disadvantages: The size has been fixed and can only be solved by using coverage, but at the expense of performance
and internal fragmentation

dynamic partition allocation

insert image description here
No pre-allocation, dynamically create partitions according to the size of the process, generate external fragments, and some free partitions are too small to be used, and external fragments need to be solved by patchwork

The system uses the free partition table/free partition chain
Free partition table: record partition size, start address, status
Free partition chain: set the front and rear pointers at the beginning and end of each partition, and record the partition size

Here is how to select free partitions and reclaim partitions

memory allocation

Contiguous allocation and non-sequential allocation

Management of free memory

Free partition table: allocated from the first address of the selected free partition, if there is still free space, update the start address and size of the entry, otherwise delete the entry directly. The chain of free partitions is the
same

First Fit First Fit

Each time the search is started from the lower address, the first matching free partition will be used.
The low address part will generate many free partitions

Best Fit Best Fit

Prioritize the use of smaller, consistent free partitions
, but this creates external fragmentation, which is very difficult to use

Worst Fit

Prioritize the use of larger, consistent free partitions
to solve the problem of memory fragmentation, but when a large process enters, it cannot be placed

Neighborhood Fit Next Fit

Every time you start looking for a suitable free partition, start from the end of the last query to form a loop
Solve the memory fragmentation that is difficult to use due to the low address part caused by the first adaptation, and reduce the search overhead that needs to start from scratch every time
insert image description here

memory recovery

Free partition table: Merge into the adjacent free area, and update the size of the corresponding partition, if there is no adjacent one, add an entry, and record the corresponding data. The free
partition chain is the same

memory expansion

Logically expand the memory space through virtual technology

overlay technology

When the size of the program exceeds the sum of the physical memory
, it is necessary to use the overlay technology to divide the program into segments, the commonly used segments reside in the memory, and the infrequently used segments are transferred into the memory when needed. The memory is divided
into a fixed area and several overlay areas
that reside in the memory. It is placed in the fixed area and will not be called out after being called in.
The infrequently used segments are placed in the overlay area, which is called in when needed and called out when not needed. The
insert image description here
programmer declares the overlay structure, and the operating system automatically completes the overlay, which increases the programming burden. abandoned

switching technology

When the memory space is tight, the system temporarily swaps some processes in the memory out of the external memory, and swaps some processes in the external memory that are ready to run into the memory. The above-mentioned intermediate scheduling is to put the processes in the memory into the external memory. in pending state

The disk space is partitioned into file and swap areas.
The file area is mainly used to store files and pursue the utilization rate of storage space. The management of the file area space adopts a discrete allocation
method. The data is stored in the swap area.
The speed of the swap area affects the overall speed of the system. Therefore, in order to pursue the speed of swapping in and swapping out, the continuous allocation method is adopted. The I/O of the swap area is faster than that of the file. The swap usually occurs when the memory is tight, and stops when it is not tight
.
Swap out blocked, low-priority processes first, consider residency, etc...
As mentioned earlier, the PCB is always in memory

virtual storage technology

address translation

Logical and Physical Address Translation

memory protection

Ensure that processes run in their respective storage spaces without interfering with each other

  • A pair of upper and lower limit registers are set in the cpu to record the upper and lower limit addresses of the process, and when the process accesses the address, an out-of-bounds check is performed
  • The relocation register and the boundary address register, the former stores the starting physical address of the process, and the latter stores the maximum logical address

Guess you like

Origin blog.csdn.net/weixin_51109304/article/details/130922302