1.3k likes | 1.34k Views
Deadlocks. CNS 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.
E N D
Deadlocks CNS 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.
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.
Using Semaphores to Acquire and Use a Resource wait(&semaphore_1); use_resource_1; signal(&semaphore_1)
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)
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)
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
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
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?
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)
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
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
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.
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
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
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
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.
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.
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
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
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
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
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
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
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
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!
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
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
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
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
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
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
Modeling Deadlock with State Diagrams
In a set of processes P, any process pi in P would cause a transition from state sj, if * pi requests a resource (shown as ri) * pi is allocated a resource (shown as ai) * pi deallocates a resource (shown as di) a i r i s j d i
Suppose that at state sj, some process p2 is blocked because all of the transitions out of sj are caused by other processes. That is, process p2 can not do anything to force a change in state. a 3 r 4 s j d 1
If a process pi is blocked in state sj, And there is no series of state transitions which will lead to a state in which pi is unblocked, then pi is deadlocked. The state sj is called a deadlock state. a 3 r 4 s j d 3
A State Transition Model for One Process and Two Resources d process owns 1 resource process owns 2 resources a r r a s 0 s 4 s 1 s 3 s 2 d r – request a – allocate d - deallocate
A State Transition Model for Two Processes and Two Resources d0 d0 r0 a0 r0 a0 s 40 s 30 s 00 s 10 s 20 r1 r1 r1 r1 r1 d0 d0 r0 a0 r0 a0 s 01 s 41 s 11 s 31 s 21 a1 a1 a1 a1 d1 d1 d1 d0 d1 r0 r0 a0 s 02 s 12 s 32 s 22 r1 r1 r1 r1 d0 r0 r0 a0 s 03 s 13 s 33 s 23 a1 a1 d1 d1 r0 s 03 r0 – process 0 request a0 – process 0 allocate d0 – process 0 deallocate s 13
A State Transition Model for Two Processes and Two Resources d0 d0 r0 a0 r0 a0 s 40 s 30 s 00 s 10 s 20 r1 r1 r1 r1 r1 d0 d0 r0 a0 r0 a0 s 01 s 41 s 11 s 31 s 21 a1 a1 a1 a1 d1 d1 d1 d0 d1 r0 r0 a0 s 02 s 12 s 32 s 22 no resources held r1 r1 r1 r1 d0 one resource held r0 r0 a0 s 03 s 13 s 33 s 23 two resources held deadlock state a1 a1 d1 d1 r0 s 04 r0 – process 0 request a0 – process 0 allocate d0 – process 0 deallocate s 14
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.
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.
Reminder process resource The resource has been assigned to the process. The process is blocked, waiting for the resource. resource process
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
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.
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
B R A List L R D E T C S W U F G W
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