chapter 10: virtual memory 虚拟 存储器

100
Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Chapter 10: Virtual Memory 虚虚虚虚虚 Background Demand Paging 虚虚虚虚 Process Creation ( 虚虚虚虚虚 ) Page Replacement 虚虚虚 Allocation of Frames 虚虚虚虚虚 Thrashing 虚虚 Operating System Examples

Upload: valiant

Post on 05-Jan-2016

146 views

Category:

Documents


0 download

DESCRIPTION

Chapter 10: Virtual Memory 虚拟 存储器. Background Demand Paging ( 请求页式) Process Creation ( 进程的创建 ) Page Replacement ( 页置换) Allocation of Frames ( 页框的分配) Thrashing ( 抖动) Operating System Examples. 局部性原理. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.1Operating System Concepts

Chapter 10: Virtual Memory虚拟存储器

Background Demand Paging (请求页式) Process Creation ( 进程的创建 )

Page Replacement (页置换) Allocation of Frames (页框的分配) Thrashing (抖动) Operating System Examples

Page 2: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.2Operating System Concepts

局部性原理 局部性原理 (principle of locality) :指程序

在执行过程中的一个较短时期,所执行的指令地址和指令的操作数地址,分别局限于一定区域。具体表现在两个方面:时间局部性:一条指令的一次执行和下次执

行,一个数据的一次访问和下次访问都集中在一个较短时期内;

空间局部性:当前指令和邻近的几条指令,当前访问的数据和邻近的数据都集中在一个较小区域内。

Page 3: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.3Operating System Concepts

局部性原理的具体体现程序在执行时,大部分是顺序执行的指令,

少部分是转移和过程调用指令。过程调用的嵌套深度一般不超过 5 ,因此执

行的范围不超过这组嵌套的过程。程序中存在相当多的循环结构,它们由少量

指令组成,而被多次执行。程序中存在相当多的数据结构的操作,如数

组操作,往往局限在较小范围内。

Page 4: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.4Operating System Concepts

虚拟存储器的原理

在程序装入时,不必将其全部读入到内存,而只需将当前需要执行的部分页或段读入到内存,就可让程序开始执行。

在程序执行过程中,如果需执行的指令或访问的数据尚未在内存(称为缺页或缺段),则由处理器通知操作系统将相应的页或段调入到内存,然后继续执行程序。

另一方面,操作系统将内存中暂时不使用的页或段调出保存在外存上,从而腾出空间存放将要装入的程序以及将要调入的页或段。这样,只需程序的一部分在内存就可执行。

1. 虚拟存储的基本原理

Page 5: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.5Operating System Concepts

2. 引入虚拟存储技术的好处

大程序:可在较小的可用内存中执行较大的用户程序;

大的用户空间:提供给用户可用的虚拟内存空间通常大于物理内存 (real memory)

并发:可在内存中容纳更多程序并发执行; 易于开发:与覆盖技术比较,不必影响编程

时的程序结构

Page 6: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.6Operating System Concepts

3. 虚拟存储技术的特征

不连续性:物理内存分配的不连续,虚拟地址空间使用的不连续(数据段和栈段之间的空闲空间,共享段和动态链接库占用的空间)

部分交换:与交换技术相比较,虚拟存储的调入和调出是对部分虚拟地址空间进行的;

大空间:通过物理内存和快速外存相结合,提供大范围的虚拟地址空间总容量不超过物理内存和外存交换区容量

之和

Page 7: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.7Operating System Concepts

10.1 Background

Virtual memory – separation of user logical memory from physical memory. (虚拟内存—物理内存和用户逻辑内存的区分)Only part of the program needs to be in memory for

execution. (只有部分运行的程序需要在内存中) Logical address space can therefore be much larger

than physical address space. (因此,逻辑地址空间能够比物理地址空间大)

Allows address spaces to be shared by several processes. (允许若干个进程共享地址空间)

Allows for more efficient process creation.(允许更多有效进程创建)

Page 8: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.8Operating System Concepts

Background Cont.

Virtual memory can be implemented via:(虚拟内存能够通过以下手段来执行)Demand paging (请求页式)Demand segmentation (请求段式)

Page 9: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.9Operating System Concepts

Virtual Memory That is Larger Than Physical Memory

Page 10: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.10Operating System Concepts

10.2 Demand Paging Bring a page into memory only when it is needed

(只有在一个页需要的时候才把它换入内存) . Less I/O needed (需要很少的 I/O ) Less memory needed (需要很少的内存) Faster response (快速响应) More users (多用户)

Page is needed reference to it

(需要页) (查阅此页) invalid reference abort

(无效的访问) (中止) not-in-memory bring to memory

(不在内存) (换入内存)

Page 11: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.11Operating System Concepts

Transfer of a Paged Memory to Contiguous Disk Space( a paging system with swapping)

Page 12: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.12Operating System Concepts

Valid-Invalid Bit With each page table entry a valid–invalid bit is

associated(1 in-memory, 0 not-in-memory)

在每一个页表的表项有一个有效 - 无效位相关联, 1 表示在内存, 0 表示不内存

Initially valid–invalid

but is set to 0 on all entries.在所有的表项,这个位被初始化为 0

valid-invalid bit11110

00

Frame #

page table

Page 13: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.13Operating System Concepts

Valid-Invalid Bit Cont.

Example of a page table snapshot.一个页表映象的例子

During address translation, if valid–invalid bit in page table entry is 0 page fault.

在地址转换中,如果页表表项位的值是 0 ( 缺页 )

Page 14: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.14Operating System Concepts

Page Table When Some Pages Are Not in Main Memory

Page 15: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.15Operating System Concepts

Page Fault( 缺页 )

Steps in handling:

1. If there is ever a reference to a page, first reference will trap to OS page fault trap

如果有对一个页的访问,第一个访问要陷入 OS 缺页2. OS looks at another table to decide:

OS 查看另一个表来决定 Invalid reference abort. 无效引用 终止 Just not in memory.仅仅不在内存

Page 16: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.16Operating System Concepts

Page Fault Cont.

3. Get empty frame (得到空的页框) .4. Swap page into frame (把页调入页框) .5. Reset tables, validation bit = 1 (重新设置页表,把位设为 1 ) .

6. Restart instruction (重启指令) : Least Recently Used (最近未使用)

Page 17: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.17Operating System Concepts

Steps in Handling a Page Fault

Page 18: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.18Operating System Concepts

Steps in Handling a Page Fault

1. Check an internal table for this process, to determine whether the reference was a valid or invalid memory access

2. If the reference was invalid, we terminate the process. If it was valid, but we have not yet brought in that page, we now page it in

3. We find a free frame4. We schedule a disk operation to read the desired page into

the newly allocated frame 5. When the disk read is complete, we modify the internal

table kept with the process and the page table to indicate that the page is now in memory

6. We restart the instruction that was interrupted by the illegal address trap. The process can now access the page as though it had always been in memory

Page 19: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.19Operating System Concepts

Hardware to support demanding paging

Page table Secondary memory

Page 20: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.20Operating System Concepts

Discuss about page fault interrupt( 缺页中断 )

由处理器的地址变换机构产生缺页中断,然后调用操作系统提供的中断处理例程。

缺页中断的特殊性 : 缺页中断在指令执行期间产生和进行处理,而不是在一条

指令执行完毕之后。所缺的页面调入之后,重新执行被中断的指令。

一条指令的执行可能产生多次缺页中断,如: swap A, B而指令本身和两个操作数 A, B 都跨越相邻外存页的分界处,则产生 6次缺页中断。

swap A,B

A

B

Page x

x+1

y

y+1

z

z+1

Page 21: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.21Operating System Concepts

Some Example ( 1 )

Example 1. ADD A, B => C1. Fetch and place the instruction (ADD)

2. Fetch A

3. Fetch B

4. Add A and B

5. Store the sum in C

在取指令、取操作数 A 和 B、保存结果到 C 的任何一个步骤中都有可能发生缺页中断

此外,指令、操作数等可能跨在相邻的两个页之间

Page 22: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.22Operating System Concepts

Some Example ( 2 )

Example 2. 块操作指令 ( 如 IBM 360/370 MVC 指令)可移动 256个字节可能源或目的地数据块分跨在不同页上可能源和目的地之间有重叠

Example 3. 自动增、自动减间接指令MOV (R2)+ , -(R3)

Page 23: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.23Operating System Concepts

What happens if there is no free frame?

Page replacement – find some page in memory, but not really in use, swap it out

页置换—找到内存中并没有使用的一些页,换出 .

需要考虑 :Algorithm (算法)Performance (性能) – want an algorithm which

will result in minimum number of page faults (找出一个导致最小缺页数的算法) .

Same page may be brought into memory several times

同一个页可能会被装入内存多次

Page 24: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.24Operating System Concepts

10.2.2 Performance of Demand Paging

Page Fault Rate (缺页率) 0 p 1.0 if p = 0 no page faults (如果 p = 0 ,没有缺页) if p = 1, every reference is a fault

(每次访问都缺页) Effective Access Time (EAT)

EAT = (1 – p) x ma + p x page fault time

= (1 – p) x memory access + p (page fault overhead + [swap page out ] + swap page in + restart overhead)

Page 25: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.25Operating System Concepts

Demand Paging Example Swap page time ( 交换页的时间 ) = 25 msec =

25,000,000 nsec a memory access time is 100nsec EAT = (1 – p) x 100 + p (25,000,000)

= 100 + 24,999,900 x p (in nsec) 可见 EAT 与缺页率( page-fault rate )成正比

If p=1/1000, EAT = 25us If p=1/10000, EAT = 2.5us If p=1/100000, EAT = 0.25us

假设memory access time is 100nsec ,为确保因缺页引起的性能下降低于 10% ,即: EAT<110ns, 则: 100 + 24,999,900 x p <110 p<0.0000004 也即必须确保 缺页率 <1/2500000

Page 26: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.26Operating System Concepts

10.3 Process Creation

Virtual memory allows other benefits during process creation:在进程创建过程中虚存存在其它的好处:- Copy-on-Write 写拷贝- Memory-Mapped Files 内存映射文件

Page 27: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.27Operating System Concepts

10.3.1 Copy-on-Write Fork()—creates a child process as a duplicate of its parent. Copy-on-Write (COW) allows both parent and child processes

to initially share the same pages in memory.

COW 允许父进程和子进程在内存中初始共享相同页 If either process modifies a shared page, only then is the page

copied.

如果任一进程(父或子)需要修改一个共享页,则仅仅是该页被拷贝,随后再修改该页。

For COW technique, Only the pages that are modified by either process are copied All non-modified pages be shared by the parent and child process

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

COW 允许进程的创建更加有效,只有在需要修改页时才会拷贝页面 Windows 2000, Linux and Solaris 2 都采用这种技术

Page 28: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.28Operating System Concepts

Copy-on-Write Free pages are allocated from a pool of free pages , called

zeroed-out pages. The technique is known as zero-filled-on-demand. (预先清零 )

空闲页从一个 zeroed-out 页池中被分配。 System call vfork() in Unix (for virtual memory fork()). With

vfork() ,the parent process is suspended and the child process uses the address space of parent

Page 29: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.29Operating System Concepts

10.3.2 Memory-Mapped Files Memory-mapped file I/O allows file I/O to be treated as routine

memory access by mapping a disk block to a page in memory.

通过映射磁盘块到内存中一页的方式,内存映射文件 I/O 允许文件的 I/O 操作被处理成常规的存储访问。

A file is initially read using demand paging. A page-sized portion of the file is read from the file system into a physical page. Subsequent reads/writes to/from the file are treated as ordinary memory accesses.

一个文件开始被读使用请求页。文件的一页大小从文件系统被读入一物理的页。随后文件读 / 写处理被当作普通存储器访问。

Simplifies file access by treating file I/O through memory rather than read()/write() system calls. 通过内存处理文件 I/O 可简化文件访问。这种处理方式要优于通常的 read()/write() 系统调用。

Also allows several processes to map the same file allowing the pages in memory to be shared.

允许若干个进程映射到相同的文件,以此达到内存中页面被共享的目的

Page 30: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.30Operating System Concepts

Memory Mapped Files

Page 31: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.31Operating System Concepts

10.4 Page Replacement

Prevent over-allocation of memory by modifying page-fault service routine to include page replacement (通过修改缺页服务例程,来包含页置换,防止分配过多的内存) .

Use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk (通过设置修改位以减少页面传送的次数—只有被修改的页面才写入磁盘) .

Page replacement completes separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory (页置换完善了逻辑内存和物理内存的划分—在一个较小的物理内存基础之上可以提供一个大的虚拟内存

Page 32: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.32Operating System Concepts

Need For Page Replacement

Page 33: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.33Operating System Concepts

Need For Page Replacement

在前面的例子中 , User1 的第 3 页和 User2的第 1 页尚未分配相应的物理内存。此时物理内存已经全部被占用。

此时, User1 中的 load M 指令需要使用第3 页上的数据,但是,该页并未在内存中,该如何处理?

OS 有三种处理方法 :中止一个用户进程交换出 (Swap out) 一个进程页面置换

Page 34: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.34Operating System Concepts

10.4.1 Basic Page Replacement

Steps:1. Find the location of the desired page on disk.

在磁盘上找到需要页的位置2. Find a free frame找到空闲的页框 :

- If there is a free frame, use it. (如果有一个空闲页框,使用它) - If there is no free frame, use a page replacement algorithm to select a victim frame.

如果没有空闲页框,使用页代替算法选择一个牺牲 (淘汰 )页框。

3. Read the desired page into the (newly) free frame. Update the page and frame tables.读入该页到 ( 最新 )空闲页框。更新页和页框表

4. Restart the process.

重启进程

Page 35: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.35Operating System Concepts

Page Replacement

Page 36: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.36Operating System Concepts

Page Replacement

Use modify bit (or dirty bit) to reduce overhead of page transfers – only modified pages are written to disk (通过设置修改位以减少页面传送的次数—只有被修改的页面才写入磁盘) . The bit is indicating that the page has been modified If the bit is set, the page must be written to disk If the bit is not set, the page has not been modified sine it

was read into memory, so we can avoid writing the page to disk.

In this way ,we can reduce the time required to service a page fault

Page 37: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.37Operating System Concepts

Page Replacement Algorithms

Two major problems: Frame-allocation algorithm

How many frames to allocate to each process?

Page-replacement algorithm Which frames are to be replaced?

There are many algorithms Goal:

Lowest page-fault rate

Page 38: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.38Operating System Concepts

Page Replacement Algorithms Want lowest page-fault rate. (追求最小的缺页率) Evaluate algorithm by running it on a particular

string of memory references (reference string) and computing the number of page faults on that string. (通过运行一个内存访问的特殊序列(访问序列),计算这个序列的缺页次数) We only consider page number, rather than the entire

address If we have a reference to a page p, then any

immediately following references to page p will never cause a page fault

In all our examples, the reference string is (在所有的例子中,访问序列是) :

(1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5) and( 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1 )

Page 39: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.39Operating System Concepts

Graph of Page Faults Versus The Number of Frames

Page 40: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.40Operating System Concepts

10.4.2 FIFO Page Replacement

缺页次数: 15 次

Page 41: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.41Operating System Concepts

10.4.2 FIFO Algorithm-- Belady’s Anomaly

在 FIFO算法中,有时侯页架数的增加反而会使缺页次数增加,如下例: number of frames is 3 or 4

Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 3 frames (3 pages can be in

memory at a time per process)

4 frames

FIFO Replacement – Belady’s Anomaly more frames less page faults

1

2

3

1

2

3

4

1

2

5

3

4

9 page faults

1

2

3

1

2

3

5

1

2

4

5 10 page faults

44 3

Page 42: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.42Operating System Concepts

FIFO Illustrating Belady’s Anamoly

Page 43: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.43Operating System Concepts

10.4.3 Optimal Replacement

Replace page that will not be used for longest period of time. (被置换的页将是今后最长时间不被使用的页)

缺页次数: 9次

Page 44: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.44Operating System Concepts

Optimal Algorithm

4 frames example

1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

How do you know this? 情况与 SJF CPU-scheduling算法相似

Used for measuring how well your algorithm performs.

(用来衡量你的算法的效率)

1

2

3

4

6 page faults

4 5

Page 45: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.45Operating System Concepts

10.4.4 LRU Page Replacement

缺页次数: 12 次

Least Recently Used (LRU)

Page 46: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.46Operating System Concepts

Least Recently Used (LRU) Algorithm

Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

实现方式:( 1 ) Counter implementation (计数器的实现)

Every page entry has a counter; every time page is referenced through this entry, copy the clock into the counter. (每一个页表项 有一个计数器,每次页通过这个表项被访问,把时间拷贝到计数器中)

When a page needs to be changed, look at the counters to determine which are to change. (当一个页需要替换时,查看计数器来决定改变哪一个页。)

1

2

3

5

4

4 3

5

Page 47: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.47Operating System Concepts

LRU Algorithm (Cont.)

( 2 ) Stack implementation – keep a stack of page numbers in a double link form :

栈实现—在一个双链表中保留一个记录页数目的栈 Page referenced (被访问的页) :

move it to the top when a page is referenced (移到栈顶)

requires 6 pointers to be changed

需要改变 6个指针 No search for replacement

没有为置换进行查找 The top of the stack is always the most recently used page The bottom is the LRU page

Page 48: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.48Operating System Concepts

Use Of A Stack to Record The Most Recent Page References

Page 49: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.49Operating System Concepts

置换算法举例( 1 )

某程序在内存中分配三个页面,初始为空,页面走向为 4, 3 , 2 , 1 , 4, 3 , 5 , 4, 3 , 2 , 1 , 5 。

OPT 4 3 2 1 4 3 5 4 3 2 1 5

页1 4 3 2 1 1 1 5 5 5 2 1 1

页2 4 3 3 3 3 3 3 3 5 5 5

页3 4 4 4 4 4 4 4 4 4 4

x x x x 3 3 x 3 3x x 3 共缺页中断7次

Page 50: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.50Operating System Concepts

置换算法举例( 2 )

LRU 4 3 2 1 4 3 5 4 3 2 1 5

页1 4 3 2 1 4 3 5 4 3 2 1 5

页2 4 3 2 1 4 3 5 4 3 2 1

页3 4 3 2 1 4 3 5 4 3 2

x x x x x x x 3 3x x x

共缺页中断10次

Page 51: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.51Operating System Concepts

置换算法举例( 3 )

FIFO 4 3 2 1 4 3 5 4 3 2 1 5

页1 4 3 2 1 4 3 5 5 5 2 1 1

页2 4 3 2 1 4 3 3 3 5 2 2

页3 4 3 2 1 4 4 4 3 5 5

x x x x x x x 3 3x x 3共缺页中断9次

Page 52: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.52Operating System Concepts

10.4.5 LRU Approximation Algorithms

Reference bit (访问位)With each page associate a bit, initially -= 0

每个页都与一个位相关联,初始值位 0When page is referenced bit set to 1

当页被访问位时,设位 1Replace the one which is 0 (if one exists). We do not

know the order, however.

如果存在的话,置换位为 0 的页。然而我们并不知道页的使用顺序,只知道那些页被使用过( reference bit=1 ),哪些被没有使用过( reference bit =0 )

根据这些信息,可以设计出许多与 LRU 相类似的算法 .

Page 53: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.53Operating System Concepts

10.4.5.1 Additional-Reference-Bits Algorithm

每一页的页表中都保持一个 8位字节,初始时为 0 设定一个定时器(例如,每 100ms/ 次)。每次定

时中断时将每页的 8位右移 1 位,去掉最右位 某页被访问时,最高位(最左位)置 1 需要置换页时,比较各页中 8位数的大小。 最小数的页(不一定唯一)被置换

Page 54: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.54Operating System Concepts

10.4.5.2 Second-Chance Algorithm二次机会算法

A FIFO replacement algorithm Need reference bit. (需要访问位) Clock replacement. (时钟置换) Algorithm :

reference bit = 1 after the page is first referenced If reference bit = 0 replace that page Else if a page to be replaced (in clock order) has reference

bit = 1, then (如果将要 <以顺时针 >交换的访问位是 1 ,则):1. set the reference bit of this page to 0 (把访问位设位 0

) .2. leave the page in memory (把该页保留在内存中) .3. replace next page [reference bit =0] (in clock order),

subject to same rules (以同样的规则,按顺时针方向去替换下一个页) .

Page 55: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.55Operating System Concepts

Second-Chance (clock) Page-Replacement Algorithm

Page 56: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.56Operating System Concepts

10.4.5.3 Enhanced Second-Chance Algorithm

Use an order pair: Reference bit and Modify bit 4 possible classes:

(0,0) neither recently used nor modified—best page to replace

(0,1) not recently used but modified—not quite as good. The page will be written out before replacement

(1,0) recently used but clean—it probably will be used again soon

(1,1) recently used and modified– it probably will be used again soon, and the page will be need to be written out to disk before it can be replaced

Replace the first page encountered in the lowest nonempty class

Compared clock algorithm, this algorithm give preference to those pages that have been modified to reduce the number of I/Os required

Page 57: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.57Operating System Concepts

10.4.6 Counting Algorithms

Keep a counter of the number of references that have been made to each page.用一个计数器记录对每一个页的访问次数。

Least frequently used (LFU) page-replacement Algorithm: replaces page with smallest count.以最小的计数置换一个页 缺点:一些页可能前期被密集访问,随后就不在被访问,但计数值很大 改进:计数器定期右移,使计数值呈指数衰减

Most frequently used (MFU) page-replacement Algorithm : based on the argument that the page with the smallest count was probably just brought in and has yet to be used.

处理方式刚好与 LFU 相反。以上面的观点为基础,最小计数的页也许刚刚被换入 ,并且尚未使用

Page 58: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.58Operating System Concepts

10.4.7 Page-Buffering Algorithm

Replaced page is added to one of two lists free page list if page has not been modifiedmodified page list if page has been modified

,这些页在被替换时可以暂时不写入磁盘,而是在磁盘空闲时,将一批页一起被写出去

Note: all replaced paged are still keeping in memory. 如果进程需要访问该页,可以迅速返回活动页中。

实际上,自由页表和修改页表充当着页的 Cache 的角色。

修改页表的另一种有用功能:修改的页可以成族地写回磁盘,大大减少 I/O 操作的数目。

Used in VAX/VMS system

Page 59: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.59Operating System Concepts

10.5 Allocation of Frames Each process needs minimum number of pages (每个进程所需

要的最少的页的数目) 有计算机的体系结构所决定的

Example: IBM 370 – 6 pages to handle SS MOVE instruction: instruction is 6 bytes, might span 2 pages. 2 pages to handle <from>. 2 pages to handle <to>.

最坏情况:许多次的间接内存访问指令,每一个间接地址分跨在两个页架上,造成一条指令需要很多页架 如果一个进程只给分配 2 个 page该怎么办?

Two major allocation schemes (两个主要的分配策略): fixed /equal allocation (固定 / 平均分配) priority /proportional allocation (优先 /按比例分配)

Page 60: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.60Operating System Concepts

Fixed Allocation ( 1 ) Equal allocation (平均分配) – e.g., if 100

frames and 5 processes, give each 20 pages. (例:如果有 100 个页框,和 5 个进程,则每个进程分给 20 个页)

( 2 ) Proportional allocation (按比例分配) – Allocate according to the size of process. (根据每个进程的大小来分配)

mSs

pa

m

sS

ps

iii

i

ii

for allocation

frames of number total

process of size

5964137127

56413710

127

10

64

2

1

2

a

a

s

s

m

i

Page 61: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.61Operating System Concepts

Priority Allocation

( 3 ) Use a proportional allocation scheme using priorities rather than size.根据优先级而不是大小来使用比率分配策略

If process Pi generates a page fault,

如果进程 Pi产生一个缺页 select for replacement one of its frames

选择替换该进程中的一个页框 select for replacement a frame from a process

with lower priority number

从一个较低优先级的进程中选择一个页面来替换

Page 62: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.62Operating System Concepts

10.5.3 Global vs. Local Allocation

Global replacement (全局替换) – process selects a replacement frame from the set of all frames; one process can take a frame from another.

进程在所有的页中选择一个替换页面;一个进程可以从另一个进程中获得页面优点:可获得较好的系统整体性能 问题:一个进程的缺页率变得不确定

Local replacement (局部替换)– each process selects from only its own set of allocated frames.

每个进程只从属于它自己的页中选择

Page 63: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.63Operating System Concepts

10.6 Thrashing( 抖动 ) If a process does not have “enough” pages, the

page-fault rate is very high. This leads to:

如果一个进程没有足够的页,那么缺页率将很高,这将导致 low CPU utilization ( CPU利用率低下) . operating system thinks that it needs to increase the

degree of multiprogramming.

操作系统反而会认为需要增加多道程序设计的道数以此提高CPU 的利用率

another process added to the system.

系统中将加入一个新的进程 Thrashing (抖动) a process is busy swapping

pages in and out (一个进程的页面经常换入换出) .

Page 64: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.64Operating System Concepts

Thrashing

Why does paging work? (为什么分页工作) Locality model (局部性 / 位置模式)

Process migrates from one locality to another.(进程从一个位置移到另一个位置) Localities may overlap. (位置可能重叠)

Why does thrashing occur? (为什么抖动会发生) size of locality > total memory size

Page 65: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.65Operating System Concepts

Locality In A Memory-Reference Pattern

Page 66: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.66Operating System Concepts

10.6.2 Working-Set Model working-set window a fixed number of page

references Example: sample 10,000 instructions

WSSi (working set of Process Pi) =total number of pages referenced in the most recent (varies in time) if too small will not encompass entire locality. if too large will encompass several localities. if = will encompass entire program.

D = WSSi total demand frames if D > m (available frames) Thrashing will occur Policy if D < m, then another process can be initiated. Policy if D > m, then suspend one of the processes.

Page 67: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.67Operating System Concepts

Working-set model

Page 68: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.68Operating System Concepts

10.6.2 Keeping Track of the Working Set

Difficulty: how to keep track of the working set. Approximate with interval timer + a reference bit

近似的一个内部时钟和一个访问位 Example: = 10,000

Timer interrupts after every 5000 time units.每 5000 个时钟单位产生一个时钟中断

Keep 2 in-memory bits for each page.为每个页保留 2 位 (in-memory bits)

Whenever a timer interrupts copy and sets the values of all reference bits to 0.任何时候一个时钟中断拷贝,把所有访问位设为 0

If one of the in-memory bits = 1 page in working set.如果一个在内存中的位是 1 ,说明页在工作集

Why is this not completely accurate? (为什么不是非常的精确) Improvement = 10 bits and interrupt every 1000 time units.

提高:用 10 个位,以及每 1000 个时钟单位中断。 但是,系统代价将大增!

Page 69: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.69Operating System Concepts

10.6.3 Page-Fault Frequency Scheme

Establish “acceptable” page-fault rate (设置可接受的缺页率) . If actual rate too low, process loses frame (如果缺页率太低,回收一些进程的页框) . If actual rate too high, process gains frame (如果缺页率太高,就分给进程一些页框) . 如果缺页率很高,但又没有空的页架可分配,则挂起某些进程,将其空闲的页架分配出去。

Page 70: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.70Operating System Concepts

10.8 Other Considerations

Major decisions in a paging system: Replacement algorithm and Allocation policy.

And also there are many other considerations (following):

10.8.1 Prepaging (预先调页) An attempt to prevent high level of initial paging when a

process is started. If suspending a process, we remember the working set

for that process When the process is resumed, we automatically bring

back into memory its entire working set before restarting the process

Page 71: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.71Operating System Concepts

调入策略 (fetch policy)

调入策略确定在外存中的页面调入时机。在虚拟页式管理中有两种常用策略。

请求调页 (demand paging) :只调入发生缺页时所需的页面。 优点:容易实现。 缺点:对外存 I/O 次数多,开销较大

预调页 (prepaging) :在发生缺页需要调入某页时,一次调入该页以及相邻的几个页。 优点:提高调页的 I/O 效率。 缺点:基于预测,若调入的页在以后很少被访问,则效率低。常用于程序装入时的调页。

Page 72: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.72Operating System Concepts

调入页面的来源

通常对外存交换区的 I/O 效率比文件区的高。关于调入页面的来源,这里有两种做法:

进程装入时,将其全部页面复制到交换区,以后总是从交换区调入。执行时调入速度快,要求交换区空间较大。

凡是未被修改的页面,都直接从文件区读入,而被置换时不需调出;已被修改的页面,被置换时需调出到交换区,以后从交换区调入。节省交换区空间。可能引发问题。如:装入可执行文件 a从而创建

进程 P ,如果在 P执行时,改写了可执行文件a (如重新编译和链接),而此后 P发生缺页需要从 a中调入页面,则可能会因为各个页面内容无法配合而出错 ( 如 Bus Error 或 Segmentation Fault)

Page 73: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.73Operating System Concepts

清除策略 (cleaning policy)

在虚拟页式管理中,何时将已修改页面调出到外存上。有两种常用清除策略:

请求清除 (demand cleaning) :该页被置换时才调出,把清除推迟到最后一刻。缺点:调入所缺页面之前还要调出已修改页面,

缺页进程的等待时间较长, 预清除 (precleaning) :该页被置换之前就调出,

因而可以成批调出多个页面。缺点:可能形成不必要的开销。已修改页面被调

出之后仍停留在内存,如果这些页面被置换之前就被再次修改,则这些页面可以返还到进程的常驻集,而之前所做的调出操作就成为不必要的开销。这种策略发展成为页面缓冲算法 (page buffering) 。

Page 74: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.74Operating System Concepts

10.8.2 Page size (页面尺寸选择) Fragmentation (碎片)

To minimize internal fragmentation , we need a small page size

Page table size (表大小) I/O overhead ( I/O 开销)

To minimize I/O time, a large page size is needed. A small page size allows each a page to match program

locality more accurately. A small page size should result in less I/O and less

total allocated memory. To minimize the number of page faults, we need

to have a large page size The historical trend is toward larger page sizes

198x: 4096 is upper bound 199x: 4096 is the most common page size Now: much larger size

Page 75: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.75Operating System Concepts

10.8.3 TLB Reach (TLB 范围 ) TLB Reach (TLB 范围 )- The amount of memory accessible

from the TLB. ( 从 TLB 可访问到的存储器的数量 ) 与 chapter 9 中的 hit ratio 相似

TLB Reach = (TLB Size) X (Page Size) 可见:增加 TLB Size 或 Page Size ==》增加 TLB Reach 但是,增加 TLB Size ==》增加硬件成本 增加 Page Size ==》增加内部碎片 理想的方法:

Increase the Page Size Provide Multiple Page Sizes

Ideally, the working set of each process is stored in the TLB. Otherwise there is a high degree of page faults.理想地 , 每进程的工作集存储在 TLB 中。否则有很高的缺页率。

Note:TLB – Translation Look-aside Buffer 联想存储器

Page 76: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.76Operating System Concepts

Increasing the TLB Reach

Increase the Page Size. This may lead to an increase in fragmentation as not all applications require a large page size.

增加页大小。但是 , 因为不是所有的应用程序都要求一个较大的页面尺寸,这可能导致碎片的增加。

Provide Multiple Page Sizes. This allows applications that require larger page sizes the opportunity to use them without an increase in fragmentation.

提供多种的页大小。允许要求很大页大小的应用程序有机会使用他们 , 且同时没有增加碎片。不同应用可以使用不同页大小。但是,这需要增加相应的支持 to manage the TLB, 可能会导致系统性能下降

Page 77: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.77Operating System Concepts

10.8.4 Inverted Page Table

与〈 9.4.4.3 Inverted Page Table〉情况相似 Create a table that has only one entry per physical

memory page, indexed by the pair <process-id, page-number>

The information about which virtual-memory page is stored in each physical frame

Reduce the amount of physical memory needed to store table information

Page 78: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.78Operating System Concepts

10.8.5 Program structure (程序结构)

Example: a java program to init to 0 each element of a 1024 by 1024 array: int A[ ][ ] = new int[1024][1024];Each row is stored in one page Program 1 for (j = 0; j < A.length; j++)

for (i = 0; i < A.length; i++)A[i,j] = 0;

1024 x 1024 page faults

Program 2 for (i = 0; i < A.length; i++)for (j = 0; j < A.length; j++)

A[i,j] = 0; 1024 page faults

Page 79: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.79Operating System Concepts

Factors to affect locality and page-fault rate

Data structures and programming structures

Compiler and loader Programming language

Java programs have better locality of reference than C or C++ programs on a virtual-memory system

Page 80: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.80Operating System Concepts

10.8.6 I/O Interlock ( I/O互锁)

在 I/O 时 ,可能有这种情况发生 : I/O 中断程序把需要传送的数据刚刚放到 Memory 中 , 在相应的进

程被唤醒之前 , 该页面已经被其它页面置换出去 I/O Interlock ( I/O互锁) – Pages must sometimes be

locked into memory.

有时页必须被锁进内存 Consider I/O. Pages that are used for copying a file

from a device must be locked from being selected for eviction by a page replacement algorithm.

考虑 I/O. 在通过某个页置换算法选择淘汰的页的时候,用于从一台设备拷贝文件的页必须被锁定。

Page 81: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.81Operating System Concepts

Reason Why Frames Used For I/O Must Be In Memory

Page 82: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.82Operating System Concepts

10.8.7 Real-Time Processing

Real-time systems almost never have virtual memory

Solaris 2 allow both time-sharing and real-time computing within a system. To solve the fault problem: To allow a process to tell which pages are

important to that process. To allow privileged users to require pages to

be locked into memory

Page 83: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.83Operating System Concepts

10.7 Operating System Examples

Windows NT

Solaris 2

Linux

Page 84: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.84Operating System Concepts

10.7.1 Windows NT

Uses demand paging with clustering. Clustering brings in pages surrounding the faulting page.

用集群使用请求页。集群包含缺页周边的页 Processes are assigned working set minimum and

working set maximum.

当进程被创建时 , 被分配最小工作集合和最大工作集合。 Working set minimum is the minimum number of

pages the process is guaranteed to have in memory.

最小工作集合是保证进程在内存中页的最小数目。 当系统有足够的空闲内存时 ,进程可被分配许多页 , 达到最

大工作集 (Working-set maximum). 甚至超过最大工作集 .

Page 85: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.85Operating System Concepts

WINDOWS NT

A process may be assigned as many pages up to its working set maximum.

进程可以被分配许多页,直到它的最大值工作集合 When the amount of free memory in the system

falls below a threshold, automatic working set trimming is performed to restore the amount of free memory.当系统的空闲内存数量在阀值下面时,自动工作集清理完成恢复一定数量空闲内存。

Automatic working-set trimming removes pages from processes that have pages in excess of their working set minimum. 自动工作集清理把超过最小工作集进程的页移开(减少页数目)。

Page 86: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.86Operating System Concepts

WINDOWS NT

Page replacement algorithms:On single-processor x86 systems, a

variation of the clock algorithm is used.On Alpha and multiprocessor x86 systems, a

variation of the FIFO algorithm is used.

Page 87: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.87Operating System Concepts

10.7.2 Solaris 2 Kernel maintains a list of free pages to assign faulting

processes.核心进程维护空闲页表分配给缺页进程

Lotsfree – threshold parameter to begin paging. Lotsfree- 开始 paging 的阀值

Paging is performed by pageout process. paging 由 pageout 进程实现

Pageout scans pages using modified clock algorithm. Pageout 使用修改过的 CLOCK算法扫描页

Scanrate is the rate at which pages are scanned. This ranged from slowscan to fastscan.Scanrate 是页被扫描的频率。这个范围是慢速扫描到快速扫描

Pageout is called more frequently depending upon the amount of free memory available.Pageout 被调用频繁程度取决于可用空闲内存数目

Page 88: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.88Operating System Concepts

Solar Page Scanner

Page 89: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.89Operating System Concepts

10.7.3 Linux

Physical memory allocation: Buddy system

Paging is peformed by kswapd process.

Kswapd scans pages using modified clock algorithm.

Page 90: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.90Operating System Concepts

Example of Buddy System

Page 91: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.91Operating System Concepts

10.9 Summary

Demand Paging (请求页式) Process Creation Page Replacement (页置换) Allocation of Frames (页框的分配) Thrashing (抖动) Operating System Examples

Page 92: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.92Operating System Concepts

2005年研究生入学考试( 1 )试题 3 ( 10 分): Demand paging 算法是 paging算法在虚拟存储

空间管理的扩展。其主要的改进是:仅当需要访问某页面时,如果它不在内存,才把它调入内存。按照这个思路,将 segmentation 算法(段式管理存储算法)扩展到虚拟存储空间管理,也可以产生类似的算法,不妨称之为 demand segmentation 。

( 1 )请给出相应算法,并简要说明。 ( 2 ) Demand paging 一般都用 TLB 。请问 demand

segmentation 算法需要类似的装置吗?为什么?

Answer: ( 1 )主要思想:仅当需要访问某段面时,如果它不在内存,才把它调入内存。当内存空间不足时,应该有段置换算法。

( 2 )从地址翻译流程可见,它需要两次访问内存,造成效率减半。 Cache 的访问时间接近于寄存器,远快于内存。如果命中,那么每次地址翻译只需要一次访问内存。

Page 93: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.93Operating System Concepts

2003年研究生入学考试( 1 )

2. 采用 不会产生内部碎片( internal fragmentation )。A 、分页式存储管理 B、分段式存储管理C 、固定分区式存储管理 D、段页式存储管理

11. 拿内存加上外存容量之和与虚拟存储空间相比,其大小关系是 _____ 。

A. 前者比后者大 B. 前者比后者小 C. 二者相等 D. 不一定

Answer: 2:B, 11:D

Page 94: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.94Operating System Concepts

2003年研究生入学考试( 2 )试题 3 ( 9分):“按需调页”式虚拟存储管理

( demand paging )需要交换空间( swapping space)的支持,如专用的磁盘分区。

( 1 )已知硬盘上有一交换区( swapping area ),试为它设计交换区的内部组织结构,说明如何访问其中的页面内容。

( 2 )物理内存的页面交换往往采用 LRU算法。在交换区里, LRU算法还有多大价值?试分析之。

( 3 )如果以磁盘作为交换区,磁盘与内存在存取速度上的差异必然影响操作系统的性能。请提出一种改善性能的方案,并简要说明。

Answer:1. 任何一种组织结构。如 Linux 所采用的。得 3 分。2. LRU算法在此不起作用。得 3 分。3. 任何一种改善方法。如 cache 。得 3 分。

Page 95: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.95Operating System Concepts

2002年研究生入学考试( 1 )

试题4( 12 分):按需调页( demand paging)的虚拟存储管理系统是如何工作的?分页机制和交换机制各起什么作用?请解释在一个存储管理系统中是否需要同时提供这两种机制。

Answer:1. demand paging 的工作原理和地址翻译流程 6分2. 分页起什么作用 2 分3. 交换起什么作用 2 分4. 存储管理系统中不一定同时提供这两种机制 2 分

Page 96: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.96Operating System Concepts

2001年研究生入学考试( 1 )

试题 1 ( 6分):虚拟存储管理利用了 swap area (交换区)、内存以及 cache (高速缓存)。假设:从 cache读取一个字节长的数据需 A 纳秒;如果该数据不在cache ,却在内存,则从内存读至 cache 需 B纳秒,然后还需从 cache得到;如果该数据既不在 cache ,又不在内存,则从 swap area 读入内存需 C 纳秒,然后还需传至 cache ,才能读取。已知 cache 的命中率( hit ratio )是 (n-1)/n ,内存的命中率是 (m-1)/m 。求平均访问时间。

Answer:(n-1)/n*A+1/n*((m-1)/m*(A+B)+1/m*(A+B+C))= A+1/n * B + 1/n * 1/m * C

Page 97: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.97Operating System Concepts

2001年研究生入学考试( 2 )

试题 2 ( 6分):用伙伴系统( buddy system )分配 1M长度的内存块。试根据如下所示的申请 /释放序列,画出该内存块的使用状况图:

申请 70K (分配结果记作 A ),申请 35K (分配结果记作 B),申请80K (分配结果记作 C ),释放 A 块,申请 60K (分配结果记作 D),释放 B块,释放 D块,释放 C 块。

提示:分配 A 块前后的内存使用状况如下两图

1 M

A=128K 128K 256K 512K

Page 98: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.98Operating System Concepts

2001年研究生入学考试( 3 )Answer:

Page 99: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.99Operating System Concepts

2000年研究生入学考试( 1 )试题 5 ( 6分):一进程已分配到 4个页帧( page frame ),如

下表(所有数字都为 10 进制数,且以 0 开始)。

当进程访问第 4页时,产生缺页中断。请分别用 FIFO (先进先出)、 LRU (最近最少使用)、 NRU (最近不用)算法,决定缺页中断服务程序选择换出的页面。

虚拟页号 页帧 装入时间 最近访问时间 访问位 修改位

2 0 60 161 0 1

1 1 130 160 0 0

0 2 26 162 1 0

3 3 20 163 1 1

Answer: 页号 /帧号FIFO : 0 页 /2帧LRU : 2 页 /0帧NRU : 2 页 /0帧

Page 100: Chapter 10:  Virtual Memory 虚拟 存储器

Silberschatz, Galvin and Gagne 200210.100Operating System Concepts

Exercises

3, 4, 5, 6, 9, 10, 16