3.2 Virtual memory management

3.2.1 Basic concepts of virtual memory


1. Characteristics of traditional storage management methods

1)一次性: The job must be loaded into memory all at once before it starts running

2)驻留性:After the job is loaded into the memory, it always resides in the memory

2. The principle of locality: temporal locality and spatial locality

时间局部性:Once a certain instruction in the program is executed, the instruction may be executed again soon after
空间局部性: the program accesses a certain storage unit, and shortly after, nearby storage units are also accessed.

3. Definition and characteristics of virtual storage

1)多次性:There is no need to load all the jobs into the memory at once, and it is allowed to be transferred into the memory in stages.
2)对换性:There is no need to always reside in the memory when the job is running, allowing swapping in and swapping out while the job is running.
3)虚拟性:Expand the memory capacity logically.

4. Implementation of virtual memory technology

Virtual memory technology need to build on 离散分配the basis of the above memory management.

There are three ways to implement virtual memory technology:

  • Request paging storage management
  • Request Fragmentation Management
  • Request segment page storage management

3.2.2 Request paging management mode

Compared with basic paging management, in request paging management, in order to realize "request paging", the operating system needs to know 每个页面是否已经调入内存; if it has not been transferred, then it also needs to know the location of the page in the external memory.

1. Page table mechanism

The request paging system is built on the basis of the basic paging system, 支持虚拟存储器功能and the 请求调页功能sum is added for order 页面置换功能. Request paging is currently the most commonly used method to implement virtual memory.
Insert picture description here

![](https://img-blog.csdnimg.cn/20201021193226693.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODIyMDc5OQ==,size_16,color_FFFFFF,t_70#pic_center
2. Page fault interruption mechanism

In the request paging system, whenever the page to be accessed is not in the memory, a page fault interrupt is generated, requesting the system to transfer the missing page into the memory. At this time, the process of page faults should be blocked ( I/O请求),若此时内存中有空闲块,则将该页调入该块, 若没有空闲块,则要淘汰某页。(If the eliminated page has been modified during memory, it must be written back to external memory)

  • Interrupt signals are generated and processed during instruction execution, not after an instruction is executed 属于内中断.

Insert picture description here

3. Address conversion agency

The main differences between request paging storage management and basic paging storage management:

In the process of program execution, 当所访问的信息不在内存时,由操作系统负责将所需信息从外存调入内存 (the operating system needs to provide a request paging function to transfer missing pages from external storage to memory) , and then continue to execute the program.

If the memory space is not enough, the operating system is responsible 将内存中暂时用不到的信息换出到外存 (the operating system should provide the page replacement function, swapping out temporarily unused pages out of external storage).
Compared with the paging system, operations on the status bit P are mainly added.
Insert picture description here
Insert picture description here


3.2.3 Paging algorithm (deciding which page should be swapped in and which page should be swapped out)

1) Optimal (OPT) replacement algorithm

Each time you choose to eliminate the page will be 以后永不使用, or in 最长时间内不再被访问的页面, this can ensure the lowest page fault rate.
7在第18次访问才调入,故先替换7
Insert picture description here
缺页中断未必发生页面置换 Page replacement only occurs when the physical block is full (For example, the first three page faults occurred, but no page replacement occurred)

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

Each 选择淘汰的页面is最早进入内存的页面

Insert picture description here
Belady异常-------- When the number of physical blocks allocated for a process increases, the number of page faults does not decrease but increases instead.

只有FIFO算法会产生Belady异常

3) The least recently used (LRU) replacement algorithm

The pages eliminated each time are最近最久未使用的页面

4) Clock (CLOCK) replacement algorithm

Best replacement algorithm of the OPT can best 但无法实现;
FIFO replacement algorithm is simple 但算法性能差;
least recently use good replacement algorithm performance, 最接近OPT算法性能but to implement 需要专门的硬件支持,算法开销大.


Simple clock replacement algorithm

Insert picture description here


Improved clock replacement algorithm

priority:

  • Not visited recently
  • Not visited recently, but modified
  • Visited but not modified
  • Visited, modified
    Insert picture description here

Insert picture description here


一轮扫描的情况
Insert picture description here
Insert picture description here
Insert picture description here


二轮扫描的情况

第一轮结束后并没有找到(0,0)
Insert picture description here
第二轮,寻找第一个(0,1),并将扫描过的访问位置为0Insert picture description here
淘汰第一个(0,1)
Insert picture description here


三轮扫描的情况

第一轮结束后并没有找到(0,0)
Insert picture description here
第二轮,寻找第一个(0,1),并将扫描过的访问位置为0,并没有找到

Insert picture description here
第三轮,寻找第一个(0,0)页面,不修改任何信息
Insert picture description here


四轮扫描的情况

第一轮结束后并没有找到(0,0)
Insert picture description here
第二轮,寻找第一个(0,1),并将扫描过的访问位置为0,并没有找到
Insert picture description here
第三轮,寻找第一个(0,0)页面,不修改任何信息,并没有找到

Insert picture description here
第四轮,寻找第一个(0,1)页面
Insert picture description here


改进型的时钟置换算法 At mostFour scan


3.2.4 Page allocation strategy

1) Page allocation and replacement strategy

Insert picture description here
固定分配全局置换:
Allocate a certain number of physical blocks to each process. During the entire process 都不变, if a page fault occurs, you can onlySelect a page from the pages in the memory of the process to swap out.

可变分配局部置换: When a 最易于实现的物理块分配和置换策略
page is missing, the systemTake a physical block from the free physical block queue and assign it to the process可以动态增加进程的物理块。

可变分配局部置换:
When a page fault occurs in a process, == only allows one page to be swapped out from the page in the memory of the process. == So it will not affect other processes.若进程在运行中频繁缺页,系统再为进程分配若干物理块


2) Timing of loading the page

预调页策略:According to the principle of locality (mainly refers to spatial locality, that is: if a memory unit is currently accessed, it is likely to visit the adjacent memory units later.), 一次调入若干个相邻的页面可能比一次调入一个页面更高效。but if most of the pages are loaded in advance If they have not been visited, it is inefficient. Therefore, it is possible to predict the pages that may be accessed in the near future and transfer them into memory in advance, but it is currently predicted 成功率只有50% 左右. 故这种策略主要用于进程的首次调入(运行前调入), The programmer points out which parts should be transferred first.

请求调页策略:The process is running 发现缺页时才将所缺页面调入内存(运行时调入). Pages imported by this strategy will definitely be accessed, but since only one page can be imported at a time, and disk I/0 operations are required for each paging, the I/0 overhead is relatively high.


3) Where to load the page
Insert picture description here
对换区 is usually 采用连续分配方式, but the way is 文件区used 离散分配, so the disk I/O speed of the swap area is faster than that of the file area.
1. The system has enough space on the exchange area: transferred to the page, bring in all 内存与对换区之间conduct,

2. The system lacks enough space in the swap area:, 凡是不会被修改的数据都直接从文件区调入 because these pages will not be modified, so 换出时不必写回磁盘you can load it from the file area next time you need it. For 可能被修改的部分,换出时需写回磁盘对换区, transfer in from the swap area when needed next time.

3. UNIX mode: All data related to the process before running is placed in the file area. 故未使用过的页面,都可从文件区调入。 If the used page needs to be swapped out, it will be written back to the swap area and transferred from the swap area when needed next time.


3.2.5 Jitter

The page that has just been swapped out will be swapped into memory again, and the page that has just been swapped in will be swapped out of external storage immediately, This frequent paging behavior is called 抖动, or 颠簸. The main reason for jitter isThe number of pages frequently accessed by the process is higher than the number of available physical blocks (the physical blocks allocated to the process are not enough)


3.2.6 Working set

驻留集: Refers to the request in paging storage managementA collection of memory blocks (pages) allocated by the process.
工作集: Refers to in a certain time interval,The collection of pages actually accessed by the process.

工作集是指在某段时间间隔内,进程要访问的页面的集合。

Insert picture description here
驻留集大小不能小于工作集大小,否则进程运行过程中将频繁缺页。


Insert picture description here

例题
Insert picture description here
物理地址:Page frame number + page offset
逻辑地址:Logical number (page number) + page offset

A resident set size of 2 means that only two pages can exist in memoryPage numbers 0 and 2 in the page table are in the memory, and page number 1 is not. Therefore, the page replacement algorithm needs to be eliminated 页面. Note that the page number is eliminated, not the page frame number.

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_38220799/article/details/109205161
Recommended