中国科学技术大学计算机系 陈香兰 xlanchen@ustc.edu.cn 2013Fall 第六讲 死锁及其处理 中国科学技术大学计算机系 陈香兰 xlanchen@ustc.edu.cn 2013Fall.

Slides:



Advertisements
Similar presentations
智慧老伯的一席話 原稿 : 溫 Sir 中譯 : 老柳 A man of 92 years, short, very well- presented, who takes great care in his appearance, is moving into an old people’s.
Advertisements

第七课:电脑和网络. 生词 上网 vs. 网上 我上网看天气预报。 今天早上看了网上的天气预报。 正式 zhèngshì (报告,会议,纪录) 他被这所学校正式录取 大桥已经落成,日内就可以正式通车 落伍 luòw ǔ 迟到 chídào 他怕迟到,六点就起床了.
胸痛中心的时间流程管理 上海胸科医院 方唯一.
Course 1 演算法: 效率、分析與量級 Algorithms: Efficiency, Analysis, and Order
上海第二医科大学 附属瑞金临床医学院检验系 洪秀华 卫蓓文
专题八 书面表达.
Business English Reading
武汉职业技术学院 微生物技术应用 背景知识四:微生物生长测定技术.
作業系統 第六章 同步與死結.
Performance Evaluation
資料庫設計 Database Design.
即兴中文讲演比赛 On-Site Speech 新型比赛项目
Scrum 实践.
个人总结及展望 主讲人:胡玲玲.
第6章 死結(Deadlock).
Mode Selection and Resource Allocation for Deviceto- Device Communications in 5G Cellular Networks 林柏毅 羅傑文.
Operating System Process Management - 4 Monday, August 11, 2008.
Operating System CPU Scheduing - 2 Monday, August 11, 2008.
Operating System CPU Scheduing - 3 Monday, August 11, 2008.
Minimum Spanning Trees
Module 5 Shopping 第2课时.
指導教授:許子衡 教授 報告學生:翁偉傑 Qiangyuan Yu , Geert Heijenk
Deadlocks P0 P1 Example System has 2 tape drives.
Applied Operating System Concepts
和春技術學院資訊管理系 九十三學年度第一學期 系統程式 第三章 死結(Deadlock)
第三章 处理机调度与死锁 3.1 处理机调度的层次和调度算法的目标 3.2 作业与作业调度 3.3 进程调度 3.4 实时调度
Draft Amendment to STANDARD FOR Information Technology -Telecommunications and Information Exchange Between Systems - LAN/: R: Fast BSS.
作業系統 第六章 同步與死結.
Guide to Freshman Life Prepared by Sam Wu.
HLA - Time Management 陳昱豪.
EVS-05-27e Action items7 China will provide language for low battery energy warning by next EVS IG meeting.
Course 9 NP Theory序論 An Introduction to the Theory of NP
创建型设计模式.
临界区软件互斥软件实现算法.
SpringerLink 新平台介绍.
Online job scheduling in Distributed Machine Learning Clusters
Lesson 44:Popular Sayings
临界区软件互斥软件实现算法 主讲教师:夏莹杰
Maths at Harrow: 在哈罗学习数学
第十五课:在医院看病.
Chapter 5 Recursion.
Operation System(OS).
Respect cannot be demanded, it must be earned
中国科学技术大学计算机系 陈香兰 2013Fall 第七讲 存储器管理 中国科学技术大学计算机系 陈香兰 2013Fall.
中国科学技术大学计算机系 陈香兰 Fall 2013 第三讲 线程 中国科学技术大学计算机系 陈香兰 Fall 2013.
Common Qs Regarding Earnings
從 ER 到 Logical Schema ──兼談Schema Integration
成才之路 · 英语 人教版 · 必修1 路漫漫其修远兮 吾将上下而求索.
安全状态的例子 例:假定系统有三个进程P1、P2、P3,共有12台磁带机。进程P1总共要求10台磁带机,P2和P3分别要求4台和九台。设在T0时刻,进程P1、P2和P3已经获得5台、2台和2台,还有3台空闲没有分配。 进程 最大需求 已分配 可用 P P2 4 2 P3 9 2 T0时刻系统时安全的。这时存在一个安全序列
高考应试作文写作训练 5. 正反观点对比.
SpringerLink 新平台介绍.
Distance Vector vs Link State
李元金 计算机与信息工程学院 第 10讲 处理机调度与死锁(4) 李元金 计算机与信息工程学院 1/
CHAPTER 6 Concurrency:deadlock And Starvation
Efficient Query Relaxation for Complex Relationship Search on Graph Data 李舒馨
Create and Use the Authorization Objects in ABAP
名词从句(2).
第八章 死锁 死锁的基本概念 死锁的解决方案 (预防,避免,检测及解除) 资源分配图.
资源分配与调度 第5章 资源分配与调度.
分布式系统 Distributed Systems 第 9 讲 协调和协定 Lecture 9 Coordination and Agreement 王晓阳、张 奇 复旦大学 计算机科学技术学院.
名词从句(4) (复习课).
Distance Vector vs Link State Routing Protocols
何正斌 博士 國立屏東科技大學工業管理研究所 教授
死 锁 Deadlock.
怎樣把同一評估 給與在不同班級的學生 How to administer the Same assessment to students from Different classes and groups.
Race Conditions and Semaphore
Hospitality English 酒店商务英语 讲师:罗云利 工商与公共管理学院.
Respect cannot be demanded, it must be earned
中正大學,資工系,作業系統實驗室 陽春副教授 羅習五
Presentation transcript:

中国科学技术大学计算机系 陈香兰 xlanchen@ustc.edu.cn 2013Fall 第六讲 死锁及其处理 中国科学技术大学计算机系 陈香兰 xlanchen@ustc.edu.cn 2013Fall

内容提要 死锁的定义和产生死锁的原因 产生死锁的必要条件 处理死锁的基本方法 Reading 死锁的预防 死锁的避免 死锁的检测与解除 计算机操作系统,汤子瀛,4.6、4.7、4.8节 Operating System Concepts,7Th edition,ch7

内容提要 死锁的定义和产生死锁的原因 产生死锁的必要条件 处理死锁的基本方法 死锁的预防 死锁的避免 死锁的检测与解除

死锁的定义 Deadlock 死锁 所谓死锁,是指多个进程因竞争资源而造成的一种僵局(Deadly-Embrace),若无外力作用,这些进程将永远不能再向前推进。

产生死锁的原因 归结为两点 竞争资源 进程推进顺序非法 为便于讨论,首先给出资源分配图的概念

资源分配图(Resource allocation Graph) 死锁可用资源分配图来描述 资源分配图是由一组结点N和一组边E所组成的一个有向图G=(N, E) N=P∪R P是一组进程结点,P={P1,P2,…,P3} R是一组资源结点,R={R1,R2,…,R3} e={Pi,Rj},或PiRj,资源请求边 e={Rj,Pi},或RjPi,资源分配边

资源分配图的图形表示 使用小圆卷表示一个进程 使用方框表示一个资源类型 使用一个点表示一个资源实例 请求边由进程指向方框; 分配边必须始于方框中的某个点 Pi Rj Pi Rj

资源分配图举例

发生死锁的资源分配图

一、竞争资源引起死锁 可剥夺和非剥夺性资源 竞争非剥夺资源 可剥夺资源,如CPU,内存 不可剥夺资源,如磁带机,打印机 Example: System has 2 tape drives P1 and P2 each hold one and each needs another one P1 tape1 tape2 P2

竞争临时性资源 临时性资源 vs. 永久性资源 S1、S2、S3是临时资源 P1:Release(S1);Request(S3) 不可能发生死锁 P1 S3 S1 P1:Request(S3);Release(S1) P2:Request(S1);Release(S2) P3:Request(S2);Release(S3) 可能发生死锁 P3 P2 S2

二、进程推进顺序不当引起死锁 基于进程的异步特性。 进程推进顺序合法 vs. 非法 举例:P1和P2竞争R1和R2

内容提要 死锁的定义和产生死锁的原因 产生死锁的必要条件 处理死锁的基本方法 死锁的预防 死锁的避免 死锁的检测与解除

产生死锁的必要条件Necessary Conditions 当下列4个必要条件同时具备时,就会产生死锁 互斥条件(Mutual exclusion) 请求和保持条件(Hold and wait) 不剥夺条件(No Preemptive) 环路等待(Circular wait)

1、互斥条件 指进程对所分配到的资源进行排它性使用。 对于这样的资源,不妨标记为R: 在一段时间内R只能有一个进程占有,例如P1 若此时还有其他进程(例如P2)要请求资源R,则P2只能被阻塞,直到P1用完后释放资源R R P1 P2

2、请求和保持条件 有一个进程(例如P1)已经获得了至少一个资源,但又提出了新的资源要求,而该资源又已经被其他进程所占有,此时P1被阻塞,但P1对已经获得的其他资源保持不放 被请求的其他资源 已经拥有的资源 P1

3、不剥夺条件 对进程已经占有的资源,在未使用完之前,不能被剥夺,只能在使用完时由自己释放

4、环路等待条件 发生死锁时,必然存在一个进程-资源的环形链 即存在进程集合 {P0,P1,P2,…,Pn} 其中,P0正在等待P1占用的资源;P1正在等待P2占用的资源;……;Pn正在等待P0占用的资源

发生死锁的资源分配图

If graph contains no cycles  no deadlock If graph contains a cycle  关于环和死锁 If graph contains no cycles  no deadlock If graph contains a cycle  if only one instance per resource type, then deadlock if several instances per resource type, possibility of deadlock

存在环,但没有发生死锁的资源分配图

内容提要 死锁的定义和产生死锁的原因 产生死锁的必要条件 处理死锁的基本方法 死锁的预防 死锁的避免 死锁的检测与解除

处理死锁的基本方法 Ensure that the system will never enter a deadlock state Deadlock prevention,死锁的预防 Breaks at least one of the necessary conditions Deadlock avoidance,死锁的避免 For each request, OS decides whether or not the process should wait Allow the system to enter a deadlock state and then recover Deadlock detect & recover 鸵鸟政策: Ignore the problem and pretend that deadlocks never occur in the system Used by most OS, including UNIX

内容提要 死锁的定义和产生死锁的原因 产生死锁的必要条件 处理死锁的基本方法 死锁的预防 死锁的避免 死锁的检测与解除

预防死锁的方法 破坏死锁的必要条件 对于互斥条件(Mutual Exclusion) 摒弃“请求和保持”条件 摈弃“不剥夺”条件 Not required for sharable resources; Must hold for non-sharable resources 无法破坏 摒弃“请求和保持”条件 摈弃“不剥夺”条件 摒弃“环路等待”条件

摒弃“请求和保持”条件 Must guarantee that whenever a process requests a resource, it does not hold any other resources Require process to 1)request and be allocated all its resources before it begins execution, or 2)allow process to request resources only when the process has none 简单、易于实现、安全 缺点:Low resource utilization; starvation possible.

摈弃“不剥夺”条件 No Preemption 有的程序重新运行会出问题、周转时间长、降低吞吐量 If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. Preempted resources are added to the list of resources for which the process is waiting Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. 有的程序重新运行会出问题、周转时间长、降低吞吐量

摒弃“环路等待”条件 Circular Wait 缺点: Impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. 无法形成环 申请高序号的进程总能获得资源 缺点: 序号限制了扩展性 强制资源使用的顺序,造成被占用资源闲置 降低了方便性

内容提要 死锁的定义和产生死锁的原因 产生死锁的必要条件 处理死锁的基本方法 死锁的预防 死锁的避免 死锁的检测与解除

死锁的避免 避免死锁的方法是: 将系统的状态划分为安全状态和不安全状态,通过施加一些较弱的限制条件,使得系统始终都处于安全状态 允许进程动态的申请资源,但在分配资源之前要对系统的安全状态进行判定,若分配操作是安全的,则分配;否则不予分配。

安全状态的定义: 所谓安全状态,是指系统能按照某种进程顺序 <P1, P2,…, Pn> 来为每个进程Pi分配其所需资源,直至满足每个进程对资源的最大需求,使每个进程都可以顺序的完成。 称上面的进程序列为安全序列 对于序列<P1, P2,…, Pn>, if for each Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j<i. 则该序列是安全的。

安全状态和不安全状态举例 System has 12 tape drivers Current is safe, since <P2, P1, P3> is a safe sequence If P3 requests 1 tape driver, and OS allocate one to it, the system enters an unsafe state Process Max request Allocated Available P1 10 5 3 P2 4 2 P3 9 Process Max request Allocated Available P1 10 5 2 P2 4 P3 9 3

安全状态和死锁的关系 If a system is in safe state  no deadlocks. If a system is in unsafe state  possibility of deadlock. Avoidance  ensure that a system will never enter an unsafe state.

Safe, unsafe , deadlock state spaces

Dijkstra的银行家算法 每类资源有多个资源实例 每个进程必须预先声明它所需要的最多资源数 当一个进程申请申请资源时,可能需要等待 当进程申请到所有资源后,必须在有限的时间内释放这些资源 需要设置若干数据结构来表达资源需求和分配情况

银行家算法中的数据结构 令 n = 进程数;m = 资源种类数 Available[0..m-1]: 长度为m的向量,可利用资源向量 Available[j] = k  资源Rj有k个空闲资源 Max[0..n-1][0..m-1]: n×m矩阵,最大请求矩阵. Max[i][j] = k 进程Pi最多将申请k个Rj资源 Allocation[0..n-1][0..m-1]: n×m矩阵,分配矩阵 Allocation[i,j] = k 进程Pi当前已经被分配了k个Rj资源 Need[0..n-1][0..m-1]: n×m矩阵,需求矩阵 Need[i][j] = k 进程Pi可能还需要再分配k个Rj资源 Need [i][j] = Max[i][j] – Allocation [i][j].

银行家算法 假设在某时刻, 进程Pi发出请求的资源请求向量为Requesti, 若Request[j]=k,即需要k个Rj类型的资源 针对上述请求 首先判断请求是否合法:比较Requesti与Needi 若Requesti≤Needi,合法,继续;不合法,系统出错 其次判断当前请求是否可能立即得到满足: 比较Requesti与Available Requesti ≤ Available,可能,继续;不可能,等待

尝试分配:假定将资源分配给进程Pi 执行安全算法,判定安全状态 暂时更新各个数据结构,Available、Allocationi、Needi Available := Available - Requesti; Allocationi := Allocationi + Requesti; Needi := Needi – Requesti; 执行安全算法,判定安全状态 安全,真正分配; 不安全,不予分配,Pi必须等待,返回原来资源分配状态

Safety Algorithm 安全检查算法需要两个工作向量 算法步骤如下: WORK,表示可用资源数,随着算法的进行不断发生变化 FINISH,表示经检查能顺利获得资源并运行完毕的进程 算法步骤如下:

Work[0..m-1] & Finish[0..n-1], initialize Work := Available Finish[i] = false for i = 0,1, …, n-1 Find i such that both Finish [i] = false Need[i]  Work If no such i exists, go to step 4 Work := Work + Allocation[i] Finish[i] := true go to step 2 If Finish [i] = true for all i, then the system is in a safe state, else its an unsafe state.

Example of Banker’s Algorithm P = {P0, P1, …, P4}; R={A(10), B(5), C(7)} Snapshot at time T0: Allocation Max Available A B C P0 0 1 0 7 5 3 3 3 2 P1 2 0 0 3 2 2 P2 3 0 2 9 0 2 P3 2 1 1 2 2 2 P4 0 0 2 4 3 3

Example (Cont.) The content of the matrix. Need is defined to be Max-Allocation. The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria. Need A B C P0 P1 P2 P3 P4 7 4 3 1 2 2 6 0 0 0 1 1 4 3 1 MAX A B C 7 5 3 3 2 2 - 9 0 2 2 2 2 4 3 3 Allocation A B C 0 1 0 2 0 0 = 3 0 2 2 1 1 0 0 2

Available=3 3 2 WORK Need Allocation Work+ Allocation Finish A B C P0 7 4 3 0 1 0 P1 1 2 2 2 0 0 P2 6 0 0 3 0 2 P3 0 1 1 2 1 1 P4 4 3 1 0 0 2 5 3 2 true 3 3 2 5 3 2 7 4 3 true 7 4 3 true 7 4 5 < P1, P3, P4, P2, P0> < P1, P3, P4, P0, P2>

Example (Cont.): P1 request (1,0,2) Check that Request  Available ( (1,0,2)  (3,3,2)  true) 由安全判定算法,sequence <P1, P3, P4, P0, P2> satisfies safety requirement. Can request for (3,3,0) by P4 be granted? Can request for (0,2,0) by P0 be granted? Allocation Need Available A B C P0 0 1 0 7 4 3 2 3 0 P1 3 0 2 0 2 0 P2 3 0 1 6 0 0 P3 2 1 1 0 1 1 P4 0 0 2 4 3 1 假装分配之后 作业

内容提要 死锁的定义和产生死锁的原因 产生死锁的必要条件 处理死锁的基本方法 死锁的预防 死锁的避免 死锁的检测与解除

Allow system to enter deadlock state 死锁的检测与解除 Allow system to enter deadlock state The system must provide Detection algorithm Recovery scheme

Single Instance of Each Resource Type The detect algorithm Wait-for graph, a variant of the resource-allocation graph Nodes are processes Pi  Pj if Pi is waiting for Pj Deadlock ≡ wait-for graph contains cycle Periodically invoked to search for a cycle in the graph Complexity O(n2), where n is the number of vertices (processes) in the graph

单个资源分配图的化简 从资源分配图化简到等待图 Corresponding wait-for graph Resource-Allocation Graph Corresponding wait-for graph

Available[0..m-1], 可利用资源向量 Available[i] the number of resources of Ri 若资源具有多个资源实例 令m = 资源类型个数; n = 进程个数 Available[0..m-1], 可利用资源向量 Available[i] the number of resources of Ri Allocation[0..n-1][0..m-1], n×m矩阵,分配矩阵 Request [0..n-1][0..m-1], n×m矩阵,请求矩阵 The current request of each process Request [i][j] = k Pi is requesting k more instances of Rj

Detection Algorithm:检测算法 Work[0..m-1] & Finish[0..n-1], initialize Work := Available For i = 0,1, …, n, if Allocation[i]  0, then Finish[i] := false; otherwise, Finish[i] := true //请求&保持 Find an index i such that both Finish[i] = false Request[i]  Work //请求可以得到满足 If no such i exists, go to step 4 Work := Work + Allocation[i] Finish[i] := true go to step 2 If for some i, 1  i  n, Finish[i] = false, then the system is in deadlock state. Moreover, if Finish[i] = false, then Pi is deadlocked

Complexity O(m x n2)

Example of Detection Algorithm P={P0, …, P4}; R={A(7), B(2), C(6)} Snapshot at time T0: Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i. Allocation Request Available A B C P0 0 1 0 0 0 0 P1 2 0 0 2 0 2 P2 3 0 3 P3 2 1 1 1 0 0 P4 0 0 2

Example (Cont.) P2 requests an additional instance of type C State of system? 可以回收 P0当前拥有的资源, 但无法满足其他进程需求. Deadlock exists, consisting of processes P1, P2, P3, and P4 Allocation Request Available A B C P0 0 1 0 0 0 0 P1 2 0 0 2 0 2 P2 3 0 3 0 0 1 P3 2 1 1 1 0 0 P4 0 0 2

Detection-Algorithm Usage 什么时候,什么频率调用检测算法 How often a deadlock is likely to occur? How many processes will need to be rolled back? one for each disjoint cycle If detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph and so we would not be able to tell which of the many deadlocked processes “caused” the deadlock.

Recovery from Deadlock Process Termination Abort all deadlocked processes Abort one process at a time until the deadlock cycle is eliminated. In which order should we choose to abort? Priority of the process How long process has computed, and how much longer to completion Resources the process has used Resources process needs to complete How many processes will need to be terminated Is process interactive or batch?

Recovery from Deadlock Resource Preemption Selecting a victim Minimize cost Rollback return to some safe state, restart process from that state Starvation Same process may always be picked as victim, include number of rollback in cost factor

Combined Approach to Deadlock Handling Combine the three basic approaches Prevention Avoidance Detection allowing the use of the optimal approach for each of resources in the system Partition resources into hierarchically ordered classes Use most appropriate technique for handling deadlocks within each class

作业 什么是死锁?产生死锁的原因是什么? 死锁产生的必要条件是什么? 处理死锁的方法有哪些? Ppt中作业