Operating system study notes-memory management

Memory management

1. The concept of memory management

The functions of memory management are:

  • Allocation and recovery of memory space .
  • Address translation . The logical address in the program cannot be the same as the physical address of the main memory, so the memory must provide an address conversion function to convert the logical address into a physical address.
  • Memory space expansion . Use virtual storage technology or automatic coverage technology to logically expand memory.
  • Storage protection . Ensure that each job runs in its own storage space without interfering with each other.

1.1. Program loading and linking

To create a process, you first need to load programs and data into memory. The user source program becomes a program that can be executed in the memory, usually the following steps are required:

  • Compile. The user source code is compiled into several target modules by the compiler;
  • link. The linker links the compiled set of object modules and required library functions together to form a complete load module;
  • load. The loading program loads the loading module into the memory to run.

Insert picture description here

The program links the following three ways:

  • Static link . Before the program runs, link the target modules and the library functions they need into a complete executable program, and do not separate them later;
  • Dynamic link when loading . When loading the memory, use the method of loading and linking;
  • Dynamic link at runtime . Facilitate the sharing of target modules.

When the memory loading module is loaded into the memory , there are also three ways:

  • Absolutely loaded . The compiler will generate the absolute address of the target code.
  • Repositionable loading . Static relocation, the process of modifying instructions and data in the target during loading is called relocation. Address conversion is usually completed at one time during loading, so it is called static relocation.
  • Dynamic runtime loading . Dynamic relocation. Address conversion is postponed until the program is actually executed. Need the help of a relocation register.

1.2, memory protection

Before memory allocation, it is necessary to protect the operating system from the influence of user processes and to protect the user processes from being affected by each other. There are two methods for memory protection:

  1. The CPU is provided a pair of upper and lower register , storing the highest address and the lowest address in the memory of the user operations ;
  2. Use **relocation register (or base address register) and boundary address register (length-limiting register)** to achieve this protection. The relocation register contains the smallest physical address value, and the boundary address register contains the maximum logical address .

1.3, coverage and exchange

Overwriting and swapping are methods used to expand memory in a multiprogramming environment .

1.3.1, coverage

The basic idea of ​​coverage: Because the program and data are not accessed at all times when the program is running, the user space can be divided into a fixed area and several coverage areas . Put the frequently active part in the fixed area, and segment the rest according to the calling relationship . First put those segments that will be accessed into the coverage area, and put other segments in the external memory. Before calling them, the system transfers them into the coverage area to replace the segments in the coverage area.

1.3.2, exchange

The basic idea of ​​exchange: move the program in the waiting state from the memory to the auxiliary storage and free up the memory space. This process is called swap out ; the process of moving the program that is ready to compete for CPU operation from the auxiliary storage to the memory, this process Called swap-in .

The exchange technology is mainly carried out between different processes (or jobs), while the overlay is used in the same program or process .

1.4 Management Method of Continuous Distribution

Continuous allocation refers to the allocation of a continuous memory space for a user program , mainly including: single continuous allocation, fixed partition allocation and dynamic partition allocation .

1.4.1, single continuous distribution

In this case, the memory is divided into the system area and the user area . The system area is only used by the operating system, usually in the lower address part; the user area is the memory space provided for the user except the system area. This method does not require memory protection, because there is always only one program in the memory .

1.4.2, fixed partition allocation

It divides the user memory space into several fixed-size areas, and each area only loads one job . Fixed partition allocation has two different methods when dividing partitions :

  • The partition sizes are equal.
  • The partition size varies.

In order to facilitate memory allocation, it is usually divided into queuing by size, and a partition description table is established for it , in which each entry includes the starting address, size and status of each partition (whether it is allocated) .

1.4.3, dynamic partition allocation

This partitioning method does not pre-divide the memory, but dynamically establishes the partition according to the size of the process when the process is loaded into the memory, and makes the partition size just suitable for the needs of the process . Therefore, the size and number of partitions in the system are variable. But over time, a lot of external debris will be generated . These external debris will be resolved through compact technology.

When the process is loaded or swapped into the memory, if there are multiple large enough free blocks in the memory, the operating system must determine which memory block to allocate to the process. This is a dynamic allocation strategy. Consider the following algorithms:

  • First adaptation algorithm . Idle partitions are sorted by address in ascending order, search sequentially when allocating memory, and find the first partition whose size can meet the requirements;
  • Best fit algorithm . Free partitions are sorted by capacity; the worst performance will generate the most external fragmentation.
  • Worst adaptation algorithm . Free partitions are sorted by decreasing capacity;
  • The loop adapts to the algorithm for the first time . Evolved from the first adaptive algorithm, when allocating memory, the search starts from the location of the last search result.

1.5. Discontinuous distribution management method

Discontinuous allocation allows a program to be scattered into non-adjacent memory partitions . Discontinuous allocation management methods are divided into paging storage management methods and segmented storage management methods according to whether the partition size is fixed .

1.5.1, paging storage management method

The idea of ​​paging: divide the main memory space into equal and fixed blocks, relatively small, as the basic unit of main memory. Each process is also divided in units of blocks . When the processes are executed, they apply for block space in the main memory one by one in units of blocks . In-page fragmentation will occur, but the capacity will be small.

1.5.1.1 Several basic concepts of paging storage:
  1. Page and page size . The blocks in the process are called pages, and the blocks in memory are called page frames or page frames . External memory is also divided in the same unit, which is directly called a block . The process needs to apply for main memory space during execution, that is, to allocate available page frames in the main memory for each page, which results in a one-to-one correspondence between pages and page frames.

    To facilitate address conversion, the page size is an integer power of 2.

  2. Address structure . Logical address structure of paging storage management:

    31 … 12 11 … 0
    Page number P In-page offset W

    The address structure determines how large the address space of the virtual memory is .

  3. Page table . In order to find the physical block corresponding to each page of the process in the memory, the system establishes a page table for each process, which records the physical block number of the page in the memory . The page table generally exists in the memory . The page table is composed of page table entries and consists of two parts. The first part is the page number, and the second part is the block number in the physical memory . The second part of the page table entry and the second part of the address together form the physical address . The function of the page table is to realize the address mapping from page number to physical block number !

Insert picture description here

1.5.1.2, basic address translation

The task of address conversion is to convert logical addresses into physical addresses in memory, and address conversion is implemented with the help of page tables.

Insert picture description here

A page table register (PTR) is usually set in the system to store the starting address F of the page table in the memory and the page table length M. When the process is not executed, the start address and length of the page table are stored in the process control block. When the process is executed, the start address and length of the page table are stored in the page table register. Suppose the page size is L, the conversion process from logical address A to physical address E is as follows:

  1. Calculate page number P (P = A/L) and page offset W (W = A%L) , generally given in decimal;
  2. Compare page number P and page table length M , if P >= M, an out-of-bounds interrupt will be generated, otherwise the execution will continue;
  3. Page table entry address corresponding to page number P in the page table = page table start address F + page number P * page table entry length , take out the page table entry content b, which is the physical block number.
  4. Calculate E = b * L + W and use the obtained E to access the memory.

Page management only needs to give an integer to determine the corresponding physical address, because the page size L is fixed. Therefore, the address space in page management is one-dimensional .

There are two main problems with page management: (1) Each memory access operation requires a logical address to physical address conversion, and the address conversion process must be fast enough, otherwise the memory access speed will be reduced; (2) Each The process introduces the page table for the storage mapping mechanism. The page table cannot be too large, otherwise the memory utilization will be reduced.

1.5.1.3, address translation with fast table

If the page table is all in the memory, access to a data or an instruction must access the memory at least twice; the first time is to access the page table to determine the physical address of the accessed data or instruction; the second time is to change the address Access data or instructions . To this end, a high-speed cache with parallel lookup capability —fast table , also known as associative memory (TLB) , is introduced into the address conversion mechanism to store several page table entries currently accessed to speed up address conversion .

Insert picture description here

In the paging mechanism with fast tables, the address conversion process is as follows:

  1. After the CPU gives the logical address, the hardware performs address conversion, sends the page number into the cache register, and compares this page number with all page numbers in the fast table;
  2. If a matching page number is found, it means that the page table to be accessed is in the fast table, and the corresponding page frame number is directly obtained from it, and the physical address is formed with the offset in the page. In this way, one memory access can be completed;
  3. If not found, you need to access the page table in main memory.

1.5.2 Basic segmented storage management method

The paging storage method is considered from the perspective of the computer, and the purpose is to improve the utilization of the memory. The paging is realized by hardware and is completely transparent to the user. The segmented storage management method is proposed in consideration of users and programmers to meet the needs of convenient programming, information protection and sharing, dynamic growth and dynamic linking.

1.5.2.1, segmentation

The segment management method divides the logical space according to the natural segments in the user process . The segment requires continuity, and the segment does not require continuity, so the address space of the entire job is two-dimensional.

31 … 16 15 … 0
Segment number S Offset within segment W

The segment number and offset within the segment must be explicitly provided by the user .

1.5.2.2, segment table

Each process has a segment table that maps logical space and memory space. Each segment entry corresponds to a segment of the process, and the segment entry records the starting address and length of the segment in memory .

Segment number Section length The start address of this section in main memory

Insert picture description here

1.5.2.3, address conversion

The segment table entry actually only has two parts: the segment length C and the memory start address .

Insert picture description here

  1. Take the first few digits from the logical address A as the segment number S, and the last few digits as the offset W within the segment, generally given in binary;
  2. Compare the segment number S with the segment table length M, if S >= M, an out-of-bounds interrupt will be generated, otherwise the execution will continue;
  3. The segment table entry address corresponding to the segment number S in the segment table = the start address of the segment table F + the segment number S * the length of the segment table entry, take the first few bits of the segment table entry to get the segment length C. If the offset in the segment >= C, an out-of-bounds interrupt is generated, otherwise the execution continues.
  4. Take out the starting address b of the segment in the segment table entry, calculate E = b + W, and use the obtained physical address E to fetch memory.
1.5.2.4. Protection and sharing of segments

In the segmentation system, segment sharing is achieved through the corresponding entries in the segment tables of the two jobs pointing to the same physical copy of the shared segment . When a job is reading data from the shared segment, you must prevent another job from modifying the data in the shared segment . The code that cannot be modified is called pure code or reentrant code (it is not a critical resource). Such code and data that cannot be modified can be shared.

1.5.3, segment page storage management

The address space of the job is first divided into several segments, and the segments are paged .

Insert picture description here


2. Virtual memory management

2.1, the concept of virtual memory management

Based on the principle of locality , when the program is loaded, a part of the program is loaded into the memory and the rest is left in the external memory to start the program. In the process of executing the program, when the accessed information is not in the memory, the operating system transfers the required part into the memory , and then continues to execute the program. On the other hand, the operating system swaps out the temporarily unused content in the memory to the external memory, thereby freeing up space for the information that will be transferred to the memory. In this way, the system seems to provide users with a memory larger than the actual memory, called virtual memory . Virtual memory is built on the discrete allocation of memory management .

2.2, request paging storage management method

2.2.1, page table mechanism

Page number Physical block number Status is P Access field A Modify bit M Memory address
  • Status bit P: whether it is transferred to the memory;
  • Access field A: used to record the number of times this page has been accessed within a period of time, or how long this page has not been accessed recently, for reference in replacement algorithms;
  • Modification bit M: Identifies whether the page has been modified after being loaded into the memory;
  • External memory address: It is used to point out the address of the page in external memory for reference when loading.

2.2.2, page fault interrupt mechanism

When a page to be accessed is not in the memory, a page fault interrupt is generated, requesting the operating system to load the missing page into the memory. For this reason, the process of page fault is blocked (paging is completed and awakened). If there is a free block in the memory, a block is allocated and its page table entry is modified; if there is no free block, a page must be eliminated.

The page fault interrupt is different from the general interrupt as an interrupt:

  • The interrupt signal generated and processed during the execution of the instruction rather than after the completion of an instruction is an internal interrupt;
  • During the execution of an instruction, multiple page fault interrupts may occur.

2.2.3, address conversion agency

When changing the address, first search the quick table:

  • If the page to be accessed is found, modify the access bit of the page table entry, and then use the physical block number given by the page table entry and the address in the page to form the physical address;
  • If you cannot find the page you want to access, you should look up the page table in the memory, and then compare the status bit P in the page table entry to see if the page has been loaded into the memory. If the page is not loaded, a page fault interrupt will occur. The external memory loads the page into the memory.

2.3, page replacement algorithm

2.3.1, the best (OPT) replacement algorithm

Sequentially scan the last page that appears, and the page that no longer appears in the longest time. The algorithm cannot be implemented.

2.3.2, first-in first-out (FIFO) replacement algorithm

Prioritize the elimination of pages that enter memory earliest

2.3.3, the least recently used (LRU) replacement algorithm

Reverse scan the last page that appeared, and select the page that has not been visited for the longest time recently and eliminate it.

2.3.4, clock (CLOCK) replacement algorithm

A simple CLOCK replacement algorithm associates an additional bit with each frame, which is called a used bit . When a page is loaded into the memory for the first time, the use position of the frame is set to 1; when the page is accessed later, its use bit is also set to 1. When a certain page needs to be replaced, the operating system scans the buffer and looks for a frame with a bit of 0 to be called out. If all are 1, the pointer circulates around the buffer, and all used positions are 0, replacing the page at the original position.

The improved CLOCK replacement algorithm adds a bit of modification . The replacement process is as follows:

  1. Find (0,0), that is, the unmodified page is not visited
  2. Find (0,1), the access location is 0, the modified page is not accessed
  3. Find (0,0), the page visited but not modified
  4. Find (0.1), visit and modify the page

2.4, page allocation strategy

2.4.1. Resident set size

  1. Fixed partition local replacement . It allocates a certain number of physical blocks to each process, and it does not change during the entire run. If the process has a page fault, you can only select a page from the pages in the memory of the process to swap out, and then transfer the required page. The physical block that is locked .
  2. Variable partition global replacement . It allocates a certain number of physical blocks for each process in the system, and the operating system itself reserves a free physical block. When a page fault occurs in a process, the system takes a physical block from the free physical block and allocates it to the process, and loads the missing page. For unlocked physical blocks, physical blocks are allocated as long as the page is missing .
  3. Variable partition local replacement . It allocates a certain number of physical blocks for a process. When a process has page faults, it can only be swapped out from the pages of the process; if the process frequently has page faults, the system allocates several physical blocks for the process. Half-locked physical blocks are dynamically increased or decreased according to the frequency of page faults .

2.4.2. Timing of loading the page

  1. Pre-paging strategy . Load several adjacent pages at once before running .
  2. Request paging strategy . Call in when you need to visit during operation .

2.4.3, where to import from

The external memory in the request paging system is divided into two parts: the file area for storing files and the swap area for storing swap pages . The swap area usually uses a continuous allocation method, while the file area uses a discrete allocation method .

  1. The system has enough swap area space . Transfer in and out from the swap area.
  2. The system lacks enough space in the swap area . All files that will not be modified are directly imported from the file area, and the parts that may be modified are transferred to the swap area when they are swapped out, and imported from the swap area when needed later.
  3. The UNIX way . Files related to the process are placed in the file area, so pages that have not been run should be loaded from the file area. Pages that have been run but have been swapped out are placed in the swap area, so they should be transferred from the swap area the next time they are loaded.

2.5, jitter

In the page replacement process, there is a bad situation: the page that has just been swapped out will be swapped into memory immediately, and the page that has just been swapped in will be swapped out of memory immediately. This frequent paging schedule is called thrashing or thrashing . The main reason for jitter is that the number of pages frequently accessed by a process is higher than the number of available physical page frames .

2.6, working set

The working set refers to the collection of pages to be accessed by the process within a certain period of time . The working set is determined by the time t and the size of the working set window .

. Files related to the process are placed in the file area, so pages that have not been run should be loaded from the file area. Pages that have been run but have been swapped out are placed in the swap area, so they should be transferred from the swap area the next time they are loaded.

Guess you like

Origin blog.csdn.net/qq_36879493/article/details/107976283