夜雨聆风学习资料网 资料列表 开通 VIP 用登录 / 注册 ⌄
PDF

操作系统_第3章_内存管理_2026考研专业课_202026计算机_202026计算机_2026年优路计算机408_02.基础阶段_01.考研电子信息基础精讲课程_01.操作系统基础精讲课程

查看资料信息、公开预览和下载状态。

共 209 页PDF · 3.4MB
下载

文档在线预览

公开展示前 10 页
100%
1
《操作系统_第3章_内存管理_2026考研专业课_202026计算机_202026计算机_2026年优路计算机408_02.基础阶段_01.考研电子信息基础精讲课程_01.操作系统基础精讲课程》第1页预览
2
《操作系统_第3章_内存管理_2026考研专业课_202026计算机_202026计算机_2026年优路计算机408_02.基础阶段_01.考研电子信息基础精讲课程_01.操作系统基础精讲课程》第2页预览
3
《操作系统_第3章_内存管理_2026考研专业课_202026计算机_202026计算机_2026年优路计算机408_02.基础阶段_01.考研电子信息基础精讲课程_01.操作系统基础精讲课程》第3页预览
4
《操作系统_第3章_内存管理_2026考研专业课_202026计算机_202026计算机_2026年优路计算机408_02.基础阶段_01.考研电子信息基础精讲课程_01.操作系统基础精讲课程》第4页预览
5
《操作系统_第3章_内存管理_2026考研专业课_202026计算机_202026计算机_2026年优路计算机408_02.基础阶段_01.考研电子信息基础精讲课程_01.操作系统基础精讲课程》第5页预览
6
《操作系统_第3章_内存管理_2026考研专业课_202026计算机_202026计算机_2026年优路计算机408_02.基础阶段_01.考研电子信息基础精讲课程_01.操作系统基础精讲课程》第6页预览
7
《操作系统_第3章_内存管理_2026考研专业课_202026计算机_202026计算机_2026年优路计算机408_02.基础阶段_01.考研电子信息基础精讲课程_01.操作系统基础精讲课程》第7页预览
8
《操作系统_第3章_内存管理_2026考研专业课_202026计算机_202026计算机_2026年优路计算机408_02.基础阶段_01.考研电子信息基础精讲课程_01.操作系统基础精讲课程》第8页预览
9
《操作系统_第3章_内存管理_2026考研专业课_202026计算机_202026计算机_2026年优路计算机408_02.基础阶段_01.考研电子信息基础精讲课程_01.操作系统基础精讲课程》第9页预览
10
《操作系统_第3章_内存管理_2026考研专业课_202026计算机_202026计算机_2026年优路计算机408_02.基础阶段_01.考研电子信息基础精讲课程_01.操作系统基础精讲课程》第10页预览

本文档共 209 页 剩余 199 页未展示,请下载后查阅

下载

文档文本内容

操作系统
第三章 内存管理死锁
及解决方法内容
l 产生死锁与饥饿的原因
l 解决死锁的方法
l 死锁/同步的经典问题:哲学家进餐问题D e a d l o c k
l Permanent blocking of a set of processes that either
compete for system resources or communicate with each
other.
l No efficient solution.
l Involve conflicting needs for resources by two or more
processes.E g . P r o c e s s P a n d Q c o m p e t e t w o
r e s o u r c e s , T h e i r g e n e r a l f o r m s a r e :
Process P Process Q
… …
Get A Get B
… …
Get B Get A
… …
Release A Release B
… …
Release B Release A
… …死锁与进程的推进顺序有关。若修改P的代码,则不会产
生死锁Reusable Resources (可重用资源)
lUsed by one process at a time and not depleted (耗尽)by that
use.
lProcesses obtain resources that they later release for reuse
by other processes.
lProcessors, I/O channels, main and secondary memory, files,
databases, and semaphores.
lDeadlock occurs if each process holds one resource and
requests the other.E x a m p l e o f D e a d l o c kA n o t h e r E x a m p l e o f D e a d l o c k
l Space is available for allocation of 200K bytes, and the following sequence of
events occur.
P1 P2
. . .
. . .
Request 80K bytes;
Request 70K bytes;
. . .
l Deadlock occurs if both processes progress to thei.r .s e.cond request.
Request 60K bytes;
Request 80K bytes;Consumable Resources (可消耗资源)
l Created (produced) and destroyed (consumed) by a process.
l Interrupts, signals, messages, and information in I/O
buffers.E x a m p l e o f D e a d l o c k
l Deadlock occurs if receive is blocking
P1
P2
. . .
. . .
Receive(P2)
Receive(P1);
. . .
; . . .
Send(P2, M1);
Send(P1, M2);
l 此类死锁是由于设计失误造成的,很难发现,且潜伏期较长C o n d i t i o n s f o r D e a d l o c k
l Mutual exclusion(互斥)
● only one process may use a resource at a time.
l Hold-and-wait(保持并等待)
● A process may hold allocated resources while awaiting
assignment of other resources.C o n d i t i o n s f o r D e a d l o c k
l No preemption(不剥夺)
§ No resource can be forcibly removed from a process
holding it.
l Circular wait(环路等待)
§ A closed chain of processes exists, such that each
process holds at least one resource needed by the next
process in the chain.C o n d i t i o n s f o r D e a d l o c k
l 条件Mutual exclusion、 Hold-and-wait 、 No preemption是死
锁产生的必要条件,而非充分条件。
l 条件Circular wait是前3个条件产生的结果。D e a d l o c k P r e v e n t i o n ( 预 防 死 锁 )
间接方法,禁止前3个条件之一的发生:
1. 互斥 : 是某些系统资源固有的属性,不能禁止
2. 禁止“保持并等待”条件:要求进程一次性地申请其所需的全部资源。若系统
中没有足够的资源可分配给它,则进程阻塞。
3. 禁止“不剥夺”条件:①若一个进程占用了某些系统资源,又申请新的资源,
则不能立即分配给它。必须让它首先释放出已占用资源,然后再重新申请;②
若一个进程申请的资源被另一个进程占有,OS可以剥夺低优先权进程的资源分
配给高优先权的进程(要求此类可剥夺资源的状态易于保存和恢复,否则不能
剥夺)D e a d l o c k P r e v e n t i o n ( 预 防 死 锁 )
l 直接方法,禁止条件4(环路等待)的发生
即禁止“环路等待”条件:可以将系统的所有资源按
类型不同进行线性排队,并赋予不同的序号。进程对某类
资源的申请只能按照序号递增的方式进行。
显然,此方法是低效的,它将影响进程执行的
速度,甚至阻碍资源的正常分配。D e a d l o c k A v o i d a n c e ( 避 免 死 锁 )
l 预防死锁通过实施较强的限制条件实现,降低了系统性能。
l 避免死锁的关键在于为进程分配资源之前,首先通过计算,
判断此次分配是否会导致死锁,只有不会导致死锁的分配
才可实行。
l A d e c i s i o n i s m a d e d y n a m i c a l l y w h e t h e r t h e
current resource allocation request will, if
granted, potentially lead to a deadlock.
l Requires knowledge of future process request.A p p r o a c h e s t o D e a d l o c k A v o i d a n c e
l Do not start a process if its demands might lead to
deadlock.
l Do not grant an incremental resource request to a
process if this allocation might lead to deadlock.R e s o u r c e A l l o c a t i o n D e n i a l
l Referred to as the banker’s algorithm.
l State of the system is the current allocation of
resources to process.
l Safe state is where there is at least one sequence that
does not result in deadlock.
l Unsafe state is a state that is not safe.S a f e S t a t e
l 指系统能按某种顺序如<P1,P2,…,Pn>(称<P1,P2,…,Pn>为安全序列),来为每个进程分
配其所需资源,直至最大需求,使每个进程都可顺序完成,则称系统处于safe state.
l 若系统不存在这样一个安全序列,则称系统处于unsafe state.D e t e r m i n a t i o n o f a S a f e S t a t e
Initial StateD e t e r m i n a t i o n o f a S a f e S t a t e
P2 Runs to CompletionD e t e r m i n a t i o n o f a S a f e S t a t e
P1 Runs to CompletionD e t e r m i n a t i o n o f a S a f e S t a t e
P3 Runs to CompletionD e t e r m i n a t i o n o f a n Unsafe State
Initial StateD e t e r m i n a t i o n o f a n Unsafe StateS a f e S t a t e vs. U n s a f e S t a t e
l 并非所有不安全状态都是死锁状态
l 当系统进入不安全状态后,便可能进入死锁状态
l 只要系统处于安全状态,则可避免进入死锁状态。S a f e S t a t e to U n s a f e S t a t e
例,假设系统中有3个进程P1、P2、P3,共有12台磁带机。进程P1共需要10台,P2、P3分别需要
4台和9台。设T0时刻,进程P1、P2、P3已分别获得5台、2台和2台,尚有3台未分配,即图示:
进程 最大需求 已分配 可用
P1 10 5 3
P2 4 2
P3 9 2S a f e S t a t e to U n s a f e S t a t e
l T0时刻系统是安全的,因为存在一个安全序列<P2,
P1,P3>,即只要系统按此进程序列分配资源,每
个进程都可顺利完成。
进程 最大需求 已分配 可用
P1 10 5 3
P2 4 2
P3 9 2
l 但是,如果不按照安全序列分配资源,则系统可
能会由安全状态进入不安全状态S a f e S t a t e to U n s a f e S t a t e
例如,T0时刻以后,P3又申请1台磁带机。若系统将剩余3台中的1台分配给P3,则系统进入不安
全状态。
进程 最大需求 已分配 可用
P1 10 5 2
P2 4 2
P3 9 3l 该算法可用于银行发放一笔贷款前,预测该笔贷款是否会引起银行资金周转问
题。
l 这里,银行的资金就类似于计算机系统的资源,贷款业务类似于计算机的资源
分配。银行家算法能预测一笔贷款业务对银行是否是安全的,该算法也能预测
一次资源分配对计算机系统是否是安全的。
l 为实现银行家算法,系统中必须设置若干数据结构。数据结构
1. 可利用资源向量Available:是一个具有m个元素的数组,其中的每一个
元素代表一类可利用资源的数目,其初始值为系统中该类资源的最大可
用数目。其值将随着该类资源的分配与回收而动态改变。Available[j]
= k,表示系统中现有R 类资源k个。
j
2. 最大需求矩阵Max:是一个n * m的矩阵,定义了系统中n个进程中的每一
个进程对m类资源的最大需求。Max(i,j) = k,表示进程i对R 类资源的
j
最大需求数目为k个。数据结构
l分配矩阵Allocation:是一个n * m的矩阵,定义了系统中每一类资源的数
量。例如,Allocation(i,j) = k,表示进程i当前已分得R 类资源的数目
j
为k个。
l需求矩阵Need:是一个n * m的矩阵,用以表示每一个进程尚需的各类资源
数。例如,Need[i,j] = k,表示进程i还需要R 类资源k个,方能完成其任
j
务。数据结构
上述三类矩阵存在下述关系:
Need [i,j] = Max[i,j] - Allocation [i,j]设Request 是进程P 的请求向量。Request [j] = k,
i i i
表示进程P 需要k个R 类资源。当进程P 发出资源请
i j i
求后,系统按下述步骤进行检查:
1. 如果,Request ≤ Need 则转向步骤2;否则,出错
i i,
2. 如果,Request ≤ Available,则转向步骤3;否则,
i
表示尚无足够资源可供分配,进程P 必须阻塞等待。
i3. 系统试探性地将P 申请的资源分配给它,并修改下列
i
数据结构中的值:
Available := Available - Request ;
i
Allocation := Allocation + Request ;
i
Need := Need - Request ;
i i i
4 系统利用安全性算法,检查此次资源分配以后,系统
是否处于安全状态。若安全,才正式将资源分配给进程P
i
,完成本次资源分配;否则,试探分配失效,让进程P 阻
i
塞等待。安全性算法
(1)设置两个工作向量:
 设置一个数组Finish[n]。当Finish[i]=True(0 ≤
i ≤ n,n为系统中的进程数)时,表示进程P 可以获
i
得其所需的全部资源,而顺利执行完成。
‚ 设置一个临时向量Work,表示系统可提供给进程继
续 运 行 的 资 源 的 集 合 。 安 全 性 算 法 刚 开 始 执 行 时 ,
Work := Available安全性算法
(2)从进程集合中找到一个满足下列条件的进程:
Finish[i] = false; 并且 Need ≤ Work;
若找到满足该条件的进程,则执行步骤(3),否则执行步骤(4);
(3) 当进程P 获得资源后,将顺利执行直至完成,并释放其所拥有的全部
i
资源,故应执行以下操作:
Work := Work + Allocation ; 以及
i
Finish[i] := True;转向步骤(2);安全性算法
(4) 如果所有进程的Finish[i] = True,则表
示系统处于安全状态, 否则系统处于不安全状
态。举例
T 时刻的资源分配情况
0
假定系统中有四个进程P1, P2, P3, P4和三种类型的资源R1, R2, R3,每一种资源的数量分别
为9、3、6,T 时刻的资源分配情况如下表所示:
0资 Max Allocation Need Available
源
进
程
R1 R2 R3 R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 3 2 2 1 0 0 2 2 2 0 1 1
P2 6 1 3 6 1 2 0 0 1
P3 3 1 4 2 1 1 1 0 3
P4 4 2 2 0 0 2 4 2 0T 时刻的安全性
0
从T 时刻的安全性分析可知,T 时刻存在着一个安全序列<P2,P1,P4,
0 0
P3>,故,T 时刻系统是安全的。
0l 假设T 时刻,进程P1申请资源,其请求向量为Request (0,0,1),系统按银行家算法进行检查:
0 1
Request (0,0,1) ≤ Need (2,2,2),且
1 1
Request (0,0,1) ≤ Available (0,1,1)
1
l 故,系统试探性地为P1分配资源,并修改Available,Allocation 和Need 向量。
1 1Allocation Need Available
进程
R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 1 0 1 2 2 1 0 1 0
P2 6 1 2 0 0 1
P3 2 1 1 1 0 3
P4 0 0 2 4 2 0
利用安全性算法检查此时系统是否安全:
此时,系统的可用资源向量为Available(0,1,0),比较各进程
•
的需求向量Need,系统不能满足任何进程的资源请求,系统进入不
安全状态。
所以,P1请求的资源不能分配,只能让进程P1阻塞等待。
•l假 设 T 时 刻 , 进 程 P 4 申 请 资 源 , 其 请 求 向 量 为
0
Request (1,2,0),系统按银行家算法进行检查:
4
Request (1,2,0) ≤ Need (4,2,0),且
4 4
Request (1,2,0) > Available (0,1,1)
4
lP4的请求向量超过系统的可用资源向量,故P4的请
求不能满足。进程P4阻塞等待
l如果T 时刻,进程P4申请资源,其请求向量为
0
Request (0,1,0),系统是否能将资源分配给它?
4D e a d l o c k A v o i d a n c e
lMaximum resource requirement must be stated in advance(预先必须申明
每个进程需要的资源总量)
lProcesses under consideration must be independent; no
synchronization requirements (进程之间相互独立,其执行顺序取决于系统
安全,而非进程间的同步要求)
lThere must be a fixed number of resources to allocate(系统必须提供
固定数量的资源供分配)
lNo process may exit while holding resources(若进程占有资源,则不能
让其退出系统)D e a d l o c k D e t e c t i o n ( 检 测 死 锁 )
l 检测死锁不同于预防死锁,不限制资源访问方
式和资源申请。
l OS周期性地执行死锁检测例程,检测系统中是
否出现“环路等待”。S t r a t e g i e s o n c e D e a d l o c k D e t e c t e d
l Abort all deadlocked processes.
l Back up each deadlocked process to some previously defined checkpoint, and
restart all process.
• original deadlock may occur.
l Successively abort deadlocked processes until deadlock no longer exists.
l Successively preempt resources until deadlock no longer exists.S e l e c t i o n C r i t e r i a D e a d l o c k e d
P r o c e s s e s
l Least amount of processor time consumed so far.
l Least number of lines of output produced so far.
l Most estimated time remaining.
l Least total resources allocated so far.
l Lowest priority.D i n i n g P h i l o s o p h e r s P r o b l e m
l描述:有5个哲学家,他们的生活方式是交替地进行思考和进餐。哲学家们
共用一张圆桌,分别坐在周围的五张椅子上。圆桌中间放有一大碗面条,
每个哲学家分别有1个盘子和1支叉子。如果哲学家想吃面条,则必须拿到
靠其最近的左右两支叉子。进餐完毕,放下叉子继续思考。
l要求:设计一个合理的算法,使全部哲学家都能进餐(非同时)。算法必
须避免死锁和饥饿,哲学家互斥共享叉子。D i n i n g P h i l o s o p h e r s P r o b l e mD i n i n g P h i l o s o p h e r s P r o b l e m - b y S e m a p h o r e sD i n i n g P h i l o s o p h e r s P r o b l e m
l 可能产生死锁!
l 可行的解决方案:只允许4个哲学家同时进餐厅用
餐,则至少有一个哲学家可以拿到两支叉子进餐,
完毕,放下叉子,其他哲学家就可进餐。不会出
现死锁和饥饿Program diningphilosophers;
Var fork:array [0..4] of semaphore (:= 1);
room : semaphore (:= 4);
i : integer;
procedure philosopher (i : integer);
begin
repeat
think; /* 哲学家正在思考 */
wait(room); /* 第5位哲学家将被阻塞在room信号量队列 */
wait(fork[i]); /* 取其左边的筷子 */
wait(fork[(i + 1) mod 5]); /* 取其右边的筷子 */
eat; /* 吃面条 */
signal(fork[(i + 1) mod 5]); /* 放回右边的筷子 */
signal(fork[i]); /* 放回左边的筷子 */
signal(room); /* 唤醒阻塞在room信号量队列中的哲学家 */
forever
end;
begin
parbegin
philosopher(0); philosopher(1); philosopher(2); philosopher(3); philosopher(4);
parend
end.内 容
Memory Management Requirements
1
Memory Management Techniques
2
Virtual Memory Management Techniques
3L e a r n i n g o b j e c t i v e s ( 3 . 1 - 3 . 2 )
By the end of this lecture you should be able to:
ØUnderstand Memory Management Requirements
• Explain what’s Relocation(重定位), Logical Organization
vs. Physical Organization, Logical/Physical Address,
Relative/Absolute Address, Overlay(覆盖), Page/ Frame,
Translation Lookaside Buffer(联想存储器,快表)L e a r n i n g o b j e c t i v e s ( 3 . 1 - 3 . 2 )
( c o n t i n u e )
By the end of this lecture you should be able to:
ØUnderstand Memory Management Techniques
• Fixed Partitioning, Dynamic Partitioning , Simple Paging,
Simple Segmentation
• Explain Internal/External Fragmentation(内/外零头),
Compaction(紧凑 )
• Understand Dynamic Partitioning Placement Algorithm,
Logical-to-Physical Address Translation存储管理需求分析M e m o r y M a n a g e m e n t
Ø Subdividing memory to accommodate multiple processes.
Ø Memory needs to be allocated efficiently to pack as many
processes into memory as possible.M e m o r y M a n a g e m e n t R e q u i r e m e n t s
Ø Relocation (重定位)
Ø Memory Protection (存储保护)
Ø Memory Sharing (存储共享)
Ø Logical Organization (逻辑组织)
Ø Physical Organization (物理组织)R e l o c a t i o n
Ø Programmer does not know where the program will
be placed in memory when it is executed.
Ø While the program is executing, it may be swapped
to disk and returned to main memory at a
different location (relocated).
Ø Memory references(存储引用)must be translated in
the code to actual physical memory address.R e l o c a t i o n
ØWhen program loaded into memory the actual (absolute) memory
locations are determined.
ØA process may occupy different partitions which means
different absolute memory locations during execution (from
swapping).
ØCompaction will also cause a program to occupy a different
partition which means different absolute memory locations.A d d r e s s e s
Ø Logical
• reference to a memory location independent of the current
assignment of data to memory.
• translation must be made to the physical address.
Ø Relative
• address expressed as a location relative to some known point.
Ø Physical
• the absolute address or actual location in main memory.P r o t e c t i o n
ØProcesses should not be able to reference memory
locations in another process without permission.
ØImpossible to check absolute addresses in
programs at compile time since the program could
be relocated.
ØMust be checked during execution by the
processor(hardware) rather than OS(software)
ØOperating system cannot anticipate all of the
memory references that a program will make.S h a r i n g
ØAllow several processes to access the
same portion of memory.
ØBetter to allow each process (person)
access to the same copy of the
program rather than have their own
separate copy.L o g i c a l O r g a n i z a t i o n
Ø Programs are written in modules.
Ø Modules can be written and compiled independently.
Ø Different degrees of protection given to modules (read-only,
execute-only).
Ø Share modules.P h y s i c a l O r g a n i z a t i o n
ØComputer memory is organized into at least
two levels, referred as main memory and
secondary memory.
ØMemory available for a program plus its
data may be insufficient.
- Overlaying allows various modules to be
assigned the same region of memory.
ØProgrammer does not know how much space
will be available.存储划分技术
分区技术覆盖与交换Memory Management Techniques
ØFixed Partitioning(固定分区)
ØDynamic Partitioning(动态分区)
ØSimple Paging(简单分页)
ØSimple Segmentation(简单分段)
ØVirtual-Memory Paging(虚拟存储分页)
ØVirtual-Memory Segmentation(虚拟存储分段)P a r t i t i o n i n g T e c h n i q u e
Ø 单一连续分配
Ø Fixed Partitioning Technique(固定分区技术)
Ø Dynamic Partitioning Technique(动态分区技术)
Ø Buddy System(伙伴系统)F i x e d P a r t i t i o n i n g
Ø 系统初始启动时将内存划分为数目固定、尺寸固定的多个分区。
Ø 这些分区的尺寸可以相等也可以不等。F i x e d P a r t i t i o n i n g
Ø Equal-size partitions
● any process whose size is less than or equal to the
partition size can be loaded into an available partition.
● if all partitions are full, the operating system can swap a
process out of a partition.F i x e d P a r t i t i o n i n g
ØA program may be too big to fit into a partition.
The programmer must design the program with
overlays .
ØMain memory use is inefficient. Any program, no
matter how small, occupies an entire partition.
This is called internal fragmentation (内零头).
ØBoth of these problems can be lessened, though not
solved, by using unequal-size partitions.P l a c e m e n t A l g o r i t h m w i t h P a r t i t i o n s
Ø Equal-size partitions
● because all partitions are of equal size, it does not matter which partition is
used.
Ø Unequal-size partitions
● can assign each process to the smallest partition within which it will fit.
● queue for each partition.
● processes are assigned in such a way as to minimize wasted memory within a
partition.Fixed Partitioning (长处和不足)
Ø Advantages
§ 实现简单;
§ 系统开销小。
Ø Disadvantages
§ 存在Internal Fragment, 存储利用率不高;
§ 分区尺寸固定,系统无法运行大程序;
§ 分区数目固定,使活动进程的数目受限D y n a m i c P a r t i t i o n i n g
Ø Partitions are of variable length and number.
Ø Process is allocated exactly as much memory as required.
Ø Eventually get holes in the memory. This is called external fragmentation(外零
头).
Ø Must use compaction(紧凑) to shift processes so they are contiguous and all
free memory is in one block.D y n a m i c P a r t i t i o n i n g ( 外 零 头 和 紧 凑 技
术 )
动态分区为进程分配大小合适的分区,消除了分区
Ø
Internal Fragment。但是,却产生分区External
Fragment
Ø Compaction :为了使外零头得到充分利用,可以将把内存
中的所有空闲分区拼接成一个较大的空闲分区。即系统可以
把内存中的所有进程移到内存的某一端;相应地,内存中的
所有空闲分区将被移到内存的另一端。
ØCompaction技术要求系统具有动态重定位的能力D y n a m i c P a r t i t i o n i n g P l a c e m e n t
A l g o r i t h m
Ø First-fit algorithm(首次适应算法)
§ Scan memory from the beginning and choose the
first available block that is large enough.
§ Simplest, Best and Fastest.
§ May have many process loaded in the front end
of memory that must be searched over when
trying to find a free block.D y n a m i c P a r t i t i o n i n g P l a c e m e n t
A l g o r i t h m
Ø Next-fit(邻近适应算法)
§ Scan memory from the location of the last placement and choose
the next available block that is large enough.
§ More often allocate a block of memory at the end of memory where
the largest block is found.
§ The largest block of memory is broken up into smaller blocks.
§ Compaction is required more frequently to obtain a large block at
the end of memory.D y n a m i c P a r t i t i o n i n g P l a c e m e n t
A l g o r i t h m
Ø Best-fit algorithm(最佳适应算法)
§ Chooses the block that is closest in size to the request.
§ Worst performer overall.
§ Since smallest block is found for process, the smallest amount of
fragmentation is left. Memory compaction must be done more often.D y n a m i c P a r t i t i o n i n g P l a c e m e n t
A l g o r i t h m
Ø Worst-fit(最差适应算法)R e g i s t e r s U s e d d u r i n g E x e c u t i o n
Ø Base register(基址寄存器)
• starting address for the process.
Ø Bounds register(界限寄存器)
• ending location of the process.
Ø These values are set when the process is loaded and when
the process is swapped in.R e g i s t e r s U s e d d u r i n g E x e c u t i o n
Ø The value of the base register is added to a relative address to produce an
absolute address.
Ø The resulting address is compared with the value in the bounds register.
Ø If the address is not within bounds, an interrupt is generated to the operating
system.B u d d y S y s t e m ( 伙 伴 系 统 )
Ø Entire space available is treated as a single block of 2U.
Ø If a request of size s such that 2U-1 < s <= 2U, entire
block is allocated.
● Otherwise block is split into two equal buddies.
● Process continues until smallest block greater than or equal
to s is generated.存储划分技术
简单分页技术S i m p l e P a g i n g T e c h n i q u e
Ø Partition memory into small equal-size chunks and divide
each process into the same size chunks.
Ø The chunks of a process are called pages and chunks of
memory are called frames(页框、页帧).Simple Paging Technique (简单分页技术)
Ø Operating system maintains a page table(页表) for each process.
- contains the frame location for each page in the
process.
- logical address consists of a page number (页号)and
an offset (偏移量) within the page.
Ø A single free-frame list (空闲页框表) of all frames in main memory that are
currently unoccupied and available for pages.P a g e T a b l e s f o r E x a m p l eL o g i c a l - t o - P h y s i c a l A d d r e s s
T r a n s l a t i o n
Ø Address translation is done by processor hardware (MMU,存储管理单元).
Ø The processor must know how to access the page table of the current process,
using page table register (页表寄存器).
Ø Logical address :page number, offset.
Ø Physical address:frame number, offset.L o g i c a l - t o - P h y s i c a l A d d r e s s
T r a n s l a t i o n
在分页系统中,地址变换步骤:
Ø 根据逻辑地址及页大小求出页号和页内偏移;
Ø 用页号检索页表,查找指定页对应的页框号;
Ø 根据页框号、页内偏移以及页框尺寸求出物理地
址Translation Lookaside Buffer (快表-加快页表检索速度)
Ø Each virtual memory reference can cause two physical
memory accesses.
• one to fetch the page table.
• one to fetch the data.
Ø To overcome this problem a high-speed cache is set up for
page table entries.
• called the TLB - Translation Lookaside Buffer.T r a n s l a t i o n L o o k a s i d e B u f f e r
Ø Contains page table entries that have been most recently
used.
Ø Functions same way as a memory cache.T r a n s l a t i o n L o o k a s i d e B u f f e r
Ø Given a virtual address, processor examines the TLB.
Ø If page table entry is present (a hit), the frame number is retrieved and the
real address is formed.
Ø If page table entry is not found in the TLB (a miss), the page number is used
to index the process page table.T r a n s l a t i o n L o o k a s i d e B u f f e r
Ø First checks if page is already in main memory .
• if not in main memory a page fault is issued.
Ø The TLB is updated to include the new page entry.S i z e o f P a g e & P a g e T a b l e s
1. Page size
2. Page table size
3. For huge page table
§ Organization
§ StorageP a g e S i z e
Ø Less internal fragmentation, small page size
• Smaller page size, more pages required per process.
• More pages per process means larger page tables.
• Larger page tables means large portion of page tables in
virtual memory.
Ø Secondary memory favors large page size
• Secondary memory is designed to efficiently transfer large
blocks of data .P a g e S i z e
Ø Small page size
• large number of pages will be found in main memory.
• As time goes on during execution, the pages in memory
will all contain portions of the process near recent
references.
• Page faults (页面访问失败,缺页)low.
Ø Increased page size
• causes pages to contain locations further from any recent
reference.
• Page faults rise.P a g e S i z e
Ø Multiple page sizes provide the flexibility needed to
effectively use a TLB.
Ø Large pages can be used for program instructions.
Ø Small pages can be used for threads.
Ø Most operating system support only one page size.E x a m p l e P a g e S i z e sP a g e T a b l e S t r u c t u r e
Ø 许多计算机系统支持大容量虚拟内存。比如,在VAX系统中,每个进程使用的
虚拟内存最大容量为 ( = 2G) 个字节。
Ø 大容量虚拟内存的实现需要大页表。比如,在VAX系统中,页面的大小为
(=512 )个字节,因此如果某进程使用的虚拟内存容量为系统规定的最大容
量, 那么该进程的页表将有多达 (= 4M)个表项。
Ø 下面介绍三种解决“大页表占用大内存”的方法V i r t u a l P a g e T a b l e s
Ø The entire page table may take up too much main memory.
Ø Page tables are also stored in virtual memory.
Ø When a process is running, part of its page table is in
main memory.
Ø How to deal with it?多级页表 Two-Level Scheme for 32-bit addressInverted Page Table (反置页表)存储划分技术
简单分段技术S i m p l e S e g m e n t a t i o n T e c h n i q u e
Ø 基于模块化程序设计时,程序员常根据需要将进程分割成
许多大小不一定相同的Segment ,系统则将 物理内存动
态地划分成许多尺寸不一定相等的
Ø 当一个进程被装入物理内存时,系统将为该进程的每个段
独立地分配一个分区;同一进程的多个段不必存放在连续
的多个分区中.S i m p l e S e g m e n t a t i o n T e c h n i q u e
Ø All segments of all programs do not have to be of the same
length.
Ø There is a maximum segment length.
Ø Logical address consists of two parts : a segment number
and an offset.
Ø Since segments are not equal, segmentation is similar to
dynamic partitioning.D a t a S t r u c t u r e i n F r a g m e n t a t i o n
Ø Segment Table(段表)
用于描述进程的分段情况,记载进程的各个段到物理内存中分区的映射情
§
况。
§ 基本元素:段号、段的物理起始地址、段长度
Ø Partition Table(分区表)
§ 用于记载物理内存的分区情况分段系统中的地址变换和存储保护过程
Ø 在分段系统中,CPU将按照以下步骤进行地址变换和存储保护:
逻辑地址中的段号为索引检索段表从而得到指定段所对应的段表项;
•
若逻辑地址中的段内偏移大于段表项中段的长度,则产生存储保护中断
•
(该中断将由OS处理);
把逻辑地址中的段内偏移与段表项中的分区的起始物理地址相加从而得到
•
物理地址S e g m e n t a t i o n T a b l e R e g i s t e r
Ø 在分段系统中,段表被保存在物理内存中。
Ø 段表寄存器是CPU中的一个硬件设施,用来存放当前正在运行的进程其段表在物理内存中的起始物
理地址。
Ø 当系统调度某个进程运行时,它将从该进程的PCB中取出相应的值填入段表寄存器Address Translation in Segmentation SystemProtection
and
SharingS e g m e n t P r o t e c t i o n
Ø 越界检查
§ 为了进行越界检查,系统在段表寄存器中保存当前进程的段表长度并在每
个段表项中保存各个段的长度。
Ø 存取控制
§ 为了进行存取控制,系统在每个段表项中设置一个 “存取控制”字段,用
于规定进程对段的访问权限S e g m e n t P r o t e c t i o n
Ø 为了进行环保护,系统将在每个 中保存各个段所在环的特权
级别。
Ø 环保护 通常遵循以下保护规则:
一个程序可以调用驻留在同级环或较高特权环中的服务;
•
一个程序可以访问驻留在同级环或较低特权环中的数据
•环0
环0
调用 ⊙ 返
回
环1
环1
数据访问 ⊙
调用
⊙ 环2
环
2 返回
⊙
数据访问
返回⊙调用P a g i n g vs. S e g m e n t a t i o n
Ø 页面是信息的物理单位;在特定的系统中其大小是固定不变的,不随进
程的不同而不同。
Ø 段是信息的逻辑单位,其长度不定;即使是属于同一进程的两个段其
长度也可能不等。P a g i n g vs. S e g m e n t a t i o n
Ø 分页活动源于系统管理物理内存的需要,在系统内部进行,由系统实施,用户看不见。
Ø 分段活动源于用户进行模块化程序设计的需要,在系统外部进行,由用户实施,用户是知道的。P a g i n g vs. S e g m e n t a t i o n
Ø 在分页系统中,逻辑地址是一维的。
Ø 在分段系统中,逻辑地址是二维或多维的A d v a n t a g e s o f S e g m e n t a t i o n / P a g i n g
Ø 分页系统中,内零头得到了有效的抑制,外零头则完全被消除;因此,使用分
页技术可以提高物理内存的利用率。
Ø 分段系统中,动态数据结构、程序和数据共享、程序和数据保护等问题得到了
妥善解决;因此,分段技术有利于模块化程序设计。
Ø 段页技术汲取了分页技术和分段技术的上述优点C o m b i n e d P a g i n g a n d S e g m e n t a t i o n
Ø Paging is transparent to the programmer.
Ø Paging eliminates external fragmentation.
Ø Segmentation is visible to the programmer.
Ø Segmentation allows for growing data structures, modularity, and support for
sharing and protection.
Ø Each segment is broken into fixed-size pages.C o m b i n e d P a g i n g a n d S e g m e n t a t i o n
Ø在段页存储管理系统中,每个进程均被编程人员分割成
多个Segment, 每个段又被系统分割成多个Page。
Ø相应地,物理内存被系统划分成多个Frame。
Ø当一个进程被装入物理内存时,系统为该进程的每个段
的各页面独立地分配一个Frame;一个进程的同一段的
多个页面不必存放在连续的多个Frame中。C o m b i n e d S e g m e n t a t i o n a n d P a g i n gAddress Translation in Segmentation/paging System虚拟存储管理技术概述L e a r n i n g O b j e c t i v e s ( 3 . 3 - 3 . 4 )
By the end of this lecture you should be able to:
ØUnderstand Virtual Memory Management
Techniques
- Virtual-Memory Paging, Virtual-Memory
Segmentation.
- Principle of Locality(局部性原理),
Virtual Memory, Thrashing(抖动).L e a r n i n g O b j e c t i v e s ( 3 . 3 - 3 . 4 )
( c o n t i n u e )
By the end of this lecture you should be able to:
ØUnderstand虚拟存储管理技术中的软件策略
- Demand Paging(请求调页)/Prepaging(预调页).
- Basic Replacement Algorithms: Optimal Algorithm, Least
Recently Used Algorithm, First-in First-out Algorithm, Clock
Algorithm.S i m p l e M e m o r y M a n a g e m e n t T e c h n i q u e s
当进程运行时,该进程相关的程序和数据
全部驻留内存H a r d w a r e a n d C o n t r o l S t r u c t u r e s
Ø Memory references are dynamically
translated into physical addresses at run
time.
A process may be swapped in and out of main
•
memory such that it occupies different regions.
Ø A process may be broken up into pieces that
do not need to located contiguously in main
memory.
All pieces of a process do not need to be loaded
•
in main memory during execution.Principle of Locality
(局部性原理)
Ø Program and data references within a process tend to
cluster(簇).
Ø Only a few pieces of a process will be needed over a short
period of time.
Ø Possible to make intelligent guesses about which pieces
will be needed in the future.
Ø This suggests that virtual memory may work efficiently.E x e c u t i o n o f a P r o g r a m
Ø Operating system brings into main memory a few pieces of the program.
Ø Resident set(驻留集) - portion of process that is in main memory.
Ø An interrupt is generated when an address is needed that is not in main memory.
Ø Operating system places the process in a blocked state.E x e c u t i o n o f a P r o g r a m
Ø Piece of process that contains the logical address is
brought into main memory.
● Operating system issues a disk I/O Read request.
● Another process is dispatched to run while the disk I/O
takes place.
● An interrupt is issued when disk I/O complete which
causes the operating system to place the affected
process in the Ready state.A d v a n t a g e s o f B r e a k i n g u p a P r o c e s s
ØMore processes may be maintained in main memory.
● Only load in some of the pieces of each process.
● With so many processes in main memory, it is very likely a process will be in the
Ready state at any particular time.
Ø A process may be larger than all of main memory.T y p e s o f M e m o r y
Ø Real memory
Main memory
•
Ø Virtual memory
Memory on disk
•
Allows for effective multiprogramming and
•
relieves the user of tight constraints of
main memory.V i r t u a l M e m o r y
Ø 使用虚拟存储管理技术,用户将会感觉到系统的内存空间比实际内存大。
Ø 系统的可用内存空间并非计算机系统中的实际物理内存,它包含物理内
存及一部分磁盘空间。
Ø 习惯上,人们把这种用户感觉上存在但实际上并不存在的内存称为虚拟
内存。T h r a s h i n g ( 抖 动 )
Ø Swapping out a piece of a process just before that piece
is needed.
Ø The processor spends most of its time swapping pieces
rather than executing user instructions.S u p p o r t N e e d e d f o r V i r t u a l M e m o r y
Ø Hardware must support paging and segmentation .
Ø Operating system must be able to management the movement of
pages and/or segments between secondary memory and main
memory.虚拟分页和
虚拟分段技术P a g i n g
Ø Each process has its own page table.
Ø Each page table entry contains the frame number of the corresponding page in
main memory.
Ø A bit is needed to indicate whether the page is in main memory or not.M o d i f y B i t i n P a g e T a b l e
Ø Another modify bit is needed to indicate if the page has
been altered since it was last loaded into main memory.
Ø If no change has been made, the page does not have to be
written to the disk when it needs to be swapped out.P a g e T a b l e E n t r i e sV i r t u a l - M e m o r y S e g m e n t a t i o n
T e c h n i q u e
Ø May be unequal, dynamic size.
Ø Simplifies handling of growing data structures.
Ø Allows programs to be altered and recompiled independently.
Ø Lends itself to sharing data among processes.
Ø Lends itself to protection.S e g m e n t T a b l e s
Ø Corresponding segment in main memory.
Ø Each entry contains the length of the segment.
Ø A bit is needed to determine if segment is already in main memory.
Ø Another bit is needed to determine if the segment has been modified since it
was loaded in main memory.S e g m e n t T a b l e E n t r i e sVirtual-Memory Paging -Segmentation虚拟存储管理的软件策略概述虚拟存储管理技术中的软件策略
Ø OS存储管理子系统的设计要素
§ 是否使用虚拟存储管理技术
§ 选用分页技术、分段技术还是段页技术
§ 采用什么样的存储管理算法
其中,
前两个问题属于CPU硬件设计范畴;从OS设计者的角度看,它们相当于“选
•
用什么样的硬件平台作为 OS的运行平台”。
最后一个问题属于OS软件设计范畴;该问题是后面讨论的重点内容
•虚拟存储管理技术中的软件策略
Ø 除了一些老的个人计算机 OS(比如 MS-DOS)以
及专用OS外,绝大多数 OS 均使用了虚拟存储管
理技术。
Ø 单纯的分段技术现在已经很少使用;绝大多数 OS
选用了分页技术或者段页技术。
Ø 因此,我们主要讨论虚拟存储管理技术中的软件策略
Ø Resident Set Management(驻留集管理)
Ø Placement Policy(放置策略)
Ø Fetch Policy(获取策略)
Ø Replacement Policy(置换策略)
Ø Cleaning Policy(清除策略)
Ø Load Control(负载控制)R e s i d e n t S e t M a n a g e m e n t
解决的问题:
系统应当为每个活动进程分配多少个frame?R e s i d e n t S e t
指进程驻留在内存中的页面所组成的集合。
解决的基本问题即:每个活动进程驻留集的尺寸应当为多大?影 响 F r a m e 分 配 的 因 素
Ø分配给每个活动进程的frame数越少,在内存中驻留的
活动进程数就越多,进程调度程序能调度就绪进程的概
率就越大。
Ø分配给每个活动进程的frame数越少,每个活动进程在
其运行过程中发生缺页中断的概率就越大。
Ø分配给每个活动进程的frame数超过一定限度时,多余
的frame并不能显著地降低每个活动进程的缺页中断率R e s i d e n t S e t S i z e
Ø Fixed-allocation
● gives a process a fixed number of pages within which to execute
● when a page fault occurs, one of the pages of that process must be replaced
Ø Variable-allocation
● number of pages allocated to a process varies over the lifetime of the process调整驻留集尺寸的方法 (工作集方法:1)
Ø 一个进程的工作集 是指,在时间 之前的一段时间 内该进程所引用的
全部页面的集合。
Ø 工作集 是时间 的函数。一般说来, 不同, 的尺寸和/或元素
也不同。
Ø 通常人们把 称为工作集 的窗口尺寸。显然,工作集 也是窗口
尺寸 的函数。一般说来,窗口尺寸 越大,工作集尺寸 也就越
大。
Ø 要保证一个进程有效地运行,系统就必须保证 该进程的工作集都驻留
在内存中。调整驻留集尺寸的方法 (工作集方法:2)
Ø 从理论上讲,系统可以通过监控一个进程的工作集来调整其驻留集。
Ø 然而,实践中,监控各个进程的工作集将导致很大的系统开销。
Ø 而且,监控到的工作集信息是进程的历史;因此,据此调整各个进程的驻留集
未必能够获得好的系统性能。
Ø 此外,窗口尺寸的最佳值也是无法确定的。调整驻留集尺寸的方法 (PFF算法:1)
Ø 基于工作集方法的基本原理,人们在实践中提出了缺页频率算法 (Page-Fault Frequency
Algorithm,简称PFF)。
Ø 使用PFF算法的系统通过监控各个进程的页面访问失败率来调整各个进程的驻留集尺寸。调整驻留集尺寸的方法 (PFF算法:2)
Ø PFF算法将按照以下规则调整各进程的驻留集尺寸:
§ 如果一个进程的缺页率低于某个最小阈值,那么系统将减小该进程的驻留
集尺寸;
§ 如果一个进程的缺页率高于某个最大阈值,那么系统将增加该进程的驻留
集尺寸。
Ø 因为缺页率与缺页时间间隔成反比,因此,可以通过测量缺页时间
间隔来监控缺页率。P l a c e m e n t P o l i c y
解决的基本问题:
系统应当在内存的什么位置为活动进程分配frame?P l a c e m e n t P o l i c y
Ø 绝大多数使用虚拟存储分页技术的系统可以在内存中的任何位置为
活动进程分配frame。
Ø 但是,有些系统需要专门技术解决内存中页面的放置问题。F e t c h P o l i c yF e t c h P o l i c y
ØDemand paging(请求调页) only brings pages into main
memory when a reference is made to a location on the
page.
- Many page faults when process first started.
ØPrepaging(预调页) brings in more pages than needed.
- More efficient to bring in pages that reside contiguously on the
disk.Ø Which page is replaced?
Ø 当一个页面欲装入内存时,系统应当在什么范围内判断已经没有空闲frame
分配给新的页面?
Ø 当系统在指定的范围内发现已经没有空闲frame分配给新的页面时,系统
应当从指定的范围内选择哪个页面移出内存?固定分配,局部置换
可变分配,全局置换
可变分配,局部置换驻留集尺寸与置换范围
Ø 固定分配策略 局部置换策略;反之,
全局置换策略 可变分配策略
Ø 可变分配策略与局部置换策略也可组合,即系统可根据性
能的需要增加或减少分配给每个活动进程的frame数;当
进程的frame全部用完,而需要装入一个新的页面时,系
统将在该进程的当前驻留集中选择一 个页面移出内存R e p l a c e m e n t P o l i c y ( 置 换 注 意 事 项 )
Ø Frame Locking
● If frame is locked, it may not be replaced.
● Kernel of the operating system.
● Control structures.
● I/O buffers.
● Associate a lock bit with each frame.R e p l a c e m e n t P o l i c y
Ø Page removed should be the page least likely to be
referenced in the near future.
Ø Most policies predict the future behavior on the basis of
past behavior.页面置换算法B a s i c R e p l a c e m e n t A l g o r i t h m s
Ø Optimal Algorithm(最佳算法)
Ø Least Recently Used Algorithm(最近最少使用算法)
Ø First-in First-out Algorithm(先进先出算法)
Ø Clock Algorithm(时钟算法)O p t i m a l A l g o r i t h m ( 最 佳 置 换 算 法 )
Ø 置换在将来再也不被访问的页面
Ø 置换在最远的将来才被访问的页面Optimal AlgorithmOptimal Algorithm
Ø 从原理上讲,OPT算法在所有置换算法中具有最佳性能;但,
Impossible to have perfect knowledge of future events
Ø 在OS研究领域,OPT算法常常被用作比较各种置换算法性能的参照标准。Least Recently Used (LRU) (最近最少使用置换算法)
Ø Replaces the page that has not been referenced for the
longest time.
Ø By the principle of locality, this should be the page least
likely to be referenced in the near future.
Ø Each page could be tagged with the time of last reference.
This would require a great deal of overhead.Least Recently Used (LRU)L e a s t R e c e n t l y U s e d ( L R U )
Ø从原理上讲,LRU算法可以被实现;但任何一种实现方法都将产生很大的
系统开销。因此,许多OS均使用近似LRU算法。
Ø一些应用程序具有很强的非局部存储引用(比如顺序存储引用)特征。对
于此类应用程序,LRU算法将有很差的表现。F i r s t - i n , f i r s t - o u t ( F I F O )
Ø Treats page frames allocated to a process as a circular
buffer.
Ø Pages are removed in round-robin style.
Ø Simplest replacement policy to implement.
Ø Page that has been in memory the longest is replaced.
Ø These pages may be needed again very soon.First-in, first-out (FIFO)First-in, first-out (FIFO)
Ø FIFO算法最简单,最容易实现
Ø 如果应用程序具有顺序存储引用特征,那么使用FIFO算法将获得很好的
性能。
Ø 不过,大多数应用程序具有局部存储引用特征。对于此类应用程序,
FIFO算法的性能是很差的。First-in, first-out (FIFO)
Ø 容易产生抖动。
Ø 可能存在Belady现象。
Ø Belady现象:虚拟存储系统中的一种异常现象,
即增加进程的页框数,缺页率反而上升。First-in, first-out (FIFO)
若某进程的页面访问序列为:1, 2, 3, 4, 1, 2, 5,
1, 2, 3, 4, 5。若为进程分配的页框数为3,则缺页9
次。
FIFO 1 2 3 4 1 2 5 1 2 3 4 5
页
0 1 2 3 4 1 2 5 5 5 3 4 4
页
1 1 2 3 4 1 2 2 2 5 3 3
页
2 1 2 3 4 1 1 1 2 5 5
缺 页 √ √ √
x x x x x x x x XFirst-in, first-out (FIFO)
若为进程分配的页框数为4,则缺页10次。
FIFO 1 2 3 4 1 2 5 1 2 3 4 5
页
0 1 2 3 4 4 4 5 1 2 3 4 5
页
1 1 2 3 3 3 4 5 1 2 3 4
页
2 1 2 2 2 3 4 5 1 2 3
页 3 4 2
3 1 1 1 2 5 1
缺 页 √ √
x x x x x x x x x x
思考:为什么FIFO算法会出现Belady现象?Clock Policy (时钟置换算法)
Ø Additional bit called a use bit.
Ø When a page is first loaded in memory, the use
bit is set to 1.
Ø When the page is referenced, the use bit is set
to 1.
Ø When it is time to replace a page, the first
frame encountered with the use bit set to 0 is
replaced.
Ø During the search for replacement, each use bit
set to 1 is changed to 0.C l o c k P o l i c y
Ø CLOCK算法中,系统将置换范围内的所有frame 组成一 个环形缓冲区,
并为其设置一个扫描指针
Ø 没有进行页面置换时,扫描指针总是指向上一次进行页面置换时被置换
页面所在位置的下一个位置。C l o c k P o l i c y
Ø 当需要进行页面置换时,系统将移动扫描指针搜索置换范围内的各个frame以
便找到一个U位为0的frame:
如果当前扫描指针所指向的frame其U位为1,那么系统将该frame的U位设置
•
为0,扫描指针移到下一个位置,继续搜索;
如果当前扫描指针所指向的frame其U位为0,则系统将该frame中的页面作
•
为被置换页面,同时把扫描指针移到下一个位置,停止搜索。1Clock Policy时钟置换算法的改进
Ø 系统把一个页面移出内存时,如果该页面驻留内存期间没有被修改过,那么不
必把它写回辅存,否则系统必须把它写回辅存。这表明,换出未修改过的页面
比换出被修改过的页面开销小。
Ø 显然,可以依据上述结论改进CLOCK算法。改进后的CLOCK算法将在置换范围内
首选在最近没有使用过、没有被修改过的页面作为被置换页面。时钟置换算法的改进
Ø 系统为内存的每个frame配置一个Modify Bit(简称为M位)。
Ø 改进后的CLOCK算法在选择被置换页面时将同时考虑U位和M位。
Ø 一个页面的U位与M位共有四种组合:
● U=0,M=0:最近没有被使用过,也没有被修改过;
● U=1,M=0:最近被使用过,但没有被修改过;
● U=0,M=1:最近没有被使用过,但被修改过;
● U=1,M=1:最近被使用过,也被修改过;时钟置换算法的改进
Ø 改进后的 CLOCK 算法按下列步骤选择被置换页面:
1、从当前位置开始搜索 U=0且 M=0 的frame。但不修改任何U位。若找到第
一个U=0且M=0的frame,那么系统将该frame中的页面置换出去, 算法终止。时钟置换算法的改进
• 2、如果第一步没有成功, 那么扫描指针将回到原位。 再次搜索U=0但M=1的frame。此搜索过程中,
如果遇到U位为1的frame,则将其U位修改为0。 若找到第一个U=0但M=1的frame,那么系统将该frame
中的页面选作被置换页面, 算法终止。
• 3、如果第二步也没有成功, 那么扫描指针将再次回到原位且置换范围内的所有frame其U位均为0。此
时,算法将返回第一步继续执行。