版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Chapter8:DeadlocksSystemModel(系统模型)DeadlockCharacterization(死锁特征)MethodsforHandlingDeadlocks(处理死锁的方法)DeadlockPrevention(预防死锁)DeadlockAvoidance(死锁避免)DeadlockDetection(死锁检测)RecoveryfromDeadlock(死锁恢复)CombinedApproachtoDeadlockHandling(综合处理方法)Deadlock资源是有限的,对资源的需求可能是无限的当占有了部分资源而渴求更多的资源的时候,可能会引起deadlock(死锁)OS管理着、分配着计算机系统的资源,必须考虑死锁TheDeadlockProblemAsetofblockedprocesseseachholdingaresourceandwaitingtoacquirearesourceheldbyanotherprocessintheset.ExampleSystemhas2tapedrives.P1andP2eachholdonetapedriveandeachneedsanotherone.ExamplesemaphoresAandB,initializedto1
P0
P1wait(A); wait(B)wait(B); wait(A)BridgeCrossingExampleTrafficonlyinonedirection.Eachsectionofabridgecanbeviewedasaresource.Ifadeadlockoccurs,itcanberesolvedifonecarbacksup(preemptresourcesandrollback).Severalcarsmayhavetobebackedupifadeadlockoccurs.Starvationispossible.SystemModelResourcetypesR1,R2,...,RmCPUcycles,memoryspace,I/OdevicesEachresourcetype
RihasWiinstances.cpu,一个存储,一个硬盘(多个分区)、U盘、光盘……打印机,一个……Eachprocessutilizesaresourceasfollows:requestusereleaseDeadlockCharacterizationMutualexclusion(互斥):onlyoneprocessatatimecanusearesource.Holdandwait(占有和等待):aprocessholdingatleastoneresourceiswaitingtoacquireadditionalresourcesheldbyotherprocesses.Nopreemption(非剥夺):aresourcecanbereleasedonlyvoluntarilybytheprocessholdingit,afterthatprocesshascompleteditstask.Circularwait(循环等待):thereexistsaset{P0,P1,…,P0}ofwaitingprocessessuchthatP0iswaitingforaresourcethatisheldbyP1,P1iswaitingforaresourcethatisheldbyP2,…,Pn–1iswaitingforaresourcethatisheldbyPn,andP0iswaitingforaresourcethatisheldbyP0.(有环)Deadlockcanariseiffourconditionsholdsimultaneously.Resource-AllocationGraphVispartitionedintotwotypes:P={P1,P2,…,
Pn},thesetconsistingofalltheprocessesinthesystem.
R={R1,R2,…,Rm},thesetconsistingofallresourcetypesinthesystem.requestedge–directededgeP1
Rjassignmentedge–directededgeRj
PiAsetofverticesVandasetofedgesE.Resource-AllocationGraph(Cont.)Process
ResourceTypewith4instancesPi
requestsinstanceofRjPiisholdinganinstanceofRjPiPiRjRjExampleofaResourceAllocationGraphResourceAllocationGraphWithADeadlockResourceAllocationGraphWithACycleButNoDeadlockBasicFactsIfgraphcontainsnocyclesnodeadlock.
Ifgraphcontainsacycleifonlyoneinstanceperresourcetype,thendeadlock.ifseveralinstancesperresourcetype,possibilityofdeadlock.MethodsforHandlingDeadlocksEnsurethatthesystemwillneverenteradeadlockstate.Topreventoravoid Allowthesystemtoenteradeadlockstateandthenrecover.
Ignoretheproblemandpretendthatdeadlocksneveroccurinthesystem;usedbymostoperatingsystems,includingUNIX.DeadlockPreventionMutualExclusion
–notrequiredforsharableresources;mustholdfornonsharableresources.
HoldandWait
–mustguaranteethatwheneveraprocessrequestsaresource,itdoesnotholdanyotherresources.Requireprocesstorequestandbeallocatedallitsresourcesbeforeitbeginsexecution,orallowprocesstorequestresourcesonlywhentheprocesshasnone.Lowresourceutilization;starvationpossible.Restrainthewaysrequestcanbemade.会发现:实现这样一个策略可能带来的问题比他所解决的问题更多!DeadlockPrevention(Cont.)NoPreemption
–Ifaprocessthatisholdingsomeresourcesrequestsanotherresourcethatcannotbeimmediatelyallocatedtoit,thenallresourcescurrentlybeingheldarereleased.Preemptedresourcesareaddedtothelistofresourcesforwhichtheprocessiswaiting.Processwillberestartedonlywhenitcanregainitsoldresources,aswellasthenewonesthatitisrequesting.
CircularWait
–imposeatotalorderingofallresourcetypes,andrequirethateachprocessrequestsresourcesinanincreasingorderofenumeration.DeadlockAvoidance死锁避免Simplestandmostusefulmodelrequiresthateachprocessdeclarethemaximumnumberofresourcesofeachtypethatitmayneed.
Thedeadlock-avoidancealgorithmdynamicallyexaminestheresource-allocationstatetoensurethattherecanneverbeacircular-waitcondition.
无环,安全Resource-allocationstateisdefinedbythenumberofavailableandallocatedresources,andthemaximumdemandsoftheprocesses.Requiresthatthesystemhassomeadditionalaprioriinformation
available.需要一些先验知识SafeStateWhenaprocessrequestsanavailableresource,systemmustdecideifimmediateallocationleavesthesysteminasafestate.
什么叫安全状态?Systemisinsafestateifthereexistsasafesequenceofallprocesses.
Sequence<P1,P2,…,Pn>issafeifforeachPi,theresourcesthatPicanstillrequestcanbesatisfiedbycurrentlyavailableresources+resourcesheldbyallthePj,withj<i.IfPiresourceneedsarenotimmediatelyavailable,thenPicanwaituntilall
Pj
havefinished.WhenPjisfinished,Picanobtainneededresources,execute,returnallocatedresources,andterminate.WhenPiterminates,Pi+1canobtainitsneededresources,andsoon.基于:进程执行完以后会释放它占用的资源;进程会在有限时间段内执行完;所以,安不安全就是针对当前资源和进程集,看能不能找到进程的安全执行序列BasicFactsIfasystemisinsafestatenodeadlocks.
Ifasystemisinunsafestatepossibilityofdeadlock.
Avoidanceensurethatasystemwillneverenteranunsafestate.死锁避免算法的目的就是让系统从不进入非安全的状态Safe,Unsafe,DeadlockState例子Resourceinstances,12Processes,3MaximumNeeds
currentNeedsP0 10 5P1 4 2P2 9 2死锁避免使系统处于安全状态进程需要申明他需要的资源最大数(不同资源)死锁避免算法要确保系统处于安全状态,即能不能找到进程安全执行序列死锁避免算法资源分配图,适合于每类资源只有一个实例银行家算法,适合于每类资源有多个实例Resource-AllocationGraphAlgorithmClaimedge
Pi
RjindicatedthatprocessPjmayrequestresourceRj;representedbyadashedline.
Claimedgeconvertstorequestedgewhenaprocessrequestsaresource.
Whenaresourceisreleasedbyaprocess,assignmentedgereconvertstoaclaimedge.
Resourcesmustbeclaimedapriori
inthesystem.Resource-AllocationGraphForDeadlockAvoidanceUnsafeStateInResource-AllocationGraphBanker’sAlgorithmMultipleinstances.
Eachprocessmustaprioriclaimmaximumuse.
Whenaprocessrequestsaresourceitmayhavetowait.
Whenaprocessgetsallitsresourcesitmustreturntheminafiniteamountoftime.SafetyalgorithmProcess’resource-requestalgorithmDataStructuresfortheBanker’sAlgorithmAvailable:Vectoroflengthm.Ifavailable[j]=k,therearekinstancesofresourcetypeRj
available.Max:nxmmatrix.IfMax[i,j]=k,thenprocessPi
mayrequestatmostkinstancesofresourcetypeRj.Allocation:nxmmatrix.IfAllocation[i,j]=kthenPiiscurrentlyallocatedkinstancesofRj.Need:nxmmatrix.IfNeed[i,j]=k,thenPimayneedkmoreinstancesofRj
tocompleteitstask.
Need[i,j]=Max[i,j]–
Allocation[i,j].Letn=numberofprocesses,andm=numberofresourcestypes.SafetyAlgorithm1. LetWork
andFinishbevectorsoflengthmandn,respectively.Initialize:Work=AvailableFinish[i]=falsefori=1,2,…,n.2. Findanisuchthatboth:(a)Finish[i]=false(b)
Needi
WorkIfnosuchiexists,gotostep4.3. Work=Work+
Allocationi
Finish[i]=true
gotostep2.4. IfFinish[i]==trueforalli,thenthesystemisinasafestate.Resource-RequestAlgorithmforProcessPi
Request=requestvectorforprocessPi.IfRequesti
[j]=kthenprocessPiwantskinstancesofresourcetypeRj.1. If
Requesti
Needi
gotostep2.Otherwise,raiseerrorcondition,sinceprocesshasexceededitsmaximumclaim.2. IfRequesti
Available,gotostep3.OtherwisePimustwait,sinceresourcesarenotavailable.3. PretendtoallocaterequestedresourcestoPibymodifyingthestateasfollows:
Available=Available-
Requesti;
Allocationi
=Allocationi+Requesti;
Needi
=Needi
–
Requesti;IfsafetheresourcesareallocatedtoPi.IfunsafePimustwait,andtheoldresource-allocationstateisrestoredExampleofBanker’sAlgorithm5processesP0throughP4;3resourcetypesA
(10instances),B(5
instances,andC(7instances).SnapshotattimeT0:
Allocation
Max
Available ABC ABC ABC
P0 010 753 332
P1 200 322
P2 302 902
P3 211 222
P4 002 433 ExampleofBanker’sAlgorithm
Allocation
Max
Available
Need
work
Finish[i] ABC ABC ABC
ABC
ABC
332
332
P0
010 753 743false
P1
200 322 122false
P2
302 902 600false
P3
211
222011false
P4
002 433 431
falseExampleofBanker’sAlgorithm
Allocation
Max
Available
Need
work
Finish[i] ABC ABC ABC
ABC
ABC
332
332
P0
010 753 743false
P1
200 322 122false
P2
302 902 600false
P3
211
222011false
P4
002 433 431
false≤ExampleofBanker’sAlgorithm
Allocation
Max
Available
Need
work
Finish[i] ABC ABC ABC
ABC
ABC
332
532
P0
010 753 743false
P1
200 322 122true
P2
302 902 600false
P3
211
222011false
P4
002 433 431
falseP1+ExampleofBanker’sAlgorithm
Allocation
Max
Available
Need
work
Finish[i] ABC ABC ABC
ABC
ABC
332
532
P0
010 753 743false
P1
200 322 122true
P2
302 902 600false
P3
211
222011false
P4
002 433 431
falseP1≤ExampleofBanker’sAlgorithm
Allocation
Max
Available
Need
work
Finish[i] ABC ABC ABC
ABC
ABC
332
743
P0
010 753 743false
P1
200 322 122true
P2
302 902 600false
P3
211
222011true
P4
002 433 431
falseP1、P3、+ExampleofBanker’sAlgorithm
Allocation
Max
Available
Need
work
Finish[i] ABC ABC ABC
ABC
ABC
332
743
P0
010 753 743false
P1
200 322 122true
P2
302 902 600false
P3
211
222011true
P4
002 433 431
falseP1、P3、≤ExampleofBanker’sAlgorithm
Allocation
Max
Available
Need
work
Finish[i] ABC ABC ABC
ABC
ABC
332
745
P0
010 753 743false
P1
200 322 122true
P2
302 902 600false
P3
211
222011true
P4
002 433 431
trueP1、P3、P4+ExampleofBanker’sAlgorithm
Allocation
Max
Available
Need
work
Finish[i] ABC ABC ABC
ABC
ABC
332
745
P0
010 753 743false
P1
200 322 122true
P2
302 902 600false
P3
211
222011true
P4
002 433 431
trueP1、P3、P4≤ExampleofBanker’sAlgorithm
Allocation
Max
Available
Need
work
Finish[i] ABC ABC ABC
ABC
ABC
332
1047
P0
010 753 743false
P1
200 322 122true
P2
302 902 600true
P3
211
222011true
P4
002 433 431
trueP1、P3、P4、P2
+ExampleofBanker’sAlgorithm
Allocation
Max
Available
Need
work
Finish[i] ABC ABC ABC
ABC
ABC
332
1047
P0
010 753 743false
P1
200 322 122true
P2
302 902 600true
P3
211
222011true
P4
002 433 431
trueP1、P3、P4、P2
≤ExampleofBanker’sAlgorithm
Allocation
Max
Available
Need
work
Finish[i] ABC ABC ABC
ABC
ABC
332
1057
P0
010 753 743true
P1
200 322 122true
P2
302 902 600true
P3
211
222011true
P4
002 433 431
trueP1、P3、P4、P2、P0
+Thesystemisinasafestatesincethesequence<P1,P3,P4,P2,P0>satisfiessafetycriteria.ExampleP1Request(1,0,2)CheckthatRequestAvailable(thatis,(1,0,2)(3,3,2)true.
Allocation
Need
Available ABC ABC ABC
P0 010 743 230
P1
302
020
P2 302 600
P3 211 011
P4 002 431Executingsafetyalgorithmshowsthatsequence<P1,P3,P4,P0,P2>satisfiessafetyrequirement.Canrequestfor(3,3,0)byP4begranted?Canrequestfor(0,2,0)byP0begranted?Example1DeadlockDetectionAllowsystemtoenterdeadlockstate
Detectionalgorithm
RecoveryschemeSingleInstanceofEachResourceTypeMaintainwait-forgraphNodesareprocesses.Pi
PjifPi
iswaitingforPj.
Periodicallyinvokeanalgorithmthatsearchesforacycleinthegraph.
Analgorithmtodetectacycleinagraphrequiresanorderofn2operations,wherenisthenumberofverticesinthegraph.Resource-AllocationGraphandWait-forGraphResource-AllocationGraphCorrespondingwait-forgraphSeveralInstancesofaResourceTypeAvailable:Avectoroflengthmindicatesthenumberofavailableresourcesofeachtype.
Allocation:Annxmmatrixdefinesthenumberofresourcesofeachtypecurrentlyallocatedtoeachprocess.
Request:Annxmmatrixindicatesthecurrentrequestofeachprocess.IfRequest[ij]=k,thenprocessPiisrequestingkmoreinstancesofresourcetypeRj.DetectionAlgorithm1. LetWorkandFinishbevectorsoflengthmandn,respectivelyInitialize:(a)Work=Available(b) Fori=1,2,…,n,if
Allocationi
0,then
Finish[i]=false;otherwise,Finish[i]=true.(没有分配资源的进程当然不用考虑)2. Findanindexisuchthatboth:(a) Finish[i]==false(b)
Requesti
Work
Ifnosuchiexists,gotostep4.DetectionAlgorithm(Cont.)3. Work=Work+Allocationi
(仍旧假定某个进程执行完以后会释放其拥有的资源)
Finish[i]=true
gotostep2.
4. IfFinish[i]==false,forsomei,1in,thenthesystemisindeadlockstate.Moreover,ifFinish[i]==false,thenPiisdeadlocked.
ExampleofDetectionAlgorithmFiveprocessesP0throughP4;
threeresourcetypes
A(7instances),B(2instances),andC(6instances).SnapshotattimeT0:
Allocation Request Available
ABC ABC ABC
P0 010 000 000
P1 200 202
P2 303 000
P3 211 100
P4 002 002ExampleofDetectionAlgorithm
Allocation
Request
Available
work
Fnish[i]
ABC ABC ABCABC
000000
P0 010 000 false
P1 200 202false
P2 303 000false
P3 211 100false
P4 002 002falseExampleofDetectionAlgorithm
Allocation
Request
Available
work
Fnish[i]
ABC ABC ABCABC
000000
P0 010 000 false
P1 200 202false
P2 303 000false
P3 211 100false
P4 002 002false≤ExampleofDetectionAlgorithm
Allocation
Request
Available
work
Fnish[i]
ABC ABC ABCABC
000010
P0 010 000 true
P1 200 202false
P2 303 000false
P3 211 100false
P4 002 002falseP0+ExampleofDetectionAlgorithm
Allocation
Request
Available
work
Fnish[i]
ABC ABC ABCABC
000010
P0 010 000 true
P1 200 202false
P2 303 000false
P3 211 100false
P4 002 002falseP0≤ExampleofDetectionAlgorithm
Allocation
Request
Available
work
Fnish[i]
ABC ABC ABCABC
000313
P0 010 000 true
P1 200 202false
P2 303 000true
P3 211 100false
P4 002 002falseP0、P2+ExampleofDetectionAlgorithm
Allocation
Request
Available
work
Fnish[i]
ABC ABC ABCABC
000313
P0 010 000 true
P1 200 202false
P2 303 000true
P3 211 100false
P4 002 002falseP0、P2≤ExampleofDetectionAlgorithm
Allocation
Request
Available
work
Fnish[i]
ABC ABC ABCABC
000524
P0 010 000 true
P1 200 202false
P2 303 000true
P3 211 100true
P4 002 002falseP0、P2、P3+ExampleofDetectionAlgorithm
Allocation
Request
Available
work
Fnish[i]
ABC ABC ABCABC
000524
P0 010 000 true
P1 200 202false
P2 303 000true
P3 211 100true
P4 002 002falseP0、P2、P3≤ExampleofDetectionAlgorithm
Allocation
Request
Available
work
Fnish[i]
ABC ABC ABCABC
000526
P0 010 000 true
P1 200 202false
P2 303 000true
P3 211 100true
P4 002 002trueP0、P2、P3、P4+ExampleofDetectionAlgorithm
Allocation
Request
Available
work
Fnish[i]
ABC ABC ABCABC
000526
P0 010 000 true
P1 200 202false
P2 303 000true
P3 211 100true
P4 002 002trueP0、P2、P3、P4≤ExampleofDetectionAlgorithm
Allocation
Request
Available
work
Fnish[i]
ABC ABC ABCABC
000726
P0 010 000 true
P1 200 202true
P2 303 000true
P3 211 100true
P4 002 002trueP0、P2、P3、P4、P1+Sequence<P0,P2,P3,P4,P1>willresultinFinish[i]=trueforalli.Example(Cont.)P2requestsanadditionalinstanceoftypeC.
Request ABC
P0 000
P1 201
P2 001
P3 100
P4 002Stateofsystem?CanreclaimresourcesheldbyprocessP0,butinsufficientresourcestofulfillotherprocesses;requests.Deadlockexists,consistingofprocessesP1,
P2,P3,andP4.Detection-AlgorithmUsageWhen,andhowoften,toinvokedependson:Howoftenadeadlockislikelytooccur?Howmanyprocesseswillneedtoberolledback?oneforeachdisjointcycle
Ifdetectionalgorithmisinvokedarbitrarily,theremaybemanycyclesintheresourcegraphandsowewouldnotbeabletotellwhichofthemanydeadlockedprocesses“caused”thedeadlock.
RecoveryfromDeadlock:ProcessTerminationAbortalldeadlockedprocesses.
Abortoneprocessatatimeuntilthedeadlockcycleiseliminated.
Inwhich
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026届安徽省定远县藕塘中学高二数学第一学期期末质量跟踪监视试题含解析
- (2025)客服知识库搭建与新人快速上手培训心得体会(3篇)
- 护士年终个人工作总结(15篇)
- 房屋转让合同 (合集15篇)
- 2026届安徽省安庆七中生物高一上期末统考模拟试题含解析
- 2026届江苏省南通市海安高级中学高三语文第一学期期末检测试题含解析
- 上海市虹口区复兴高级中学2026届高二上数学期末综合测试试题含解析
- 2026届河北省衡水市景县中学高二上数学期末经典模拟试题含解析
- 2026届上海市虹口区高一上数学期末学业质量监测模拟试题含解析
- 2026届上海市曹杨二中高二上生物期末经典模拟试题含解析
- 英语试卷河北省2026届高三第一次八省联考(T8联考)(12.24-12.25)
- 2025年中共赣州市赣县区委政法委下属事业单位面向全区选调工作人员备考题库有答案详解
- 2025年幼儿园后勤工作总结
- 知识点及2025秋期末测试卷(附答案)-浙美版(新教材)小学美术三年级上册
- 2025山西大地环境投资控股有限公司社会招聘116人备考笔试题库及答案解析
- 机器人手术术后引流管管理的最佳实践方案
- 2025年瓦检员考试题库及答案
- (新教材)2025年部编人教版一年级上册语文全册期末复习课件
- GB/T 26951-2025焊缝无损检测磁粉检测
- (2026年)压力性损伤的预防和护理课件
- 2025年消费者权益保护专员岗位招聘面试参考试题及参考答案
评论
0/150
提交评论