Principles of Operating System and Linux Practice Course [Chapter 4 Storage Management]

4.1 Memory Hierarchy

Functions of storage management:

  • memory allocation and deallocation
  • Memory Abstraction and Mapping
  • Storage isolation and sharing
  • storage expansion

At present, computers use a hierarchical storage system
insert image description here
. Main memory: the main object of storage management by the operating system, and an important place for process activities. The main memory has a large capacity and can accommodate multiple processes at the same time. The OS needs to manage memory blocks. I/O operations also rely on main memory.

Information stored on disk and tape can be stored for a long time. Disks and tapes are considered I/O devices and fall under the category of device management. And because disks and tapes are the main storage places for software information, file management also involves the management of disk and tape space.

4.2 Address Relocation, Storage Protection and Storage Sharing

1. Logical address and physical address

Address relocation or address translation: The process of converting a logical address into a physical address when a program is executed.

A logical address is an access address that has nothing to do with the physical location of the program in memory.

A logical address must be converted to a physical address before an access to memory is performed.
insert image description here

The physical address is the address of the memory unit actually accessed by the CPU when the program is running.

A relative address is a special case of a logical address and is a memory location relative to a known point (usually the beginning of the program).

Logical addresses or relative addresses are also known as virtual memory addresses.

Logical address space: The logical address set of the target program of a user job is called the logical address space of the job.

Physical address space: The overall physical address of the actual storage unit in the main memory constitutes the physical address space in which the user program actually runs.
insert image description here
insert image description here
2. Static relocation and dynamic relocation

static relocation

  • Concept: According to the memory location where the program is loaded, the loader converts all logical addresses in the program into physical addresses at one time according to the relocation information, and then the program starts to execute. This relocation method is called static relocation (relocation location loading method).
  • Features: Static relocation does not require hardware support and is easy to implement. Static relocation does not allow a program to move locations in memory.

dynamic relocation

  • Concept: **The address conversion work is interspersed in the process of instruction execution. Every time an instruction is executed, the CPU converts the logical address involved in the instruction. **This relocation method is called dynamic relocation (dynamic runtime loading method) .
  • Features: Dynamic relocation must be implemented with the help of hardware address translation mechanism. Dynamic relocation allows programs to move locations in memory.

The specific operation of dynamic relocation:
After the program is loaded into the memory, the logical address in it remains unchanged, and the starting address of the program memory is loaded into the hardware special register - the relocation register.

Address conversion formula: physical address = logical address + memory start address

Number of relocation registers, program address structure, program physical memory space continuity:

  • There can be multiple relocation registers , which are used for relocation of program segments, data segments, and stack segments.
  • This means that the program address structure is two-dimensional , including segment addresses and offsets within segments.
  • The physical memory space where the program resides can be discrete, not necessarily continuous.

In the Intel x86 CPU, the CS, DS, ES, SS, FS, and GS segment registers function as relocation registers.

Relocation register and process switching:
The content of the relocation register is part of the process context. When the process is switched, the content of the relocation register must be switched together.

Information about relocation registers is usually kept in the process control block . When the process context switches, the content of the relocation register and other information of the current running process are protected in the process control block, and the content of the relocation register and other information of the new process are restored from its process control block, and the process continues to execute from the breakpoint.

3. Storage protection

Storage protection includes:

  • Address out-of-bounds protection : Check all main memory access addresses generated during process execution to ensure that the process only accesses its own main memory area , which is address out-of-bounds protection. Each program can only access its own main memory area and cannot jump to another process, especially cannot access any part of the operating system.
  • Information access protection : When a process accesses the main storage area allocated to itself, the system must check the access rights , such as checking whether reading, writing, execution, etc. are allowed, so as to ensure the security and integrity of the data and prevent intentional or unintentional errors. Destruction of main memory information by operation, this is information access protection.

4. Storage sharing
When multiple processes execute a program, or multiple processes cooperate to complete the same task and need to access the same data structure , the memory management system must provide a storage sharing mechanism for controlled access to the memory shared area .

4.3 Continuous storage management

1. Fixed partition storage management

Contiguous storage management: Contiguous storage management allocates a contiguous storage area to each process.

Currently popular storage management technology: virtual storage management system based on paging and segmentation.

Continuous storage management includes: fixed partition storage management, variable partition storage management

1. Fixed partition storage management scheme
Fixed partition storage management divides the memory space into several continuous areas with fixed positions and sizes. Each continuous area is called a partition, and the sizes of each partition can be the same or different.

The timing of partitioning and the number of partitions: the system operator and the operating system initialization module divide the main memory into partitions with different sizes but fixed positions according to the operation situation of the day when the system is started.

Disadvantages of the fixed partition scheme:
(1) The number and size of partitions have been determined during the system startup phase, which limits the number of active processes in the system and also limits the maximum process that can run under the current partition scheme.
(2) When the length of the partition is greater than the length of the process in it, the storage space is wasted.

Internal Fragmentation: The internal waste of the partition caused by the fact that the partition where the process resides is larger than the process size is called internal fragmentation.

2. The data structure of fixed partition storage management
includes partition number, starting address, length, and occupancy flag
insert image description here

3. Address translation for fixed partition storage management
insert image description here

4. Jobs enter the fixed partition queuing strategy
insert image description here
**one scheduling queue for each partition: **The advantage is to reduce the waste of internal space in the partition; the disadvantage is that it may cause uneven usage of each partition, some can be loaded into the process but not Even if the smallest partition is idle, it cannot be allocated to the process waiting for the smaller partition, and the memory utilization is not high

2. Variable partition storage management

The variable partition storage management method : the partition is divided according to the size of the job, and the time, size and location of the division are all dynamic, which belongs to a dynamic partition method.

1. Variable partition allocation and recovery method
insert image description here
Compression technology can be used to merge adjacent free areas, but compression technology is time-consuming and wastes processor time, and the system needs to have the ability of dynamic relocation

2. Memory allocation data structure
insert image description here

Partition table: free partition table, allocated partition table

Partition allocation method: Find a free area that is enough to accommodate the process from the free partition table, and divide the area into two. Part of it is used to load the job, becomes an allocated area, and its size and start address are registered in the allocated area table. The other part is used as a free partition, modifying the size and starting address of the original free area in the free partition table.

Partition assignment demo:
insert image description here

Memory reclamation method: register the partition where the job resides as a free area in the free partition table, and consider the problem of merging the free partition with adjacent free partitions, and delete the entry corresponding to this area from the allocated area table.

Partition chain: free partition chain, allocated block chain

Free partition chain: The head unit of the free area stores the length of this free area and the start address of the next free area, links all free areas, and sets the head pointer to point to the first free area. The free areas in the linked list are arranged in order according to size or partition address.
insert image description here

3. Algorithm for variable partition allocation
(1) First adaptation allocation algorithm (first adaptation algorithm): Search from the head of the chain sequentially, find the first partition that meets the requirements and then allocate it.
(2) Next-time adaptive allocation algorithm (circular first-time adaptive algorithm): each time it does not search sequentially from the head of the chain, but starts to search from the next free partition of the free partition found last time.
(3) Optimal Adaptive Allocation Algorithm (Best Adaptive Algorithm): Search from the head of the chain each time until a minimum partition that can meet the requirements is found. But free partitions need to be linked in order of size. Fragments easily.
(4) Worst fit allocation algorithm: scan the entire chain of free partitions, and always select the largest free partition for the job to use. Reduce debris generation.
(5) Fast Adaptive Allocation Algorithm: Set up a separate linked list of free partitions for those free areas of frequently used length.

4. Address translation and storage protection
insert image description here

3. Partner system

Buddy system: also known as the buddy algorithm, is a main memory management algorithm that is a compromise between fixed partitions and variable partitions. It was proposed by Knuth in 1973.
The buddy system uses memory blocks of different specifications called buddies that can be divided and combined as partition units.

1. The concept of partners: Two memory blocks of equal size and divided by the same free block with a size of 2i are partners with each other.
insert image description here

2. Partner system memory allocation and recovery

Allocation: The process of using the buddy system to allocate memory space is a process of continuously splitting the free memory area in half until the split memory block is the smallest buddy that is greater than or equal to the process size.

Recycling: The process of reclaiming memory by the buddy system is the process of continuously merging adjacent idle buddies into larger buddy units until the buddies are not free and cannot be merged.

Buddy system memory allocation:
insert image description here

Partner system data structure design:
insert image description here

4. Auxiliary storage management technology with insufficient main memory

Solve the problem of insufficient main memory: mobile technology (compact main memory), switching technology, overlay technology

1. Mobile technology (compact main memory)

  • Method: Move memory jobs, merge small free partitions into large free partitions, and make them satisfy new jobs.
  • Details: After the job is moved, the job base address/length limit needs to be modified
  • Disadvantages: The overhead is high, and the memory area locked due to device input/output operations cannot be moved.
  • Status quo: Modern operating systems have adopted discrete memory allocation technology, and jobs can be allocated to several discrete memory areas on the spot, without moving jobs and merging free memory areas.

2. Swap Technology

  • Method: In order to balance the system load, some processes are temporarily moved out to the disk, and at the same time, a process in the disk is swapped into the main memory to put it into operation. This kind of swap is called swapping.
  • Selection of swapped out processes: When swapping, swap out processes that have exhausted time slices or have lower priority, because they will not be put into operation in a short time.
  • Application: Used to implement virtual storage management technology, swap all or part of the content of a process between internal and external memory, balance system load or logically expand memory space.

3. Overlay technology

  • Idea: Several program segments of a program, or some parts of several programs share a certain storage space in time, which logically expands the memory space. The program can run without loading all of it, and to some extent realizes the function of running a larger program on a small-capacity memory.
  • Disadvantages: Programmers need to specify the coverage relationship of program fragments by themselves, which increases the burden on programmers to manage memory.

4.4 Paging storage management

1. Paging storage management scheme

Storage management classification:

  • Continuous Storage Space Management: Fixed and Variable Partitions
  • Discrete storage space management: paging and segmentation

1. Frames and pages

A page frame is also called a physical block. The paging storage management divides the entire memory into several parts of equal length, and each part is called a physical block or page frame.

A page is also called a page, and the job is automatically divided into several equal parts equal to each physical block by the paging system, and each part is called a page or a page.
insert image description here

Paging system storage allocation method: any page of a job can be loaded into any free physical block of the memory, and it does not require that the physical memory blocks where the logically adjacent pages are located are also adjacent.

Paged discrete storage for processes:
insert image description here

2. Address structure of paging storage management
insert image description here

Page number and page address calculation example:
If the page size is set to 4KB and the address bus width is 32 bits, find: the number of shift bits in the page and the number of page number bits
insert image description here

3. Address translation for paging storage management

Each job/process occupies a discrete physical block in memory, how does the system know which physical block belongs to which job/process? To solve this problem, the operating system needs to create a page table for each job, which registers the 页号-物理块号corresponding information of the job

Since the displacement within the page is the same as the displacement within the block , the address transformation of paging storage management evolves into the mapping from the page number to the physical block number, that is, the physical block number is obtained according to the page number .
insert image description here

Page Table
The page table is used to implement address translation. The page table records the correspondence between logical addresses and physical addresses . The operating system needs to create a page table for each job . The system can accurately access all the data belonging to a job in the memory through the page table. page.

Example of process page table:
insert image description here

4. Hardware support for paging address translation

Paging storage management adopts dynamic relocation technology , and there are two methods for setting relocation registers:

  • The first is to set up a relocation register for each page, and the entire page table is placed in the relocation register, which is expensive.
  • The second method is to store the page table in memory, and the system has anotherPage table start address register and length control registerUsed to store the page table start address and page table length of the currently running job. The process currently occupying the processor is the user of the page table register. Once the process relinquishes the processor, it should also relinquish the page table register.

Paging memory management address translation process:
insert image description here

Paging address conversion example:
If the page size is set to 4KB, and the address bus width is 32 bits, then the number of shift bits in the page is 12 bits, the number of page number bits = 32-12 = 20, which page is the logical address 60000 on? What is the in-page offset?
If the page fits into physical block 1280, what is the physical address?
insert image description here

Two, fast watch

1. The reason for introducing the fast table
The page table is placed in the memory to reduce the speed of program execution.
When the CPU accesses an instruction/data, it needs to access the memory twice.
The first access to the page table obtains the physical block number to form the physical address.
The second The instruction/data is accessed according to the physical address at the same time, and the speed is doubled

When the CPU accesses an instruction/data, the process of accessing the memory twice:
insert image description here

Block table
In order to reduce the problem of doubling the memory access speed in the paging storage management system , a dedicated cache memory is added to the storage management unit to store some of the recently accessed page table entries. This cache memory is called Fast tables or associative memory.

The fast table of Intel 80486 is 32 units

2. Use of fast watch

With the fast table, when looking for the corresponding physical block number according to the page number, first search the local page table in the fast table, if found, the physical block number and the address in the page (also the address in the block) are spliced ​​to form a physical address, according to the The physical address accesses the corresponding memory unit.

If the physical block number is not found in the fast table, then search the memory page table to obtain the physical block number, on the one hand form the physical address, and on the other hand copy the entry into the fast table for the next time the page is accessed again Obtain the physical block number from the fast table at the time.

Checking the quick table and checking the memory page table is carried out at the same time. Once the corresponding item is found from the quick table, the search for the memory page table is stopped immediately.

Paging address translation process with fast table:
insert image description here

3. Fast watch performance analysis

Assume that the time to access the main memory is 100 nanoseconds, the time to access the fast table is 20 nanoseconds, and the hit rate of the fast table can reach 90% when the fast table has 32 units, then the average time for accessing by logical address is: =
(访问快表的时间+访问内存数据的时间)×90%+ (访问内存页表的时间(同时包含访问快表的时间)+访问内存数据的时间)×(1-90%)
( 20+100)×90%+(100+100)×(1-90%)=128 nanoseconds

It is 36% lower than the time for two accesses to main memory (100 nanoseconds x 2 = 200 nanoseconds). If the fast table hits, the memory access is reduced by 1.

3. Paging storage space allocation and release

Paging system storage management data structure:
insert image description here
insert image description here

Performance analysis of the main memory allocation linked list: the number of nodes is an important factor affecting the efficiency of the main memory allocation linked list. When the number of nodes in the linked list is large, the linked list itself consumes more memory space, resulting in low usage efficiency.

4. Paging storage space page sharing and protection

1. Storage sharing
When the paging system implements storage sharing, it must distinguish between:

  • Data sharing: Different jobs are allowed to use different page numbers for the shared data pages , as long as the relevant entries in the respective page tables point to the shared data information blocks.
  • Program sharing: The logical address in the shared program segment must be the same in different process spaces. That is, a unified page number must be specified for shared programs.

When the paging system program is shared, the page number must be uniformly explained: there is only one copy of the shared program page in the memory, and the allocation of different page numbers will cause the virtual addresses in the shared program page to not overlap, and the program page cannot be shared
insert image description here

2. Information Protection
The way for the paging system to realize information protection is to add some flag bits in the page table to indicate the information operation authority of the page: read/write, read-only, executable only, non-executable

Five, multi-level page table

1. The idea of ​​multi-level page table

The page table is stored discretely and loaded on demand. If the page table is large, the physical block of memory occupied by the page table is also allowed to be discrete, and the page table can also be loaded into memory on demand , so it is necessary to create a page table of the page table , that is, the page directory, which is the secondary page table mechanism. More levels of page tables can also be established if needed.

Corollary: No matter how many levels of page tables are needed, the top-level page tables must be fully resident in memory

Two-level page table mechanism:
the system builds a page directory table for each process, each entry in it corresponds to a page table page, and each entry in the page table page gives the corresponding relationship between the page and the page frame. The page directory table is a first-level page table, and the page table page is a second-level page table.

Secondary page table example
insert image description here

2. The address structure of the two-level page table

Logical address structure of two-level page table
insert image description here

3. Address translation of two-level page table

Address decomposition: The logical address sent by the CPU is decomposed into three parts: page directory number, page table page number, and page displacement.

1. Find the first-level page table: find the corresponding entry in the page directory table according to the page directory number, and obtain the physical block number where the page table page is located; 2. Find the second-level
page table: find the page number of the page table in the physical block, and obtain The physical block number of the memory where the page is located, and the physical block number is concatenated with the displacement in the page to form the physical memory address of the data to be accessed.

Six, reverse page table

1. Principle

Mapping rules of the page table: The page table is a mapping table from virtual address to physical address.
insert image description here

Mapping rules of reversed page table: reversed page table is a mapping from physical address to virtual address.
insert image description here
The inverted page table records which page of which process is stored in each physical block in the memory, that is, its content is the page number and the identifier of the process it belongs to.

The system sets a page table entry for each physical block and sorts them by physical block number.

Example of inverted page table:
insert image description here

2. Logical address structure
insert image description here
3. Address transformation
Address decomposition: First, the logical address is decomposed into: process identifier, page number, and displacement within the page.

Retrieve the reversed page table: Then use the process identifier and page number to retrieve the reversed page table to obtain the physical block number where the page is located.

Forming a physical address: If a matching entry is retrieved, the sequence number i of the entry is the physical block number where the page is located, and the block number and the address in the page form the physical address together.

Lookup failure processing: If no matching page table entry is found after searching the entire page table, it indicates that the page has not been loaded into memory, paging is requested or the address is wrong.

Inverted page table address translation diagram:
insert image description here

4.5 Segmented storage management

1. The introduction of segmented storage management

Structural features of the program: The program usually has a certain logical structure, for example, it is divided into several segments, and each segment contains several data blocks or program modules (functions, procedures). Therefore, a program has a two-dimensional address structure: a segment and an offset within the segment.

Example program structure:
insert image description here

Structural characteristics of memory: memory presents a one-dimensional linear structure to users

Inconsistency between paging, partition storage management and program structure: Pure paging and partition storage management regards the program as a linear structure consistent with memory, even if the program is actually a segmented two-dimensional logical structure, which leads to the program in memory It is not easy to compose access according to program logic.

Solution to the problem: use segmented storage management to divide and allocate memory space according to the logical structure of the program

Segmented storage management: Segmented storage management allocates storage in units of segments. Each segment of a job is assigned a continuous main memory space. The storage locations of each segment are not necessarily adjacent, and the size of each segment is different.

2. Logical address structure of segmented storage management

The logical address managed by segmented memory is two-dimensional, including
insert image description here

The segmented address structure is visible to the user, and the user knows how the logical address is divided into segment numbers and displacements within the segment.

The maximum length of each segment is limited by the address structure, and the maximum number of segments allowed in each program is also limited.

3. Address translation mechanism for segmented storage management

Segmented storage management data structure: segment table

The operating system needs to create a segment table for each job to register the segment number of each segment, the memory start address of the segment and the segment length.

Segmented memory management hardware support: segment table control register

The system needs to set a segment table control register to store the start address and length of the segment table of the job currently occupying the processor .

Segment table example:
insert image description here

Segment address translation:
insert image description here

Segment address transformation calculation example:
(1) If the maximum segment length is 16KB and the computer address bus is 32 bits,
how many bits does the segment number occupy?
How many segments does the logical address space contain at most?
insert image description here

(2) There is a hexadecimal logical address (3, 2A60), what is the offset within the segment?
If this segment is loaded into the physical start address 1280, what is the physical address corresponding to this logical address?
insert image description here

Note: 1280 is not the physical block number, but the segment header address, therefore, 1280 can no longer be multiplied by the weight

4. Segment sharing and protection

There is only one copy of the shared segment in memory, and the entries in the different job segment tables point to the base address of the shared segment.

Similar to page sharing, the shared data segment can have different segment numbers in the segment tables of different job processes, and the shared code segment must have the same segment number in the segment tables of different job processes.

Example of segment sharing:
insert image description here

Five. Comparison of segmentation and paging

(1) A segment is a logical unit of information, determined by the logical structure of the source program, and visible to the user; a page is a physical unit of information, independent of the logical structure of the source program, and invisible to the user
.
(2) The segment length can be specified according to the needs of the user. The segment start address can start from any main memory address; the page length is determined by the system, and the page can only start with an address that is an integer multiple of the page size.
(3) In the segmentation method, the address of the source program remains in a two-dimensional structure after being linked and assembled; in the paging method, the address of the source program becomes a one-dimensional structure after being linked and assembled.
insert image description here

4.6 Virtual storage management

First, the principle of virtual memory

insert image description here

1. Definition of virtual memory

In a computer system with hierarchical memory, the function of automatic partial loading and partial swap is used to provide users with an addressable "main memory" that is much larger than the physical main memory capacity and is called virtual memory .

The benefits of virtual memory: small memory runs large processes, small memory runs many processes

The cost of virtual memory:

  • Time is exchanged for space, running large processes or more processes with a smaller memory space will consume more process exchange time
  • saves space, wastes time
  • Enough memory capacity is required when installing large-scale commercial software

2. The basis of realizing virtual memory - the principle of locality of program execution

The storage space accessed by the program is limited to a certain area for a period of time (this is called spatial locality), or program code and data that has been accessed recently will be accessed again soon (this is called temporal locality)

Locality of program execution allows programs to be loaded locally

In a short period of time, only the instructions and data of a certain local module in the entire work space will be executed and accessed. Other parts of the job will not be accessed temporarily.

This allows only the part of the program that needs to be run currently to be kept in memory, while other parts are temporarily left in external memory.

Facts about locality:

First, there are only a few branches and procedure calls in the program, most of which are sequentially executed instructions

Second, the program contains several loop structures, which consist of a small amount of code and are executed multiple times

Third, the depth of procedure calls is limited to a small range, so instruction references are usually limited to a small number of procedures

Fourth, continuous references to data structures such as arrays and records are operations on adjacent data items

Fifth, some parts of the program are mutually exclusive, not used every time it is run

3. Problems to be solved in implementing virtual memory

(1) Unified management of main storage and auxiliary storage
(2) Conversion of logical addresses to physical addresses
(3) Partial loading and partial swapping

The implementation technologies of virtual storage mainly include:
(1) Request paged virtual storage management
(2) Request segmented virtual storage management
(3) Request segmented page virtual storage management

2. Request paging virtual storage management

1. The basic principle of the request paging virtual storage system is
to load one or several pages in the set of all pages before the process starts running. During the running process, when the accessed page is not in the memory, load the required page; if the memory
space When it is full and a new page needs to be loaded, a certain page is eliminated according to a certain algorithm so that a new page can be loaded.

2. Request the page table structure of the paging system
insert image description here

Outer page table
Definition: The outer page table is the correspondence table between the page and the physical address of the disk , which is managed by the operating system. Before the process starts running, the system creates an outer page table for it , and loads the process program page into the external memory. The table is ordered by process page number. In order to save the main memory, the external page table can be stored in the disk, and it will be loaded into the memory when a page fault occurs and needs to be checked. The outer page table structure is
shown below.
insert image description here

3. Hardware support for paging virtual storage system

A paged virtual memory system requires a memory management unit, the MMU .

An MMU usually consists of a chip or a group of chips that accepts a virtual address (logical address) as input and outputs a physical address.

Functions of the MMU:
①Manage the hardware page table registers, load the process page table that will occupy the processor
②Decompose the logical address into page number and page address
③Manage the fast table: find the fast table, load the entry and clear the
entry④ Access the page table
⑤ Issue a page fault interrupt when the page to be accessed is not in the memory, and issue an out-of-bounds interrupt when the page access is out of bounds
⑥ Set and check the reference bit, modification bit, valid bit, protection permission bit and other feature bits in the page table

Entity objects operated by the MMU:
①Page table register: point to the page table
②Page table: access, page fault interrupt, out-of-bounds interrupt, set and check flag
③Fast table: search, load, clear

Differences between page fault interrupts and normal interrupts:
(1) Normal interrupts will respond between two instructions; instructions involved in page fault interrupts need to respond to page fault interrupts during execution.
(2) When the instruction itself or the data processed by the instruction crosses pages, multiple page fault interrupts may occur during the execution of an instruction.

4. The address conversion process of request paging

When a process is scheduled to run on the CPU, the operating system automatically loads the page table start address in the process PCB into the hardware page table base address register. After that, the process starts to execute and access a certain virtual address, and the MMU starts to work
.
①The MMU accepts the virtual address sent by the CPU and decomposes it into two parts: the page number and the address in the page.
②Search the
fast table with the page number as the index. The internal address is spliced ​​to form a physical address, and then access the corresponding memory unit
④ If the fast table is not hit, search the memory page table with the page number as the index
⑤ Find the corresponding entry in the page table, if its status bit indicates that the page is already in memory, Then send the physical block number and the address in the page to form a physical address to access the corresponding memory unit, and at the same time, load the entry into the fast table
⑥ If the status bit of the corresponding entry found in the page table indicates that the page is not in the memory, then Issue a page fault interrupt and request the operating system to process
⑦The storage management software transfers the missing page into the memory and modifies the page table

5. Page fault interrupt processing

Step 1 Suspend the process requesting a page fault

Step 2 Check the external page table according to the page number, and find the physical address of the disk where the page is stored

Step 3 Check whether there is a free page frame in the main memory, if there is one, find one, modify the main memory management table and the corresponding page table entry, go to step 6

Step 4 If there is no free page frame in the main memory, select a page to be eliminated according to the replacement algorithm, and check whether it has been written or modified? If not, go to step 6; if so, go to step 5

Step 5 If the eliminated page has been written or modified, write its content back to the original location on the disk

Step 6 Perform paging, load the page into the page frame allocated by the main memory, and modify the process page table entry at the same time

Step 7 Return to the process breakpoint and restart the interrupted instruction

Summary of page fault handling process:

① Check whether there are free physical blocks in the memory, if so, load the page into the free physical block, and modify the corresponding items of the page table and the memory allocation table at the same time

②If there is no free physical block in the memory, select a page to be eliminated according to the replacement algorithm. If the page has been written or modified, it will be written back to the external memory; otherwise, the page will be simply eliminated. After eliminating the page, modify the corresponding item in the page table, and then transfer the page to the physical block released by the eliminated page

6. Page loading strategy and clearing strategy

Page Loading Policy: Deciding when to load a page into memory

There are two main ways to call in:

  • Page-loading: Only when programs and data need to be accessed, the required pages are loaded into the main memory . Disadvantages: The system overhead of dealing with page fault interruption and paging is relatively large, only one page is adjusted at a time, and the number of disk I/O is large
  • Pre-loading: The system predicts the pages to be used by the process, and loads them into the main memory before use, and loads several pages at a time instead of just one page. Advantages: Loading multiple pages at one time can reduce the number of disk I/O starts and save seek and search time

Page cleanup strategy: When considering when to write a modified page back to external memory,
there are two main ways to clear it:

  • Please page clear: Only when a page is selected and replaced, and it has been modified before, write this page back to the external memory
  • Scheduled cleanup: write all changed pages back to storage before they are needed

7. Page frame allocation strategy: Determine how many page frames the system allocates for each process to load pages

Factors considered when allocating page frames:
insert image description here
insert image description here
insert image description here
insert image description here

8. Page replacement strategy: A strategy to select a memory page for replacement in order to load the external memory page to be accessed

There are two main types:

  • Partial replacement: When a page fault occurs in a process, only the page is eliminated from the page frame of the process to transfer the missing page
  • Global Replacement: Evict pages from any process's page frame in the system when a process faults

9. Page frame allocation and replacement combination strategy

There are three common combined strategies for page frame allocation and replacement:

  • Fixed Allocation Partial Replacement
  • Variable allocation global replacement:
    first allocate a certain number of page frames for each process, and the system reserves a number of free page frames.
    When a page fault occurs in a process, select one from the system free page box for the process, and the number of page faults will be reduced.
    When the free page frames owned by the system are exhausted, a page of any process is selected from main memory to be eliminated, which will increase the page fault rate of that process
  • Variable allocation and local replacement
    ① When a new process is loaded into the main memory, it is allocated to a certain number of page frames according to the application type and program requirements
    ② When a page fault occurs, select a page from the resident page set of the process to replace
    ③ Re-evaluate the process from time to time The page fault rate, increase or decrease the page frame allocated to the process to improve system performance

10. Page fault rate

(1) Page fault rate

For a page access sequence of length A of process P, if the number of page faults in process P during operation is F, then f = F/A is called the page fault rate.

Factors affecting the page fault rate:
① The number of main memory page frames allocated to the process: the larger the number of page frames, the lower the page fault rate, and the smaller the page frame rate, the higher the
page fault rate. Low, the page fault rate is high if the page is small
③The page replacement algorithm determines the page fault rate
④Program characteristics: If the program locality is good, the page fault rate is low; otherwise, the page fault rate is high

(2) Jitter (or thrashing)
In the request paging virtual storage management system, the page that has just been eliminated will be accessed immediately, and it will be eliminated soon after being transferred in, and it will be transferred in soon after being eliminated. When a system is paged so frequently that most of its time is spent paging rather than performing computational tasks, this phenomenon is called thrashing (or thrashing).

11. Fixed allocation partial page replacement algorithm
Common algorithms:

  • Optimal Page Elimination Algorithm (OPT): When a page is transferred and an old page must be eliminated, the eliminated page is the page that will not be accessed in the future or the page that will be accessed after the longest time. OPT can be used to measure the standard of various specific algorithms
  • First-in-first-out page elimination algorithm (FIFO): The first-in-first-out page elimination algorithm always eliminates the page that is first transferred into main memory, or the page that resides in main memory for the longest time (except for resident ).
  • The least recently used page elimination algorithm (LRU): The eliminated page is the page that has not been accessed for a long time in the most recent period

Optimal Page Elimination Algorithm (OPT) Calculation Example:
A program allocates three page frames in the memory, initially empty, and the page direction is 4, 3, 2, 1, 4, 3, 5, 4, 3, 2, 1 , 5.
Analyze the page replacement process with an optimal page elimination algorithm.
insert image description here

First-in-first-out page elimination algorithm (FIFO) example
A program allocates three page frames in the memory, initially empty, and the page direction is 4, 3, 2, 1, 4, 3, 5, 4, 3, 2, 1 , 5.
Analyze the page replacement process with the first-in-first-out page elimination algorithm.
insert image description here


An improvement of the page buffer technology
FIFO replacement algorithm, the strategy is as follows: the eliminated pages enter two queues - the modified page queue and the non-modified page queue.
Pages in the modified page queue are written out in batches from time to time and added to the non-modified page queue;
pages in the non-modified page queue are recycled when they are referenced again, or eliminated for replacement.

The least recently used page elimination algorithm (FIFO) example
A program allocates three page frames in the memory, initially empty, and the page direction is 4, 3, 2, 1, 4, 3, 5, 4, 3, 2, 1, 5.
Analyze the page replacement process with the least recently used page elimination algorithm.
insert image description here

Several implementation methods of the least recently used page elimination algorithm (LRU):

① Citation bit method

A reference flag R is set for each page. When a page is accessed, R is set to 1 by the hardware;
every time t, the flag R of all pages is cleared to 0.
When a page fault interrupt occurs, select a page from the pages whose flag bit R is 0 and eliminate it.
After the eliminated page is selected, the flag bit R of all pages is also cleared to 0.

②counter method

A counter is set for each page, and every time a page is accessed, the counter corresponding to the page is incremented by 1.
When a page fault interrupt occurs, the corresponding page with the smallest count value can be selected to be eliminated, and all counters are cleared to 0.

③ timer method

Set a timer for each page, and whenever the page is accessed, the absolute time of the system is credited to the timer.
When replacing pages, select the page with the smallest timer value to be eliminated.

Second Chance Page Replacement Algorithm:

Algorithm idea: The page that enters the main memory first, if it is still in use recently, still has a chance to remain in the main memory as a newly transferred page.

Algorithm implementation: Check the "reference bit" of the first page of the FIFO algorithm squadron (the page that enters the main memory at the earliest)
insert image description here

Clock page replacement algorithm:
① When a page is first loaded into the main memory, its "reference bit" is set to 1.
② When any page in the main memory is accessed, the "reference bit" is set to 1. ③
When a page is eliminated, scan from the page currently pointed to by the pointer Circular queue, clear the "reference bit" of the page whose "reference bit" is 1, and skip this page; eliminate the page whose "reference bit" is 0, and push the pointer further
④Scanning Circular queue, if no pages are evicted, scan again

The improved algorithm of the clock page replacement algorithm:
combine the "reference bit" r and the "modification bit" m to form four situations

①Not recently cited, not modified (r=0, m=0) - priority elimination
②Recently cited, not modified (r=1, m=0) - r is cleared to 0 during scanning, changing to situation 1 and 3
③ Not cited recently, but modified (r=0, m=1) - second priority elimination
④ Recently cited, also modified (r=1, m=1) - r during scanning Cleared to change to case 1 and 3

12. Working Set Model of Partial Page Replacement Algorithm

Working Set: A collection of pages that a process repeatedly (frequently) accesses within a certain period of time.

The purpose of introducing the working set: Once the set of pages frequently accessed by the process in the near future (that is, the working set) all resides in memory, page fault interruption will no longer occur.

Variation characteristics of the working set:

  • The specific pages contained in the working set and their number vary with the time interval during which the track of process access pages is observed.
  • Therefore, the number of page frames allocated to a process is not fixed, but is dynamically adjusted according to the working set of the process at each moment, so as to ensure that the working set at a specific moment is completely resident in memory.

Parameters for the working set:

  • Virtual time t: The reference time point for determining the working set, which is determined by the access sequence number of a specific page and has nothing to do with the physical clock that accesses the page.
  • Virtual time interval △: also known as working set window or sliding window, that is, the length of historical page access sequence. length.

Summary of the principle of the working set algorithm:
the working set model is based on the most recent and longest unused algorithm based on local replacement of variable allocation.
The replaced page is always the furthest historical page in memory from the current time. If the page is included in the working set at the current moment, the page is not replaced, otherwise the page is replaced.

13. Introduction to Request Segmentation and Request Segment Paging Virtual Storage Management

Request segmented virtual storage management: The segmented virtual storage system stores copies of all segments of the job in the auxiliary storage . When the job is scheduled to run, it first loads one or more segments currently needed into the main memory. Load segments that are not in main memory as they are accessed during execution.

The basic principles of request segment page virtual storage management:
(1) The virtual address is divided into segments based on the logical structure of the program
(2) The real address is divided into page frames with fixed positions and equal sizes
(3) The linear address space of each segment is divided into Pages equal to the size of the page frame, thus forming the characteristics of segmented page storage management.

The data structure of request segment page storage management:
(1) Job table: register all jobs entering the system and the starting address of the job segment table
(2) segment table: at least include whether this segment is in memory, and the segment page The starting address of the table
(3) page table: including whether the page is in the main memory (interrupt bit), the corresponding main memory block number

Guess you like

Origin blog.csdn.net/qq_51453356/article/details/125457558