Chapter 4 Memory Management (Tang Xiaodan's Operating System Notes) Super full and super detailed! ! !

Chapter 4 Memory Management

Memory is an important part of a computer. five components

The memory capacity is very large, but it still does not meet the needs of users.

How memory is managed affects memory utilization and system performance

Memory management is mainly memory management, external memory management is classified into memory management

4.1 Memory Hierarchy

User requirements for storage:

  • Fast, matches the CPU
  • large capacity
  • Cheap
  • The three conditions cannot be met at the same time, all using hierarchical memory

4.1.1 Memory system with multi-layer structure

multilayer structure of memory

General-purpose computer storage hierarchy at least three levels: R (register), M (main memory), S (auxiliary storage)

More levels of high-end computers: R, cache, M, disk cache, fixed disk, removable storage media

insert image description here

4.1.2 Main memory and registers

  1. executable memory

    Registers and main memory are called executable memory

    • Its access mechanism and access speed are different from those of auxiliary storage. Auxiliary storage devices, interrupts, device drivers, etc. are very slow
    • OS memory management is responsible for the allocation, recycling, data movement and other management of executable memory
    • OS resource and file management is responsible for the management of auxiliary storage
  2. main memory

main memory abbreviated as main memory or internal memory

  • Main memory holds data and data while the process is running, also known as executable memory
  • When the program is executed, M and R transmit data to each other, read/write
  • Main memory capacity: B, KB, MB, GB
  • The speed of the main memory is much lower than that of the CPU, so R and cache are introduced
  1. register

Registers are at the same speed as the processor

  • Access to registers is the fastest and can work in harmony with the CPU
  • Expensive, small capacity, equipped with dozens - hundreds of
  • R length: 8b, 16b, 32b
  • Universal R and Permitted R

4.1.3 Cache and disk cache

  1. cache _

    Between CPU and M

    • Access faster than M
    • It mainly backs up the frequently used data in the main memory, reduces the number of times the CPU accesses M, and improves the program execution speed
    • Capacity greater than registers, nKb, nMb
    • Principles of Program Locality (Time/Space)
      • Temporal locality: Access to a certain data is accessed again soon
      • Spatial locality: when accessing a certain data, the data near this data will also be accessed array/link list in the near future
    • Secondary or multi-level cache, I-cache (access instruction), D-cache (access data)
  2. disk cache

    Disk I/O speed is much slower than main memory access speed

    • To alleviate the speed mismatch between the two, set the disk cache
    • Temporarily store some frequently used disk data and information to reduce access times
    • Disk cache is not an actual storage
    • Use part of the storage space of the main memory to temporarily store the I\O information read from the disk
    • Data is transferred between file (disk) – disk cache – main memory – cache – R

4.2 Loading and linking of programs

Processing steps for user programs

Compile: get the target module

linking: The linker produces a complete load module

Load: The loader loads the module into memory

insert image description here

4.2.1 Loading the program

  1. absolute load mode

    • Early single-channel programs used the absolute loading method, DOS
    • The object code of the absolute address (that is, the physical address) is generated after the user program is compiled
    • Program loading location is fixed
    • The relative address (logical address) of the program is the same as the actual address without relocation

    address determination method

    ① Programmers use absolute addresses directly. It is required to be familiar with the memory situation, and all addresses must be changed when the program is modified

    ②Programmers use symbolic (variable) addresses, which are converted to absolute addresses when compiling

  2. Relocatable load mode (static relocation)

    Relocation: The process of modifying the logical address of the target module when it is loaded

    Static relocation: the way in which address translation is completed once when loading and will not change later

    • Absolutely load the object module can only be loaded into the memory location specified in advance, only applicable to the single channel environment

    • In the multi-channel environment, the start address of the target module starts from 0, and other addresses are calculated relative to the start address

    • The logical address is different from the physical address, and the relocatable loading method is adopted

      insert image description here

    • Modify other logical addresses in the module according to the starting address of the module loaded into the memory

  3. Dynamic runtime loading method

    • Static relocation, once a program is loaded into memory, the program cannot be moved in memory

    • Program code often needs to be moved, such as swapping

    • Dynamic relocation - dynamic runtime loading:

      ①The logical address remains unchanged when loading, and all modules loaded into the memory use logical addresses

      ②The address is changed every time it runs

      ③Requires hardware relocation register support

      ④Allow the program to move the location in memory

4.2.2 Program link

Assemble multiple object modules and library functions into a complete load module

  1. static link

    • Link in advance, link before the program runs

    • Link all object modules and library functions into one load module before running

    • Problems to resolve when connecting

      ① Modification of relative address ② Transformation of external call symbols

    • Static link generates a complete load module, which can execute the file, which will not be opened in the future, but can be directly loaded into the memory to run

    • .lib,.a

      insert image description here

  2. load-time dynamic linking

    • When the target module is loaded into the memory, it adopts the method of linking while loading
    • When loading the target module, if an external module call event occurs, the loader will find the external target module, load it into the memory, and modify the relative address in the module

    Advantages :

    ①Easy to modify and update, modifying a module does not affect other modules

    ②Easy to realize target module sharing, link one target module to several application modules

  3. runtime dynamic linking method

    • Static linking and dynamic linking during loading are inefficient and waste memory

    • Runtime dynamic linking: some modules are deferred until they are called

    • Modules that are not called during execution will not be linked and will not be loaded into memory

      Pros : Faster loading process, saves memory

    • .dll,.so

4.3 Continuous allocation storage management method

The earliest distribution method, popular in 1960-1980

Allocate a continuous memory space for each user, and the program logic address and physical address are adjacent

4.3.1 Single continuous allocation

For single program environment

  • The memory is divided into two parts: system area and user area.
  • The system area (low address part) is used by the OS
  • The user area occupies only one user program, and the program exclusively occupies the entire user space
  • Early CP/M, MS-DOS, RT11, etc., no storage protection mechanism

4.3.2 Fixed Partition Allocation

It was first used in multi-channel system storage management, such as the early IBM360 MFT system

  • The user space is divided into several fixed-size regions, and each partition is loaded with a job

  • Partitioning method:

    • The partitions are of equal size. Convenience, practicality, but lack of flexibility
    • Partition sizes vary. Divided according to user needs, flexible allocation
  • Easy to cause waste of storage space

    Example of Fixed Partition Allocation

    Queue up the partitions by size, and create a partition usage table, which includes the starting address,
    size and status of each partition) (allocated or not)

    insert image description here

4.3.3 Dynamic Partition Allocation

  • Dynamically allocate memory according to the actual needs of the process

  • When loading a new job, according to a certain allocation algorithm, a partition is selected from the free partition table or free partition chain for allocation, which has a great impact on system performance

  • Two allocation algorithms:

    • Search for dynamic partition assignments sequentially
    • Search Dynamic Partition Allocation by Index

    Free partition table: record the situation of each free partition, each free partition has an entry: (partition number, partition size, partition start address, status)

    insert image description here

    Free partition chain: set a forward pointer and a backward pointer at the head and tail of each free partition. Link all free partitions into a two-way chain through forward and backward chain pointers

    insert image description here

Dynamic partition memory allocation

  • Allocate from free partition table (chain)

  • Request partition size u.size, size of each free partition m.size, minimum memory fragment size

    insert image description here

Dynamic partition memory reclamation

  • When freeing memory, find the insertion point from the free area (table) according to the first address of the reclaimed area
  1. The recovery area is connected to the previous free partition F1 at the insertion point

  2. The recovery area is connected with the next free partition F2 of the insertion point

  3. The recycling area is adjacent to the money at the insertion point and the last two partitions at the same time

  4. The recycle area is neither adjacent to F1 nor F2, and a new entry is created for the recycle area

    insert image description here

insert image description here

4.3.4 Dynamic Partition Allocation Algorithm Based on Sequential Search

  • System free partitions form a chain

  • Search the partitions on the free partition chain in turn until a partition with a size that satisfies the size is found

    There are 4 types of sequential search

    1. first fit method
    2. cycle first adaptation
    3. best fit
    4. worst fit
  1. First Fit (FF) Algorithm
  • The chain of free partitions is linked in order of increasing address

  • Search sequentially from the beginning of the chain until you find a free partition with a size that meets the requirements

  • Space allocation is divided from the partition, and the remaining free partitions remain in the free chain

  • If not found, memory allocation fails

    features

    ① Prioritize the use of address memory and reserve the free area of ​​high-address memory

    ②It is easy to generate debris

    ③Search from the head of the chain, increasing the search overhead

  1. Cyclic First Fit (NF) Algorithm
  • To avoid leaving many small free areas in the address part, and to reduce the overhead of free partitions

  • The NF algorithm starts searching from the next partition of the free partition found last time

  • A starting search pointer should be set, and a circular search method should be used

    features

    ①Fragment reduction

    ②Reduce search time

    ③ Lack of large free partitions

  1. Best Fit (BF) Algorithm
  • Always allocate the smallest free partition that meets the requirements
  • Require free partitions to be sorted by capacity from small to large
  • The first partition found is the best
  • The effect is not optimal, too many useless fragments
  1. Worst Fit (WF) Algorithm
  • Always pick the largest free allocation

  • The free area is required to be sorted from largest to smallest

    features

    ①Less debris, good for small and medium-sized operations

    ② High search efficiency

    ③ Lack of large space partition

4.3.5 Dynamic Partition Allocation Algorithm Based on Index Search

  • The dynamic partitioning algorithm based on sequential search is not suitable for systems that are too large

    Large and medium-sized use index-based dynamic partitioning algorithms, which are divided into three types:

    1. Fast Adaptive Algorithm

    2. buddy system

    3. hash algorithm

1. Fast adaptation algorithm

  • Also known as the classification search method, the space is allocated according to the capacity

  • The system has multiple free block chains, and each type of partition with the same capacity has a separate partition chain

  • Set up a management index table in the memory, each index entry corresponds to a partition chain, and record the partition chain head pointer

  • Classify free partitions according to the size of the common space used by the process

    distribution method

    ①Find the partition chain that satisfies the smallest condition

    ②Take the first allocation

    advantage

    ①Do not split partitions, no fragments and keep large partitions

    ② High search efficiency

    shortcoming

    ①Returning partitions is complicated and the system overhead is large

    ② Taking the process as the unit, a partition belongs to only one process, which is wasteful

  1. buddy system
  • It is stipulated that the size of all partitions is the K power of 2 (I<K<m, total memory 2 m )

  • When starting to run, the entire memory is a free partition of 2 m

  • Set up a doubly linked list separately for free partitions of the same size, with a total of k free partitions

  • Request to allocate 2 i-1 ≤ n ≤ 2 i , search for 2 i , 2 i+1 ..., memory may be divided multiple times during allocation

  • May merge multiple times during memory recovery

distribution method

Allocate a storage space with a length of n, calculate an i value, 2 2-1 <n<2 i , and search in the free partition linked list with a free partition of 2 i

If found, allocate, otherwise, start searching from 2 i+1 partition linked list

If there is a free partition of 2 i+1 , divide the partition into the same two partitions, which are partners , one of which is used to allocate the other free partition for joining 2 i

If the 2i +1 partition does not exist, then find the 2i +2 partition and search in turn

features

① The performance of allocation recovery time is worse than that of the quick adaptation method, but better than that of the sequential search method

②Memory availability is better than the quick adaptation method, but worse than the sequential search method

③Multiprocessor systems are still widely used

  1. hash algorithm
  • Take advantage of the quick lookup of hash violations, add=hash(key)

  • Construct a hash table with the size of the free partition as the key, and each entry points to the corresponding free partition linked list

  • According to the size of the required free partition, the position of the hash table item is obtained through the calculation of the hash function, and the corresponding free partition linked list is obtained from it to achieve the best allocation

4.3.6 Dynamic Relocatable Partition Allocation

compact

  • Contiguous allocation requires the allocation of a contiguous memory space

  • Cannot take advantage of small free partitions called fragments or fractions

  • By moving the location of the memory operation, the splicing of multiple scattered small free partitions into large partitions is called compaction or splicing

  • After each compaction, the program needs to be relocated

    insert image description here

dynamic relocation

  • Dynamic runtime loading method, all addresses loaded into memory are still relative (logical) addresses, and are converted to absolute (physical) addresses at runtime

  • Improve efficiency with hardware address translation

  • Add a relocation register to store the starting address of the program (data) in the memory

  • The memory address that performs the actual access is the relative address and the relocation register and

  • When compacting, only the important status register needs to be modified with the new starting address

    insert image description here

Dynamic Relocation Partition Algorithm

  • The dynamic relocation partition algorithm is basically the same as the dynamic partition algorithm, except for "compact".

  • When a large enough free partition is found, if the capacity of all small free partitions meets the requirements, compact

  • Allocation failed when unable to compact

    insert image description here

4.4 Swap

switching technology

  • One job is transferred at a time, and the others are swapped to the backup queue of the external memory. The external memory is required to be fast enough to accommodate all jobs.

4.4.1 Swap Technology in Multiprogramming Environment

introduction of swap

  • Swap concepts, why do you want to swap
  • Swap improves memory and cpu utilization and increases system throughput
  • It is widely used, Linux has swap process and swap partition, windows also supports swap function

type of swap

  • Each swap is to swap in and out a certain amount of programs or data from memory

  • Divided into two categories according to the number of swaps at each swap

    ① Overall swap

    • Also called process swapping, it takes the process as a unit to solve the problem of memory shortage, and is widely used in the intermediate scheduling of multi-programming systems.

    ②Page (segment) swap

    • Also known as partial swap, in units of pages or segments, the purpose is to support VMs, used to request pages or request segmented storage management

4.4.2 Swap space management

The main goal of swap space management

  • The OS disk space with the swap function is divided into two parts: the file area and the swap area

    ①The main goal of file area management

    • Most of the disk space is used to store file directories and content. The main goal is to improve the utilization rate of disk storage space and adopt a discrete allocation method.

    ②The main goal of swap space management

    • Store the swapped out process, the main goal is to increase the swap speed , adopt continuous allocation, and less consider external memory fragmentation

Data structure for swap space disk block management

  • In order to manage the free disk blocks in the swap area, the corresponding data structure should be configured to record the usage of free disk blocks in the swap area of ​​external storage

  • Dynamic memory partition allocation is similar, using free disk block table or free disk block chain

  • The free block table should contain two items:

    ①Swap area address, represented by disk block number

    ②Swap area size, represented by the number of disk blocks

Recycling and Utilization of Swap Space

  • Swap partition adopts continuous allocation, and its allocation and recovery are similar to dynamic partition
  • The allocation algorithm can be a first-fit algorithm, a recurrent first-fit algorithm, and a best-fit algorithm
  • Allocation and reclamation operations should be divided or merged according to the usage of the front and back swap partitions

4.4.3 Process swap out and swap in

process swap out

  • When the kernel finds that there is insufficient memory, it wakes up the swap process to perform swap out

  • The swap process transfers some processes in the memory to the swap area to free up memory space

  • The swap out process can be divided into the following two steps

    1. Selecting processes to be swapped out: selection criteria

      ① First select the blocking or sleeping state process

      ② Select a low priority process when there are multiple

      ③ Also consider the residence time of the process in memory

      ④ If there is no blocking process, choose a low priority process

    2. Process swap out process: only non-shared segments can be swapped out

process swapping

  • The swap process executes the swap-in operation at regular intervals
  • Find the process that has been swapped out in the "ready" state. If there are more than one, select the process that has been swapped out for the longest time
  • Swap takes a lot of time. The OS does not start the swap process when it is running normally. It is only swapped when it is found that there is often insufficient memory (for example: page (segment) interruption).

4.5 Paging storage management method

  • discrete memory management

    Contiguous memory allocation has fragments, and the overhead of compactness is high

    Discrete memory allocation allows processes to be scattered into many non-contiguous memory spaces

    ① Paging storage management method

    ② Segmented storage management method

    ③ Segment page storage management method

4.5.1 Basic method of paging storage management

  1. Physical blocks of pages managed by paging

    • The logical address space is divided into several pages, and the physical address is divided into several blocks, all numbered from 0
    • logical page size = physical block size
    • Allocate in units of blocks, and the logical page load of the process does not contiguous to multiple physical blocks
    • The page is too small: reduce the fragmentation in the page, but the page table is long and occupies memory, which affects the efficiency of swapping in and swapping out
    • The page is too large: the page table segment, increase the swap in and swap out, and the fragmentation in the page increases
  2. Address structure for paging management

The logical address structure of the paging system:

insert image description here

  • The displacement is the address in the page

If the page size is L and the logical address is A, then the page number P and the address d in the page are:

Rounding P=INT[A/L], modulo d=[A] MOD L
  1. page table

    • The paging system allows discrete storage of each logical page of a process in any address block of memory

    • Create an image table for each process, referred to as page table

    • The role of the page table: to realize the mapping of logical addresses and physical block numbers

    • Each logical page of a process occupies one table entry

    • Page table entry content: logical page number, physical block number, storage control field

      • Page table example:

      insert image description here

4.5.2 Address conversion mechanism

(Generally realized by hardware) The role of the address conversion mechanism: to realize the conversion from logical address to physical address

  • Since the address in the page corresponds to the address in the physical block, the address conversion is mainly the conversion from the logical page number to the physical block number

  • Address translation is implemented with the help of page tables

  • Address translation occurs frequently and is implemented in hardware

    • 页表用寄存器实现&页表驻留内存
    • 系统设置一个页面寄存器PTR,存放页表首及页表长度
    • 页表首址及页表长度存于进程PCB,运行时装入PTR
    • 分页系统地址变换过程

    insert image description here

Address translation mechanism with fast table

  • The basic paging system needs to access the memory twice to access a data

  • Fast tables, associative registers or TLBs, special caches with parallel lookup capabilities

  • The fast table is used to cache frequently accessed page table entries in the page table

  • Principle of program locality

  • A fast watch with a high hit rate loses about 10% of its speed

  • The address conversion process of the fast table paging system

    insert image description here

4.5.3 Effective time EAT for accessing memory

The time it takes from issuing a logical address access request to accessing the actual physical address unit and fetching data

Let t be the effective time of accessing memory once, then:

①Basic paging system: EAT=2t

② Paging system with fast table: EAT= a×λ+(t+λ)(1-a)+t=2t+λ-t×a

λ is the time to look up the fast table, a is the hit rate

Page table space memory problem

The OS logical space is large, and the page table occupies a large continuous memory space

For example: 32-bit logical address, 4kb page size, 1M entries

  • Solution:
    1. Page tables use discrete memory allocation
    2. Part of the page table is transferred to memory, and others are transferred to disk when needed

4.5.4 Two-Level and Multi-Level Page Tables

two-level page table

  • Solve the difficulty of finding a large continuous memory space to store tables, but the problem of page table space occupation has not been solved
  • Divide the inner page table into page tables and create a page table (outer page table), which records the first memory address of the inner page table
  • Logical address: outer address, outer page address, page address

insert image description here

  • Two-level page table address translation example
    • Add an outer page table register to store the start address of the outer page table

insert image description here

multi-level page table

  • Suitable for 32-bit and its two-level page table

  • For 64-bit, a multi-level page table is required

    The multi-level page table is to divide the two-level page table into pages and load them into different physical spaces

    At present, the maximum logical address space of 64-bit OS is 64TB=2 64 , and the three-level page table is feasible

4.5.5 Inverting Page Tables

  • Large logical space and many page table entries

  • In order to reduce the space occupied by the page table, an inverted page table is introduced, also known as an inverted page table

    normal page table

    One page table for each process, one item for each logical page, storing the IoT block number

    reverse page table

    One page table for the whole system, one table entry is set for each physical block, sorted by the physical block number, the content is the process ID and its logical page number

Address translation for inverting page tables

① According to the process ID and page number, to retrieve the inverted page table

② If retrieved, use the physical block number and page address to access the data

③Conversely, the page is not loaded into the memory, and a page fault interrupt occurs and then reloaded or an error is reported

features

① Reduce page table memory usage

②Each process also needs an external page table to record the address of each page in the external memory

③ Retrieving the inverted page table is a waste of time, which can be solved by hash, and there will be hash conflicts

4.6 Segmented storage management method

The evolution of memory management

  • To improve memory utilization:
    • Continuous allocation: single continuous allocation –> fixed partition allocation –> dynamic partition allocation
    • Contiguous allocation –> paged allocation
  • To meet the programming needs of users and programmers
    • Natural division, sharing, protection, dynamic linking, dynamic growth

4.6.1 Introduction of Segmented Storage Management

Why do you need segmented memory management?

  • Segmented storage management better meets the following needs of programmers:

    ① Convenient programming

    • The user program is divided into many segments according to the logical relationship. Each segment is addressed from number 0 and has its own name and length. address) to facilitate programming, the program is very intuitive and readable

    ② Information sharing

    • Shared process, the function is the unit, the segment is the logical unit of information, and it is unreasonable to use pages to divide

    ③ Information protection

    • Information protection is also based on procedures and functions

    ④ Dynamic growth

    • Data segment dynamic memory allocation, difficult to pre-allocate

    ⑤ Dynamic link

    • The dynamic link should be based on the target program (ie segment) as the unit

4.6.2 Fundamentals of segmented systems

Segmented storage management

  • The user address space is divided into several segments, and each segment defines a set of logical information

  • Each segment has a name and length, and is addressed from 0, using continuous allocation

  • The program address space is divided into multiple segments, each segment has a different length, and the program logic address is composed of segment name (segment number) and segment offset (segment address)

  • The address in the segment address has the following structure:

    insert image description here

segment table

  • In segmented storage management, each segment is assigned a continuous partition

  • A process has multiple segments, and each segment is discretely loaded into different partitions of memory

  • Each process has a segment mapping table ( segment table ), each segment occupies one item, and records the first address and segment length of the segment in memory

  • The segment table is placed in memory (or in R) for address translation

    insert image description here

Segment storage address translation mechanism

  • The system sets a segment table register to store the start address of the segment table and the length of the segment table

  • One access to data requires two accesses to memory

  • A quick table can be used to save frequently used segment information

  • The number of process segments is far less than the number of pages

    insert image description here

The difference between segmentation and paging

paging section
A page is a physical unit of information that is not visible to the user A segment is a logical unit of information, visible to the user
The purpose of paging is to improve memory utilization The purpose of segmentation is to meet user needs
Page size is fixed and determined by the system The segment length is not fixed and is determined by the user program
Paging is a one-dimensional address space Segmented 1D logical space

4.6.3 Information Sharing

Paging system information sharing

  • Segmentation system facilitates sharing and protection of segments

  • Paging systems can also share programs and data, but not as conveniently as segmented systems

  • The paging system requires that the page table entries of the shared code in the page table of each process point to the same physical block

  • Paging system information sharing example

    insert image description here

Segmented system information sharing

  • In the segmentation system, there is only one segment table entry for the shared segment, so sharing is very easy

    insert image description here

4.6.4 Segment page storage management method

  • The paging system uses pages as units to reduce fragmentation and improve memory utilization
  • The segmentation system uses segments as units to better meet user needs
  • The segment page system combines the two, which can not only reduce external fragmentation, but also share protection separately and dynamically link

Basic principles of segment page storage management

  • Combine paging and segmentation, first divide the user program into several sections, and each section is divided into several pages
  • The address structure consists of three parts: the segment number, the page number in the segment, and the address in the page.
  • A process has a segment table and multiple page tables, segment table entries are divided into page table start address and page table length

Examples of Address Spaces and Address Structures

insert image description here

Management segment table page table example

insert image description here

segment page address translation

  • The segment page system configures a segment table register, which stores the segment table start address and segment length
  • One access to data requires three accesses to the memory, and the fast table can be used to increase the conversion speed

Segment page address conversion diagram

insert image description here


  • Reference: Computer Operating System (Fourth Edition) (Tang Xiaodan)

Guess you like

Origin blog.csdn.net/woschengxuyuan/article/details/127430702