• Like
Operating Systems Slides 5 - Deadlock
Upcoming SlideShare
Loading in...5
×

Operating Systems Slides 5 - Deadlock

  • 2,583 views
Uploaded on

Handouts version: …

Handouts version:
http://cs2.swfu.edu.cn/~wx672/lecture_notes/os/slides/deadlock-a.pdf

More in: Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
2,583
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
73
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. DeadlocksWang XiaolinJune 19, 2013u wx672ster+os@gmail.com1 / 1
  • 2. A Major Class of Deadlocks Involve ResourcesProcesses need access to resources in reasonable order.Examples of computer resources........▶ printers▶ memory space▶ data (e.g. a locked record in a DB)▶ semaphores.Suppose...........▶ a process holds resource A and requests resource B. Atsame time,▶ another process holds B and requests ABoth are blocked and remain so2 / 1
  • 3. Resourcestypedef int semaphore;semaphore resource 1; semaphore resource 1;semaphore resource 2; semaphore resource 2;void process A(void) { void process A(void) {down(&resource 1); down(&resource 1);down(&resource 2); down(&resource 2);use both resources( ); use both resources( );up(&resource 2); up(&resource 2);up(&resource 1); up(&resource 1);} }void process B(void) { void process B(void) {down(&resource 1); down(&resource 2);down(&resource 2); down(&resource 1);use both resources( ); use both resources( );up(&resource 2); up(&resource 1);up(&resource 1); up(&resource 2);} }Deadlock!3 / 1
  • 4. Resources.Deadlocks occur when ...........processes are granted exclusive access to resourcese.g. devices, data records, files, ...Preemptable resources can be taken away from a processwith no ill effectse.g. memoryNonpreemptable resources will cause the process to fail iftaken awaye.g. CD recorderIn general, deadlocks involve nonpreemptable resources4 / 1
  • 5. Resources.Sequence of events required to use a resource........request « use « releaseopen() close()allocate() free().What if request is denied?........Requesting process▶ may be blocked▶ may fail with error code5 / 1
  • 6. The Best Illustration of a Deadlock.A law passed by the Kansas legislature early inthe 20th century........“When two trains approach each other at a crossing, bothshall come to a full stop and neither shall start up againuntil the other has gone.”6 / 1
  • 7. Introduction to Deadlocks.Deadlock........A set of processes is deadlocked if each process in the set iswaiting for an event that only another process in the set cancause.▶ Usually the event is release of a currently held resource▶ None of the processes can ...▶ run▶ release resources▶ be awakened7 / 1
  • 8. Four Conditions For DeadlocksMutual exclusion condition each resource can only beassigned to one process or is availableHold and wait condition process holding resources canrequest additionalNo preemption condition previously granted resourcescannot forcibly taken awayCircular wait condition▶ must be a circular chain of 2 or moreprocesses▶ each is waiting for resource held by nextmember of the chain8 / 1
  • 9. Four Conditions For Deadlocks.Unlocking a deadlock is to answer 4 questions:........1. Can a resource be assigned to more than one process atonce?2. Can a process hold a resource and ask for another?3. can resources be preempted?4. Can circular waits exits?9 / 1
  • 10. Resource-Allocation Graph(a) (b) (c)T UDCSBARFig. 3-3. Resource allocation graphs. (a) Holding a resource.(b) Requesting a resource. (c) Deadlock.has wants.Strategies for dealing with Deadlocks........1. detection and recovery2. dynamic avoidance — careful resource allocation3. prevention — negating one of the four necessaryconditions4. just ignore the problem altogether10 / 1
  • 11. Resource-Allocation Graph11 / 1
  • 12. Resource-Allocation Graph.Basic facts:........▶ No cycles « no deadlock▶ If graph contains a cycle «▶ if only one instance per resourcetype, then deadlock▶ if several instances per resourcetype, possibility of deadlock12 / 1
  • 13. Deadlock Detection▶ Allow system to enter deadlock state▶ Detection algorithm▶ Recovery scheme13 / 1
  • 14. Deadlock DetectionSingle Instance of Each Resource Type — Wait-for Graph▶ Pi → Pj if Pi is waiting for Pj;▶ Periodically invoke an algorithm that searches for acycle in the graph. If there is a cycle, there exists adeadlock.▶ An algorithm to detect a cycle in a graph requires anorder of n2operations, where n is the number ofvertices in the graph.14 / 1
  • 15. Deadlock DetectionSeveral Instances of a Resource TypeTapedrivesPlottersScannersCDRomsE = ( 4 2 3 1 )TapedrivesPlottersScannersCDRomsA = ( 2 1 0 0 )Current allocation matrix020001102010Request matrix212001010100R =C =Fig. 3-7. An example for the deadlock detection algorithm.Row n:C: current allocation to process nR: current requirement of process nColumn m:C: current allocation of resource class mR: current requirement of resource class m15 / 1
  • 16. Deadlock DetectionSeveral Instances of a Resource TypeResources in existence(E1, E2, E3, …, Em)Current allocation matrixC11C21Cn1C12C22Cn2C13C23Cn3C1mC2mCnmRow n is current allocationto process nResources available(A1, A2, A3, …, Am)Request matrixR11R21Rn1R12R22Rn2R13R23Rn3R1mR2mRnmRow 2 is what process 2 needsFig. 3-6. The four data structures needed by the deadlock detectionalgorithm.n∑i=1Cij + Aj = Eje.g.(C13 + C23 + . . . + Cn3) + A3 = E316 / 1
  • 17. Deadlock DetectionSeveral Instances of a Resource Type.Maths recall: vectors comparison........For two vectors, X and YX ≤ Y iff Xi ≤ Yi for 0 ≤ i ≤ me.g. [1 2 3 4]≤[2 3 4 4][1 2 3 4]≰[2 3 2 4]17 / 1
  • 18. Deadlock DetectionSeveral Instances of a Resource TypeTapedrivesPlottersScannersCDRomsE = ( 4 2 3 1 )TapedrivesPlottersScannersCDRomsA = ( 2 1 0 0 )Current allocation matrix020001102010Request matrix212001010100R =C =18 / 1
  • 19. Deadlock DetectionSeveral Instances of a Resource TypeTapedrivesPlottersScannersCDRomsE = ( 4 2 3 1 )TapedrivesPlottersScannersCDRomsA = ( 2 1 0 0 )Current allocation matrix020001102010Request matrix212001010100R =C =..A R.(2 1 0 0) ≥ R3, (2 1 0 0)
  • 20. Deadlock DetectionSeveral Instances of a Resource TypeTapedrivesPlottersScannersCDRomsE = ( 4 2 3 1 )TapedrivesPlottersScannersCDRomsA = ( 2 1 0 0 )Current allocation matrix020001102010Request matrix212001010100R =C =..A R.(2 1 0 0) ≥ R3, (2 1 0 0).(2 2 2 0) ≥ R2, (1 0 1 0)
  • 21. Deadlock DetectionSeveral Instances of a Resource TypeTapedrivesPlottersScannersCDRomsE = ( 4 2 3 1 )TapedrivesPlottersScannersCDRomsA = ( 2 1 0 0 )Current allocation matrix020001102010Request matrix212001010100R =C =..A R.(2 1 0 0) ≥ R3, (2 1 0 0).(2 2 2 0) ≥ R2, (1 0 1 0).(4 2 2 1) ≥ R1, (2 0 0 1)18 / 1
  • 22. Recovery From Deadlock▶ Recovery through preemption▶ take a resource from some other process▶ depends on nature of the resource▶ Recovery through rollback▶ checkpoint a process periodically▶ use this saved state▶ restart the process if it is found deadlocked▶ Recovery through killing processes19 / 1
  • 23. Deadlock AvoidanceResource TrajectoriesPlotterPrinterPrinterPlotterBAu (Both processesfinished)p qrstI8I7I6I5I4I3I2I1;;;;;;;deadzoneUnsafe region▶ B is requesting a resource at point t. The system mustdecide whether to grant it or not.▶ Deadlock is unavoidable if you get into unsafe region.20 / 1
  • 24. Deadlock AvoidanceSafe and Unsafe StatesAssuming E = 10,.Unsafe........ABC322947Free: 3(a)ABC422947Free: 2(b)ABC44 —4297Free: 0(c)ABC4—297Free: 4(d)Has Max Has Max Has Max Has MaxFig. 3-10. Demonstration that the state in (b) is not safe.unsafe.Safe........ABC322947Free: 3(a)ABC342947Free: 1(b)ABC30 ––297Free: 5(c)ABC30797Free: 0(d)–ABC3009–Free: 7(e)Has Max Has Max Has Max Has Max Has Max21 / 1
  • 25. Deadlock AvoidanceThe Banker’s Algorithm for a Single ResourceThe banker’s algorithm considers each request as it occurs,and sees if granting it leads to a safe state.ABCD00006Has Max547Free: 10ABCD11246Has Max547Free: 2ABCD12246Has Max547Free: 1(a) (b) (c)Fig. 3-11. Three resource allocation states: (a) Safe. (b) Safe.(c) Unsafe.unsafe!unsafe ̸= deadlock22 / 1
  • 26. Deadlock AvoidanceThe Banker’s Algorithm for Multiple ResourcesProcessTapedrivesPlottersABCDE30110011101010010010E = (6342)P = (5322)A = (1020)Resources assigned ProcessTapedrivesPlottersABCDE10302111010101102000Resources still neededScannersCDROMsScannersCDROMsFig. 3-12. The banker’s algorithm with multiple resources.D → A → B, C, ED → E → A → B, C23 / 1
  • 27. Deadlock AvoidanceMission Impossible.In practice........▶ processes rarely know in advance their max futureresource needs;▶ the number of processes is not fixed;▶ the number of available resources is not fixed.Conclusion: Deadlock avoidance is essentially a missionimpossible.24 / 1
  • 28. Deadlock Prevention— Break The Four Conditions.Attacking the Mutual Exclusion Condition........▶ For example, using a printing daemon to avoid exclusiveaccess to a printer.▶ Not always possible▶ Not required for sharable resources;▶ must hold for nonsharable resources.▶ The best we can do is to avoid mutual exclusion as muchas possible▶ Avoid assigning a resource when not really necessary▶ Try to make sure as few processes as possible mayactually claim the resource25 / 1
  • 29. .Attacking the Hold and Wait Condition........Must guarantee that whenever a process requests aresource, it does not hold any other resources.Try: the processes must request all their resourcesbefore starting executionif everything is availablethen can runif one or more resources are busythen nothing will be allocated (just wait)Problem: ▶ many processes don’t know what they willneed before running▶ Low resource utilization; starvation possible26 / 1
  • 30. .Attacking the No Preemption Condition........if a process that is holding some resources requestsanother resource that cannot be immediately allocatedto itthen 1. All resources currently being held are released2. Preempted resources are added to the list of resources forwhich the process is waiting3. Process will be restarted only when it can regain its oldresources, as well as the new ones that it is requestingLow resource utilization; starvation possible27 / 1
  • 31. .Attacking Circular Wait Condition........Impose a total ordering of all resource types, and requirethat each process requests resources in an increasing orderof enumerationA1. Imagesetter2. Scanner3. Plotter4. Tape drive5. CD Rom driveiBj(a) (b)Fig. 3-13. (a) Numerically ordered resources. (b) A resourcegraph.It’s hard to find an ordering that satisfies everyone28 / 1
  • 32. The Ostrich Algorithm▶ Pretend there is no problem▶ Reasonable if▶ deadlocks occur very rarely▶ cost of prevention is high▶ UNIX and Windows takes thisapproach▶ It is a trade off between▶ convenience▶ correctness29 / 1