deadlocksdeadlocks cs 3060. imagine a narrow set of stairs, only wide enough for one person to pass....
Post on 21-Dec-2015
213 views
TRANSCRIPT
DeadlocksDeadlocks
CS 3060CS 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
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
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)
Process A Process B
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)
Process A Process B
1 10
Time slice expires…
semaphore 1 semaphore 2
Two Processes 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)
wait(&semaphore_2); wait(&semaphore_1); use_resource_1; use resource_2; signal(&semaphore_1); signal(&semaphore_2)
Process A Process B
10 0D E A D L
O C K !
semaphore 1 semaphore 2
Two Processes 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)
wait(&semaphore_2); wait(&semaphore_1); use_resource_1; use resource_2; signal(&semaphore_1); signal(&semaphore_2)
Process A Process B
Can you fix it so no deadlock occurs?
Two Processes 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)
wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1)
Process A Process B
Two Processes Using Semaphores to Acquire and Use Two Resources
Some Examples of Deadlock
Some Examples of Deadlock
Some Examples of DeadlockDeadlock 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
Some Examples of DeadlockDeadlock on a file request
Inventory Suppliers
Purchasing
Sales
Purchasing accesses thesupplier file to place anorder for lumber.
Sales accesses theinventory file to reserve parts to builda home ordered today.
Purchasing now tries toaccess the Inventory fileto verify the quantity of lumber on hand before placing the order
Sales tries to access the supplier file to check the schedule of a subcontractor.
Some Examples of DeadlockDeadlock on a print spooler
A print spooler accepts input from many users, but will notstart a print job until it has a complete print file.
spool
print job
print jo
b
The day that a big project is due everyonein class submits their print files at the sametime. The spooler accepts pages from everyoneso several page ones show up, then several page twos, and so on. Quickly the spool filefills. It cannot print anything because is hasno complete jobs.
Four Conditions for Deadlock
Four Conditions for Deadlock
1. 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.
3. 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.
4. 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.
Deadlock ModelingDeadlock Modeling
process
resource
The resource hasbeen assigned tothe process.
resource
process
The process isblocked, waitingfor the resource.
This is called a Resource Graph
Deadlock ModelingDeadlock Modelingprocess
resource resource
process
r1 r2
p1
p2
process p1 ownsresource r1
process p1 is blockedwaiting for resource r2
process p2 is blockedwaiting for resource r1
process p2 ownsresource 2
Circular Wait Condition!
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 resourcegraph representing the processes and resourcesin the machine.
The initial state, s0, represents the situationWhere 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 RRequest resource SDo some workRelease resource RRelease resource S
Process B
Request resource SRequest resource TDo some workRelease resource SRelease resource T
Process C
Request resource TRequest resource RDo some workRelease resource TRelease resource R
Assume a Round Robin Scheduling AlgorithmWith events occurring in the following order:
Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R
Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R
A B C
R S T
Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R
A B C
R S T
Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R
A B C
R S T
Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R
A B C
R S T
Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R
A B C
R S T
Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R
A B C
R S T
D E A D L O C K !
We know that the operating system is not bound torun processes in any particular order. In this case, ifthe operating system knew of the impending deadlock,it could have scheduled the work differently!
Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R
A B C
R S T
Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R
A B C
R S T
potential deadlock – block this call!
Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R
A B C
R S T
potential deadlock – block this call!
Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R
A B C
R S T
potential deadlock – block this call!
Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R
A B C
R S T
potential deadlock – block this call!
Blocked.
blocks
Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource RProcess A does its workProcess A releases resource RProcess A releases resource S
A B C
R S T
potential deadlock – block this call!
Blocked.
blocks
There is no deadlock ---Process C does its work and releases its resourcesProcess B is now unblocked
Deadlock StrategiesDeadlock 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 tocause deadlock.
The Ostrich AlgorithmThe Ostrich AlgorithmGood or Bad?
Many potential deadlock situations occur because almost allresources on a system are finite.
For example, The number of open files that a system can manageis 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.
How often will this situation occur?
Does it occur often enough that it is worth fixing?Fixing such problems is expensive and usually involvesplacing inconvenient restrictions in what a process can do.
ReminderReminder
process
resource
The resource hasbeen assigned tothe process.
resource
process
The process isblocked, waitingfor the resource.
Deadlock Detection Deadlock Detection Is there a deadlock in the drawing below?
A
F
C
R
S
W
D
B
E
G
T
U W
Cycles are pretty easy tofind with visual inspection.Can they be found algorithmically?
Resource graphs are an example of a directed graph.many algorithms exist to detect cycles in a directed graph.
Repeat the following steps for each node in the graph
1. Initialize a list to the empty list. Designate all arcs as unvisited
2. 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.
3. From the given node, see if there are any outgoing arcs. If so, go to step 4. Otherwise go to step 5.
4. Pick one of the outgoing nodes at random and mark it. Follow it to the new current node and go to step 2.
5. 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.
Deadlock Detection Deadlock Detection
Is there a deadlock in the drawing below?
A
F
C
R
S
W
D
B
E
G
T
U W
Start here andgo left to right,top to bottom
A
F
C
R
S
W
D
B
E
G
T
U W
List L
R
A
F
C
R
S
W
D
B
E
G
T
U W
List L
R
This is the onlyoutging arc, so A is the next node
A
x
A
F
C
R
S
W
D
B
E
G
T
U W
List L
R
This is the onlyoutging arc, so S is the next node
A
S
x
x
A
F
C
R
S
W
D
B
E
G
T
U W
List L
R
S has no outgoingArcs, so back-upTo A
A
x
x
A
F
C
R
S
W
D
B
E
G
T
U W
List L
R
A has no unmarked outgoing arcs, so back-up To R
x
x
A
F
C
R
S
W
D
B
E
G
T
U W
List L
R has no unmarked outgoing arcs, so we are done inspectingthis node.
x
x
A
F
C
R
S
W
D
B
E
G
T
U W
List L
Move to the next nodeand repeat the algorithm.
It should be pretty obviousthat this iteration stops quickly, finding no cycle.
A
F
C
R
S
W
D
B
E
G
T
U W
List L
Move to the next nodeand repeat the algorithm.
A
F
C
R
S
W
D
B
E
G
T
U W
List L
BxThis is the only
outgoing arcT
A
F
C
R
S
W
D
B
E
G
T
U W
List L
Bx This is the only
outgoing arcT
x
A
F
C
R
S
W
D
B
E
G
T
U W
List L
Until finally at this point we have …
x
x
x
x
x
x
B
T
E
W
G
U
D
A
F
C
R
S
W
D
B
E
G
T
U W
List L
If we go this way we hit adead end … there are nooutgoing arcs
x
x
x
x
x
x
B
T
E
W
G
U
D
A
F
C
R
S
W
D
B
E
G
T
U W
List L
So we back up and look atthe only unmarked arcfrom D.
x
x
x
x
x
x
B
T
E
W
G
U
D
x
A
F
C
R
S
W
D
B
E
G
T
U W
List L
At this point, T gets added to the list.When the list is inspected, we note that it isin the list twice. Therefore a cycle exists.
x
x
x
x
x
x
B
T
E
W
G
U
D
T
x
Deadlock ModelingDeadlock Modeling
process
resource
The resource hasbeen assigned tothe process.
resource
process
The process isblocked, waitingfor the resource.
One way to model “units” of a resource is like this …
This resource has 2 unitsthat can be assigned.
Graph Reduction
Given a directed resource graph
1. Find a process that is currently using a resource and not waiting for one. This process can be removed from the graph. This is because the process would eventually finish and its resources returns to the pool of available resources.
2. Find a process that’s only waiting for resource classes that aren’t fully allocated. This process isn’t contributing to deadlock since it will eventually get its resources and finish. Then its resources can be returned to the available pool.
3. Go back to step 1 until all possible connections have been removed.
If there are any connections between processes and resources leftWhen you are done, then the system will deadlock.
Deadlock Prevention
Note that Deadlock prevention is different from detection.
1. 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.3. 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.4. 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.
For deadlock to occur, all of the following conditions must hold.
Deadlock prevention schemes all try to make sure that at least oneof these conditions is not true.
Attacking the Mutual Exclusion Condition
If no resource were ever assigned exclusively to a singleprocess, then there would be no deadlock problems.
Is this reasonable?
How might you avoid mutual exclusion on a printer?
It is difficult to avoid mutual exclusion. However, you canavoid assigning a resource unless and until it isabsolutely necessary. Try to make sure that as fewprocesses as possible can lay claim to the resource.
Attacking the Hold and Wait Condition
1. Require a process to request all of its resources when it is first created.
This would be impossible for an interactive system. However, a batch system could do this … only add a job to the ready list when all of the resources it needs are available. But, is there a problem?Poor utilization of resources !
2. Require a process to give up all resources it has allocated to it, before it can request any new ones.
This is okay for interactive systems, but it forces a process to compete for all resources, even when it needs an incremental resource.
Attacking the No Preemption Condition
This is not very promising … it is difficult to take a resourceaway from a process until the process has finished with it.
Attacking the Circular Wait Condition
There are two possible approaches:
Only allow a process to have one resource at a time.
Provide a global numbering system for resources. A processmay have as many resources as it wants, but requests must bein numerical order. For example, a process may request a printerand then a plotter, but never a plotter and then a printer!
Deadlock Avoidance
Deadlock avoidance schemes control state transitions. They will only allow transitions due to allocations, when they can be certain that a deadlock cannot occur due to subsequent requests.
The Banker’s AlgorithmThe Banker’s AlgorithmThe Banker’s algorithm was developed by Dijkstra in 1965and provides a scheduling algorithm that can avoid deadlocks.It is modeled on the way a small town banker might deal witha group of customers to whom he has granted lines of credit.
If granting a request leads to a safe state, the request isgranted. If it does not, the request is denied. When arequest is denied the customer may come back laterto make the request again.
Dijkstra’s algorithm is elegant in concept, but is almost useless in practice.Why?
1. It requires a process to know in advance what its maximumresource needs will be.
2. It assumes that the number of available resources is constantso does not account for failures.
3. It assumes that the number of processes is constant so does not account for users logging in and out of a system.