中科大操作系统原理与实现课件7_deadlock.pdf
《中科大操作系统原理与实现课件7_deadlock.pdf》由会员分享,可在线阅读,更多相关《中科大操作系统原理与实现课件7_deadlock.pdf(50页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、.操作系统原理与设计第7章 Deadlocks(死锁)陈香兰中国科学技术大学计算机学院2009年11月05日.提纲Background and System ModelDeadlock CharacterizationNecessary ConditionsResource-Allocation GraphMethods for Handling DeadlocksDeadlock PreventionDeadlock AvoidanceSafe StateResource-Allocation Graph SchemeBankers AlgorithmDeadlock Detection小结
2、和作业.OutlineBackground and System ModelDeadlock CharacterizationNecessary ConditionsResource-Allocation GraphMethods for Handling DeadlocksDeadlock PreventionDeadlock AvoidanceSafe StateResource-Allocation Graph SchemeBankers AlgorithmDeadlock Detection小结和作业.The Deadlock Problemdeadlock situationA se
3、t of blocked processes each holding a resource and waiting toacquire a resource held by another process in the set.ExampleISystem has 2 disk drives.IP1and P2each hold one disk drive and each needs another one.ExampleIsemaphores A and B,initialized to 1P0P1wait(A);wait(B)wait(B);wait(A).Bridge Crossi
4、ng ExampleITraffic only in one direction.IEach section of a bridge can be viewed as a resource.IIf a deadlock occurs,it can be resolved if one car backs up(preempt resources and rollback).ISeveral cars may have to be backed up if a deadlock occurs.IStarvation is possible.System ModelIResource types
5、R1,R2,.,RmICPU cycles,memory space,I/O devicesIEach resource type Rihas Wiinstances.IEach process utilizes a resource as follows:IrequestIuseIrelease.OutlineBackground and System ModelDeadlock CharacterizationNecessary ConditionsResource-Allocation GraphMethods for Handling DeadlocksDeadlock Prevent
6、ionDeadlock AvoidanceSafe StateResource-Allocation Graph SchemeBankers AlgorithmDeadlock Detection小结和作业.Deadlock Characterization:Necessary ConditionsIDeadlock can arise if four conditions hold simultaneously.IMutual exclusion:only one process at a time can use aresource.IHold and wait:a process hol
7、ding at least one resource iswaiting to acquire additional resources held by other processes.INo preemption:a resource can be released only voluntarily bythe process holding it,after that process has completed itstask.ICircular wait:there exists a set P0,P1,.,P0 of waitingprocesses such that P0is wa
8、iting for a resource that is held byP1,P1is waiting for a resource that is held by P2,.,Pn1iswaiting for a resource that is held by Pn,and Pnis waiting fora resource that is held by P0.Resource-Allocation Graph.IA set of vertices V and a set of edges E.IV is partitioned into two types:IP=P1,P2,.,Pn,
9、the set consisting of all the processesin the system.IR=R1,R2,.,Rm,the set consisting of all resource typesin the system.Irequest edge directed edge Pi RjIassignment edge directed edge RjPiIProcessIResource Type with 4 instancesIPirequests instance of RjIPiis holding an instance of Rj.Example of a R
10、esource Allocation Graph.Resource Allocation Graph With A Deadlock.Graph With A Cycle But No Deadlock.Basic FactsIIf graph contains no cycles no deadlock.IIf graph contains a cycle Iif only one instance per resource type,then deadlock.Iif several instances per resource type,possibility of deadlock.M
11、ethods for Handling DeadlocksIEnsure that the system will never enter a deadlock state.IAllow the system to enter a deadlock state and then recover.IIgnore the problem and pretend that deadlocks never occur inthe system;used by most operating systems,including UNIX.OutlineBackground and System Model
12、Deadlock CharacterizationNecessary ConditionsResource-Allocation GraphMethods for Handling DeadlocksDeadlock PreventionDeadlock AvoidanceSafe StateResource-Allocation Graph SchemeBankers AlgorithmDeadlock Detection小结和作业.Deadlock Prevention IRestrain the ways request can be made.IMutual ExclusionInot
13、 required for sharable resources;must hold fornonsharable resources.IHold and WaitImust guarantee that whenever a process requests a resource,itdoes not hold any other resources.1.Require process to request and be allocated all its resourcesbefore it begins execution,or2.allow process to request res
14、ources only when the process hasnone.ILow resource utilization;starvation possible.Deadlock Prevention IIINo Preemption1.If a process that is holding some resources requests anotherresource that cannot be immediately allocated to it,then allresources currently being held are released.IPreempted reso
15、urces are added to the list of resources forwhich the process is waiting.IProcess will be restarted only when it can regain its oldresources,as well as the new ones that it is requesting.2.preempt the desired resources from the waiting process andallocate them to the requesting processIif the resour
16、ce are neither available nor held by a waitingprocess,the requesing process must wait.Deadlock Prevention IIIICircular WaitIimpose a total ordering of all resource types,and require thateach process requests resources in an increasing order ofenumeration.1.always in an increasing order2.may release
17、some higher ordered resource before requestinglower ordered resource.OutlineBackground and System ModelDeadlock CharacterizationNecessary ConditionsResource-Allocation GraphMethods for Handling DeadlocksDeadlock PreventionDeadlock AvoidanceSafe StateResource-Allocation Graph SchemeBankers AlgorithmD
18、eadlock Detection小结和作业.Deadlock AvoidanceIRequires that the system has some additional a prioriinformation available.ISimplest and most useful model requires that each processdeclare the maximum number of resources of each type that itmay need.IThe deadlock-avoidance algorithm dynamically examines t
19、heresource-allocation state to ensure that there can never be acircular-wait condition.IResource-allocation state is defined by the number ofavailable and allocated resources,and the maximumdemands of the processes.Safe State.IWhen a process requests an available resource,system mustdecide if immedi
20、ate allocation leaves the system in a safestate.ISystem is in safe state if there exists a sequence of ALL the processes in the systems such that foreach Pi,the resources that Pican still request can be satisfiedby currently available resources+resources held by all the Pj,with j i.IThat is:IIf Pire
21、source needs are not immediately available,then Picanwait until all Pjhave finished.IWhen Pjis finished,Pican obtain needed resources,execute,return allocated resources,and terminate.IWhen Piterminates,Pi+1can obtain its needed resources,andso on.Basic Facts:Safe,Unsafe,Deadlock State IIIf a system
22、is in safe state no deadlocks.IIf a system is in unsafe state possibility of deadlock.IAvoidance ensure that a system will never enter an unsafestate.Basic Facts:Safe,Unsafe,Deadlock State IIIExample,12 tape drives and 3 processes,at T0MaxNeedscurrentP0105P142P292IIif at T2,P2request and is allocate
23、d one more tape drive,?.Avoidance algorithmsISingle instance of a resource type.IUse a resource-allocation graphIMultiple instances of a resource type.IUse the bankers algorithm.Resource-Allocation Graph SchemeIClaim edge(需求边)PiRjindicated that process Pjmayrequest resource Rj;represented by a dashe
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 中科大 操作系统 原理 实现 课件 _deadlock
限制150内