Skip to content

Chapter 9: Virtual Memory

:material-circle-edit-outline: 约 645 个字 :material-clock-time-two-outline: 预计阅读时间 2 分钟

操作系统(本)2024-11-28 第 7-8 节 ch9 Virtual Memory.pdf 操作系统(本)2024-12-03第7-8节

image-20241205141628780

Background

Virtual Memory: Only part of a running program needs to be loaded into memory for execution

Virtual memory separates user logical memory from physical memory

让各个进程都以为自己在独享内存

Allows physical address space to be shared by several processes

这个技术基于两个局限性

image-20241205120016300

Demand Paging(按需调页、请求调页)

Bring a page into memory only when it is needed

更完整的页表

在请求分页系统中的每个页表项为:

\[ <物理块号,状态位P,访问字段A,修改位M,外存地址> \]
  • 状态位 P(存在位):用于指示该页是否已调入内存,供程序访问时参考。
  •  访问字段 A:用于记录本页在一段时间内被访问的次数,或最近已有多长时间未被访问,提供给置换算法选择换出页时参考。
  •  修改位 R/W:表示该页在调入内存后是否被修改过。
  •  外存地址:用于指出该页在外存上的地址,供调入该页时使用。

Page Fault 缺页

缺页处理过程

  1. 检查进程的页表,以确定该引用是合法还是非法的地址访问。
  2. 如果引用非法,那么终止进程。如果引用有效但是尚未调入页面,那么现在应调入。
  3. 找到一个空闲帧(从空闲帧链表中取一个)
  4. 调度磁盘操作,以便将所需要的页调入刚分配的帧
  5. 当磁盘读操作完成后,修改进程的内部表和页表,以表示该页已在内存中。
  6. 重新开始因非法地址陷阱而中断的指令。进程现在能访问所需的页,就好像它似乎总在 内存中。

image-20241205121001614

A page fault causes the following sequence to occur:

  1. Trap to the OS.
  2. 。。。看 PPT21 页 ch9 Virtual Memory.pdf

页置换

没有空闲帧时需要进行页置换,我们希望找到一个算法导致最小的的页错误的发生

Demand Paging Performance

page fault rate p,p = 0 表示不会发生 page fault,p = 1 表示每个 page 都会 page fault

The effective memory-access time is:

(1 – p) x physical-memory-access + p x ( page-fault-overhead + swap-page-out + swap-page-in + restart-overhead )

[!EXAMPLE] Demand Paging Example

image-20241205121427248

Process Creation

Virtual memory allows other benefits during process creation

Copy-on-Write

Copy-on-Write (COW) allows both parent and child processes to initially share the same pages in memory

即,如果父子进程都只读一个 page,就共享这个 page

直到有一方要写某个 page,才将该 page 进行复制,让双方各自拥有一份这个 page

If either process modifies a shared page, only then is the page copied

COW allows more efficient process creation as only modified pages are copied

[!EXAMPLE]

Before Process 1 Modifies Page C

image-20241205121837477

After Process 1 Modifies Page C

image-20241205122005821