Operating System~Basic Concepts and Page Replacement Algorithms of Paging Storage Management

Basic concepts of paging storage management

Traditional continuous memory allocation, no matter what algorithm is used, may cause memory fragmentation.
Using paging address management can effectively reduce memory fragmentation.

Divide the memory space into partitions of equal size (for example: 4KB per partition), each partition is a "page frame", or "page frame", "memory block", "physical block". Each page frame has a number, that is, "page frame number" (or "memory block number", "page frame number", "physical block number") page frame number starts from o.

The address space of the user process is also divided into areas equal to the size of the page frame, called "pages" or "pages". Each page also has a number, that is, "page number", the page number also starts from 0.
(Note: The last page of the process may not be as big as a page frame. Therefore, the page frame cannot be too large, otherwise excessive internal fragmentation may occur)

The operating system allocates memory space for each process in units of page frames. Each page of the process is put into a page box. In other words, there is a one-to-one correspondence between the page of the process and the page frame of the memory.

Each page does not have to be stored consecutively, nor does it need to be in order, and can be placed in non-adjacent page boxes.

Insert picture description here

When processes are continuously stored in memory, how does the operating system realize the conversion from logical addresses to physical addresses?

Insert picture description here
How to realize the conversion of logical address to physical address when paging storage?

When the CPU executes instruction 1, it needs to access the memory unit with logical address 80. How to convert it to a physical address?
Insert picture description here
The memory unit with logical address 80: It
should be on page 1. The starting position of this page in the memory is 450, and the logical address is Relative to the start address of the page for the 80 memory unit, the "offset" should be 30.
Actual physical address = 450 + 30 = 480

1. To calculate the page number corresponding to the logical address
2. To know the starting address of the page corresponding to the page number in the memory
3. To calculate the "offset" of the logical address within the page,
4. Physical address = page starting address +Offset in page

How to calculate:
page number = logical address/page length (take the integer part of the division)
page offset = logical address% page length (take the remainder of the division) the starting position of the page in the memory: the operating system needs something This data structure records the starting position of each page of the process.
Page number = 80/ 50 = 1
page offset = 80% 50 = 30
The starting position of page 1 stored in memory is 450,
so it is 480

Page table

In order to know where each page of a process is stored in memory, the operating system needs to create a page table for each process.

Insert picture description here
The page table corresponds to the block number, and the physical address of each page in the memory can be directly found by the block number * the size of the block

Two-level page table

Problems with single-level page tables

A certain computer system is byte-addressable, supports 32-bit logical addresses, and uses paging storage management. The page size is 4KB and the page table entry length is 4B. If it is 16GB of memory, how many blocks are scored? Once there are more processes, the page table maintained by the operating system is also very large, which is also an unnecessary consumption

Problem 1: The page table must be stored continuously, so when the page table is very large, it needs to occupy a lot of continuous page frames.
Problem 2: It is not necessary to make the entire page table permanent in memory, because the process may only need to access a few specific pages in a period of time.

How to solve the problem of single-level page tables?

Paging the process address space and creating a page table for it to record the storage location of each page. The
same idea can also be used to solve the "page table must be stored continuously" problem, and page tables that must be stored continuously are paged again.

Insert picture description here

Page replacement algorithm

The main difference between request paging storage management and basic paging storage management:
During program execution, when the accessed information is not in the memory, the operating system is responsible for transferring the required information from external storage to the memory, and then continuing to execute the program.
If the memory space is not enough, the operating system is responsible for swapping out temporarily unused information in the memory to the external memory.
Use page replacement algorithm to determine which page should be swapped out

Optimal Replacement Algorithm (OPT)

Optimal replacement algorithm (OPT, Optimal): The page that is selected each time will be the page that will never be used in the future, or will not be accessed in the longest time, so that the lowest page fault rate can be guaranteed.
Example: Suppose the system allocates three memory blocks for a process, and consider that there is a page number reference string (the pages will be accessed in turn): 7,0,1,2,0,3,0,4,2,3 ,0,3,2,1,2,0,1,7,0,1 The
Insert picture description here
best replacement algorithm can guarantee the lowest page fault rate, but in fact, only in the process of execution can we know that it will be accessed next Which page is it. The operating system cannot predict the page access sequence in advance. Therefore, the best permutation algorithm cannot be achieved.

First-in-first-out replacement algorithm (FIFO)

First-in-first-out replacement algorithm (FIFO): the page to be eliminated each time is the first page
to enter the memory. Implementation method: the pages transferred into the memory are arranged in a queue according to the order of transfer, and the page at the head of the queue is selected when the page needs to be swapped out That's it. The maximum length of the queue depends on how many memory blocks the system allocates to the process.
Example: Suppose the system allocates four memory blocks for a process, and consider the following page number reference string: 3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4
Insert picture description here
Belady exception― An abnormal phenomenon in which the number of page faults does not decrease but increases when the number of physical blocks allocated to the process increases.
Only the FIFO algorithm will generate Belady exceptions. In addition, although the implementation of the FIFO algorithm is simple, the algorithm does not fit the law of the actual operation of the process, because the first entered page may also be accessed most often. Therefore, the algorithm performance is poor

The least recently used replacement algorithm (LRU)

The least recently used replacement algorithm (LRU, least recently used): The page that is eliminated each time is the page that has not been used the most recently. Implementation method: assign each page to the corresponding page table entry, use the access field to record the page since the last time it was used The elapsed time t since the visit. When a page needs to be eliminated, the one with the largest t value among the existing pages is selected, that is, the page that has not been used the most recently.
The implementation of this algorithm requires special hardware
examples: suppose the system allocates four memory blocks for a process, and consider that the following page numbers refer to
1, 8, 1, 7, 8, 2, 7, 2, 1, 8, 3, 8, 2, 1, 3, 1, 7, 1, 3, 7
Insert picture description here
The implementation of this algorithm requires special hardware support. Although the algorithm has good performance, it is difficult to implement and expensive

Clock replacement algorithm ((CLOCK)

The best permutation algorithm has the best performance, but it cannot be achieved; the first-in first-out permutation algorithm is simple to implement, but the algorithm performance is poor; the last unused permutation algorithm has good performance and is the closest to the performance of the OPT algorithm, but it requires special hardware support for implementation , The algorithm overhead is large.
The clock replacement algorithm is an algorithm with a balanced performance and overhead, also known as the CLOCK algorithm, or the recently unused algorithm (NRU, NotRecently Used)

Simple CLOCK algorithm implementation method :
set an access bit for each page, and then link the pages in the memory into a circular queue through the link pointer.
When a page is accessed, its access position is 1.
When a page needs to be eliminated, just check the access bit of the page.
If it is 0, select the page to be swapped out; if it is 1, then set it to 0, do not swap out temporarily, continue to check the next page, if all pages in the first round of scanning are 1, then change the value of these pages After the access position is set to 0 in turn, the second round of scanning is performed (the second round of scanning must have a page with the access position of o, so the simple CLOCK algorithm selects a eliminated page and it will go through at most two rounds of scanning)

The visit bit is 1, which means it has been visited recently; the visit bit is 0, which means it has not been visited recently
Insert picture description here

Guess you like

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