1 / 66

Deadlocks

Deadlocks. CS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several people to stand. Wait. Deadlock. Preemptable and Non-preemptable Resources.

melina
Download Presentation

Deadlocks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Deadlocks CS 3060

  2. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several people to stand.

  3. Wait

  4. Deadlock

  5. Preemptable and Non-preemptable Resources A preemptable resource is one that can be temporarily taken way from the process owning it without any ill effects. A non-preemptable resource is one that cannot be taken away without causing the computation to fail.

  6. Using Semaphores to Acquire and Use a Resource wait(&semaphore_1); use_resource_1; signal(&semaphore_1)

  7. Using Semaphores to Acquire and Use Two Resources wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1)

  8. Two Processes Using Semaphores to Acquire and Use Two Resources Process B Process A wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1) wait(&semaphore_2); wait(&semaphore_1); use_resource_1; use resource_2; signal(&semaphore_1); signal(&semaphore_2)

  9. Two Processes Using Semaphores to Acquire and Use Two Resources Process B Process A wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1) wait(&semaphore_2); wait(&semaphore_1); use_resource_1; use resource_2; signal(&semaphore_1); signal(&semaphore_2) Time slice expires… 1 1 0 semaphore 2 semaphore 1

  10. Two Processes Using Semaphores to Acquire and Use Two Resources Process B Process A D E A D L O C K ! wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1) wait(&semaphore_2); wait(&semaphore_1); use_resource_1; use resource_2; signal(&semaphore_1); signal(&semaphore_2) 0 1 0 semaphore 2 semaphore 1

  11. Two Processes Using Semaphores to Acquire and Use Two Resources Process B Process A wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1) wait(&semaphore_2); wait(&semaphore_1); use_resource_1; use resource_2; signal(&semaphore_1); signal(&semaphore_2) Can you fix it so no deadlock occurs?

  12. Two Processes Using Semaphores to Acquire and Use Two Resources Process B Process A wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1) wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1)

  13. Some Examples of Deadlock

  14. Some Examples of Deadlock Deadlock on a file request A System built for contractors has two files (among others) - one for suppliers - one for inventory It also has two applications (among others) - a purchasing application that orders material - a sales application that creates a supply list

  15. Purchasing accesses the supplier file to place an order for lumber. Purchasing now tries to access the Inventory file to verify the quantity of lumber on hand before placing the order Sales accesses the inventory file to reserve parts to build a home ordered today. Sales tries to access the supplier file to check the schedule of a subcontractor. Some Examples of Deadlock Deadlock on a file request Purchasing Suppliers Inventory Sales

  16. Some Examples of Deadlock Deadlock on a print spooler A print spooler accepts input from many users, but will not start a print job until it has a complete print file. print job spool print job The day that a big project is due everyone in class submits their print files at the same time. The spooler accepts pages from everyone so several page ones show up, then several page twos, and so on. Quickly the spool file fills. It cannot print anything because is has no complete jobs.

  17. Mutual exclusion condition: Each resource is either being used • by exactly one process or it is available. • 2. Hold and wait condition: Processes currently holding resources • granted earlier can request additional new resources. • No preemption condition: Resources previously granted cannot • be taken away from the process. The process must release them • voluntarily. Once a process requests a resource that is not • available, it cannot withdraw its request. • Circular wait condition: There must be a circular chain of two or • more processes, each of which is waiting for a resource held by • the next member of the chain. Four Conditions for Deadlock

  18. Deadlock Modeling process resource The resource has been assigned to the process. The process is blocked, waiting for the resource. resource process This is called a Resource Graph

  19. process p1 Deadlock Modeling process p1 owns resource r1 process p1 is blocked waiting for resource r2 Circular Wait Condition! resource resource r2 r1 process p2 owns resource 2 process p2 is blocked waiting for resource r1 p2 process

  20. This Process-Resource model provides a “snapshot” of the relationships between processes and resources at some instant in time. We need a model that addresses the dynamics of the relationships … i.e. what happens over time.

  21. S is the set of states {si} where each state s is represented with a resource graph representing the processes and resources in the machine. The initial state, s0, represents the situation Where none of the resources have been allocated. In this model we are interested in state transitions. The pattern in which resources are requested, allocated, and released will determine whether or not the system is deadlocked.

  22. Process A Request resource R Request resource S Do some work Release resource R Release resource S Process B Request resource S Request resource T Do some work Release resource S Release resource T Process C Request resource T Request resource R Do some work Release resource T Release resource R Assume a Round Robin Scheduling Algorithm With events occurring in the following order: Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R

  23. A C B Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R R S T

  24. A C B Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R R S T

  25. A C B Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R R S T

  26. A C B Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R R S T

  27. A C B Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R R S T

  28. D E A D L O C K ! A C B Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R R S T

  29. We know that the operating system is not bound to run processes in any particular order. In this case, if the operating system knew of the impending deadlock, it could have scheduled the work differently!

  30. Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R A C B R S T

  31. Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R potential deadlock – block this call! A C B R S T

  32. Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R potential deadlock – block this call! A C B R S T

  33. Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R potential deadlock – block this call! A C B R S T

  34. Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R potential deadlock – block this call! A C B Blocked. R S T blocks

  35. Process A requests resource R Process B requests resource S Process C requests resource T Process A requests resource S Process B requests resource T Process C requests resource R Process A does its work Process A releases resource R Process A releases resource S potential deadlock – block this call! A C B Blocked. R S T blocks There is no deadlock --- Process C does its work and releases its resources Process B is now unblocked

  36. Deadlock Strategies The Ostrich Algorithm: Ignore the problem. If you ignore it, it may ignore you. Detection and Recovery: Let deadlocks occur, detect them and take action. Dynamic avoidance: OS is careful how it allocates resources Deadlock Prevention: Structurally negate one of the conditions necessary to cause deadlock.

  37. Good or Bad? Many potential deadlock situations occur because almost all resources on a system are finite. For example, The number of open files that a system can manage is limited by the size of the i-node table. Suppose that 100 processes all require 100 open files before they can do their work. After each has opened 99, the i-node table becomes full. Now, none of the processes can open another file and a deadlock occurs. The Ostrich Algorithm How often will this situation occur? Does it occur often enough that it is worth fixing? Fixing such problems is expensive and usually involves placing inconvenient restrictions in what a process can do.

  38. Reminder process resource The resource has been assigned to the process. The process is blocked, waiting for the resource. resource process

  39. Is there a deadlock in the drawing below? Deadlock Detection B R A D E T C S W U F Cycles are pretty easy to find with visual inspection. Can they be found algorithmically? G W

  40. Repeat the following steps for each node in the graph • Initialize a list to the empty list. Designate all arcs as unvisited • Add the current node to the list. Check to see if the node appears in the list more than once. If it does a cycle exists. • From the given node, see if there are any outgoing arcs. If so, go to step 4. Otherwise go to step 5. • Pick one of the outgoing nodes at random and mark it. Follow it to the new current node and go to step 2. • Dead end. Remove the node from the list and go back to the previous one and make it the current node. Go to step 3. If this node is the initial node, there are no cycles. Resource graphs are an example of a directed graph. many algorithms exist to detect cycles in a directed graph.

  41. Is there a deadlock in the drawing below? Deadlock Detection B R A Start here and go left to right, top to bottom D E T C S W U F G W

  42. B R A List L R D E T C S W U F G W

  43. This is the only outging arc, so A is the next node B x R A List L R D E T C S A W U F G W

  44. B x R A This is the only outging arc, so S is the next node x List L R D E T C S A W U S F G W

  45. B S has no outgoing Arcs, so back-up To A x R A x List L R D E T C S A W U F G W

  46. B A has no unmarked outgoing arcs, so back-up To R x R A x List L R D E T C S W U F G W

  47. R has no unmarked outgoing arcs, so we are done inspecting this node. B x R A x List L D E T C S W U F G W

  48. Move to the next node and repeat the algorithm. B R A List L D E T C S W U F It should be pretty obvious that this iteration stops quickly, finding no cycle. G W

  49. Move to the next node and repeat the algorithm. List L B R A D E T C S W U F G W

  50. B R A B This is the only outgoing arc x T D E T C S List L W U F G W

More Related