chapter 7 102 ii
TRANSCRIPT
-
8/2/2019 Chapter 7 102 II
1/50
-
8/2/2019 Chapter 7 102 II
2/50
The Deadlock Problem
System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
-
8/2/2019 Chapter 7 102 II
3/50
To develop a description of deadlocks, which
prevent sets of concurrent processes from
completing their tasks
To present a number of different methods for
preventing or avoiding deadlocks in a computer
system.
-
8/2/2019 Chapter 7 102 II
4/50
Suppose system has 2 tape drives.
P1 and P2 each hold one tape drive and each needs
another one.
Deadlock
Occurs
Process 1 Process 2
Tape Drive 1 Tape Drive 2
holds holds
-
8/2/2019 Chapter 7 102 II
5/50
I need quad
A and B
I need quad
B and C
I need quad
C and B
I need quad
D and A
-
8/2/2019 Chapter 7 102 II
6/50
HALT until B
is free
HALT until C
is free
HALT until D
is free
HALT until A
is free
Deadlock Occurs
-
8/2/2019 Chapter 7 102 II
7/50
Deadlock can be defined formally as follows:
A set of processes is deadlocked if
each process in the set is
waiting for an event that only
another process in the set can
cause
-
8/2/2019 Chapter 7 102 II
8/50
8
A process must request a resource before using it,
and must release the resource after using it.
A process may request as many resources as itrequires to carry out its designated task
The number of resources requested may not
exceed the total number of resources available in
the system
E.g a process cannot request three printers if the system
only has two)
-
8/2/2019 Chapter 7 102 II
9/50
SPRING 2011 - 102
9
Conditionsfor Resource
Deadlocks
Mutualexclusion
condition
Hold and
wait
condition
No
preemption
condition.
Circular wait
condition.
-
8/2/2019 Chapter 7 102 II
10/50
SPRING 2011 - 102
10
Only one process can have exclusive control of a shared
resource and if any other process request that veryparticular resource then it has to wait till the time the
resource is released
Mutualexclusioncondition
Hold andwait
condition
A process holding one resource and waiting to acquire
another resource that are currently held by other process
1st Condition
2nd Condition
-
8/2/2019 Chapter 7 102 II
11/50
11
A circular chain of processes exist
in which each process holds
number of resources which are
requested by the next process in
the chain
Resources cannot be preempted; A resource can be
released only voluntarily by the process holding it, after
that process has completed its task.
Nopreemption
condition.
Circular waitcondition.
3rd Condition
4rth Condition
-
8/2/2019 Chapter 7 102 II
12/50
V is partitioned into two types:
P= {P1, P2, , Pn}, the set consisting of
all the processes in the system.
R = {R1, R2, , Rm}, the set consisting ofall resource types in the system.
request edgedirected edge P1 Rj
assignment edgedirected edge Rj Pi
-
8/2/2019 Chapter 7 102 II
13/50
Four conditions can be modeled by using directed graphs
The graphs have two type of nodes
Process (as circle)
Resources (as square)
Requesting a resource
Holding a resource
-
8/2/2019 Chapter 7 102 II
14/50
P1 is holding an instance
of resource R2
P1 is waiting for an
instance of a resource R1
P2 is holding an instance
of resource R1 & R2
P2 is waiting for an
instance of a resource R3
P3 is holding an instance
of resource R3
P3 is holding an instance
of resource R3
P3 is waiting for an
instance of resource R2
-
8/2/2019 Chapter 7 102 II
15/50
RESOURCE
ALLOCATION
GRAPH WITH A
CYCLE BUT NO
DEADLOCK
-
8/2/2019 Chapter 7 102 II
16/50
no deadlock No cycles
cycles
if only one instance
per resource type,
then deadlock
if several instances
per resource type,possibility of deadlock
-
8/2/2019 Chapter 7 102 II
17/50
SPRING 2011 - 10217
-
8/2/2019 Chapter 7 102 II
18/50
no deadlock No cycles
cycles
if only one instance
per resource type,
then deadlock
if several instances
per resource type,possibility of deadlock
-
8/2/2019 Chapter 7 102 II
19/50
SPRING 2011 - 10219
-
8/2/2019 Chapter 7 102 II
20/50
SPRING 2011 - 102
20
1st Strategy
2nd Strategy
4th Strategy
3rd Strategy
Just ignore the problem and pretend deadlock
never happened
Prevention, negating one of the four required
conditions
Dynamic avoidance by careful resourceallocation
Detection and recovery. Let deadlocks occur,detect them, take action.
Strategies for dealing with deadlocks:
-
8/2/2019 Chapter 7 102 II
21/50
Just ignore the problem
Reasonable if
deadlocks occur very rarely
cost of prevention is high
UNIX and Windows takes this approach
It is a trade off between
convenience
correctness
-
8/2/2019 Chapter 7 102 II
22/50
Deadlock prevention is the process of making it logically
impossible for one of the 4 deadlock conditions to hold.
Mutual exclusion
Hold and Wait
No preemption
Circular wait
Restrain the ways
resource allocation
requests can be made to
insure that at least one of
the four necessaryconditions is violated.
2nd strategy:
Deadlock
Prevention
-
8/2/2019 Chapter 7 102 II
23/50
not required for sharable resources;
must hold for non sharable resources.
Restrain the ways request can be made.
2nd strategy:
Deadlock
Prevention
Must be supported by the OS
-
8/2/2019 Chapter 7 102 II
24/50
2nd strategy:
Deadlock
Prevention
To ensure that the hold-and-wait neveroccurs in the system, we must guaranteethat, whenever a process requests aresource, it does not hold any otherresources.
-
8/2/2019 Chapter 7 102 II
25/50
2nd strategy:
Deadlock
Prevention
Require a process to request
and be allocated all its
resources before it beginsexecution
Allow a process to request
resources only when the
process has none.
2 Ways
1
2
-
8/2/2019 Chapter 7 102 II
26/50
Problems
No useful work possible with some resources while waiting for
others Also ties up resources other processes could be using
May not know required resources at start of run
Process could starve
A process that needs several popular resources may haveto wait indefinitely, because at least one of the resourcesthat it needs is always allocated to some other process
2nd strategy:
Deadlock
Prevention
-
8/2/2019 Chapter 7 102 II
27/50
2nd strategy:
Deadlock
Prevention
-
8/2/2019 Chapter 7 102 II
28/50
Preempted resources are added to the
list of resources for which the process iswaiting.
Process will be restarted only when it
can regain its old resources, as well as
the new ones that it is requesting.
2nd strategy:
Deadlock
Prevention
-
8/2/2019 Chapter 7 102 II
29/50
SPRING 2011 - 10229
-
8/2/2019 Chapter 7 102 II
30/50
This approach to the deadlock problemanticipates deadlock before it actually occurs. Thisapproach employs an algorithm to access thepossibility that deadlock could occur and actingaccordingly. This method differs from deadlockprevention, which guarantees that deadlockcannot occur by denying one of the necessaryconditions of deadlock.
If the necessary conditions for a deadlock are inplace, it is still possible to avoid deadlock by beingcareful when resources are allocated
SPRING 2011 - 102
30
2nd strategy:
Deadlock
Avoidance
-
8/2/2019 Chapter 7 102 II
31/50
Simplest and most useful model requires that each process declare themaximum number of resources of each type that it may need.
Avoid deadlock by acquiring additional information on how the
resources are to be requested.
For Example:
Process A uses resource 1 and 2 and then
releases them
Process B uses resource 2 and then 1.
3rd
strategy:
If system have this
information before handthan prior to resource
allocation a deadlock
avoidance algorithm can
determine that which
process should wait so
that a deadlock can beavoided in future
2nd strategy:
Deadlock
Avoidance
3rd
-
8/2/2019 Chapter 7 102 II
32/50
When a process requests an available resource, system must decide ifimmediate allocation leaves the system in a safe state.
System is in safe state if there exists a safe sequence of all processes.
Sequence is safe if for each Pi, the resources that Pi canstill request can be satisfied by currently available resources
If Pi resource needs are not immediately available, then Pican
wait until all Pjhave finished. When Pj is finished, Pi can obtain needed resources, execute,
return allocated resources, and terminate.
When Piterminates, Pi+1 can obtain its needed resources, and soon.
3
strategy:
Deadlock
Avoidance
2nd strategy:
Deadlock
Avoidance
-
8/2/2019 Chapter 7 102 II
33/50
Avoidance ensure that a system will never enter an unsafe state.
If a system is in
safe state no
deadlocks
If a system is in
unsafe state
possibility of
deadlock.
2nd strategy:
Deadlock
Avoidance
-
8/2/2019 Chapter 7 102 II
34/50
A state is known as SAFE state, if the system can allocate resources
to each process in SOME order and still avoid deadlock
A system is in Safe State only if there exists a Safe Sequence.
1
15
2
07
3
110
For example: Suppose
system with 10 resources
and following allocation
4
7
9
2nd strategy:
Deadlock
Avoidance
-
8/2/2019 Chapter 7 102 II
35/50
Bankers Algorithm Made by Dijkstra
Examines if an allocation leads to a safe or unsafe state
Each process must a priori claim maximum use.
When a process requests a resource it may have to wait.
When a process gets all its resources it must return them
in a finite amount of time.
2nd strategy:
Deadlock
Avoidance
-
8/2/2019 Chapter 7 102 II
36/50
2nd strategy:
Deadlock
Avoidance
Available:
Vector of length m. If available [j] = k, there are kinstances of resource type Rjavailable.
Max:
n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances of resource type
Rj.
Allocation
n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj.
Need
n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task
-
8/2/2019 Chapter 7 102 II
37/50
2nd strategy:
Deadlock
Avoidance
Need[i,j]= Max[i,j]Allocation [i,j].
Need Matrix can be calculated as
-
8/2/2019 Chapter 7 102 II
38/50
Allocation Max Available
A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
2nd strategy:
Deadlock
Avoidance
Snapshot at time T0:
-
8/2/2019 Chapter 7 102 II
39/50
The content of the matrix. Need is defined to be
Need = MaxAllocation.
Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
2nd strategy:
Deadlock
Avoidance
-
8/2/2019 Chapter 7 102 II
40/50
The system is in a safe state since the sequence
< P1, P3, P4, P2, P0> satisfies safety criteria.
2nd strategy:
Deadlock
Avoidance
-
8/2/2019 Chapter 7 102 II
41/50
Check that Request Available (that is, (1,0,2) (3,3,2) true.
Allocation Need Available
A B C A B C A B C
P0 0 1 0 7 4 3 2 3 0
P1 3 0 2 0 2 0
P2 3 0 1 6 0 0
P3 2 1 1 0 1 1
P4 0 0 2 4 3 1
Executing safety algorithm shows that sequence satisfies safety requirement.
Can request for (3,3,0) by P4 be granted?
Can request for (0,2,0) by P0 be granted?
2nd strategy:
Deadlock
Avoidance
-
8/2/2019 Chapter 7 102 II
42/50
SPRING 2011 - 10242
-
8/2/2019 Chapter 7 102 II
43/50
43
No limitation on Resource allocation
Use algorithm to detect possible deadlock
Resolve any deadlock found
When should algorithm be run?
When resource is claimed
Periodically, every n time unit
During idle periods
3rd strategy:
Deadlock
Detection
-
8/2/2019 Chapter 7 102 II
44/50
Make a resource graph
Determine the wait for graph
Locate the cycles
3rd strategy:
Deadlock
Avoidance
-
8/2/2019 Chapter 7 102 II
45/50
3rd strategy:
Deadlock
Avoidance
Resource-Allocation Graph
Corresponding wait-for graph
-
8/2/2019 Chapter 7 102 II
46/50
E is the existing resource vector
A is the available resource vector
C is the current allocation matrixCij means number of instance of each resource class
Pi currently holds
R is the request MatrixRij means the number of instance of resources j that
Pi wants
3rd strategy:
Deadlock
Detection
-
8/2/2019 Chapter 7 102 II
47/50
Deadlock detection algorithm:
1. Look for an unmarked process, Pi , for which the i-th row ofR is lessthan or equal toA.
2. If such a process is found, add the i-th row ofC to A, mark the
process, and go back to step 1.
3. If no such process exists, the algorithm terminates.
3rd strategy:
Deadlock
Detection
-
8/2/2019 Chapter 7 102 II
48/50
SPRING 2011 - 102
48
Algorithm Exercise : Is
there any Deadlock inthe system?
1
23
No Deadlock
3rd strategy:
Deadlock
Detection
-
8/2/2019 Chapter 7 102 II
49/50
49
Example
Process 1 request cant be fulfilled as CD Rom not available
Process 2 request cant be fulfilled as scanner is not free
Process 3 can run
A=(0 0 0 0 )
Process 3 will return its resources
A=(2 2 2 0)
Now Remaining Processes can run
3rd strategy:
Deadlock
Detection
-
8/2/2019 Chapter 7 102 II
50/50
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 processes
crudest but simplest way to break a deadlock
kill one of the processes in the deadlock cycle
the other processes get its resources
choose process that can be rerun from the beginning
4rth strategy:
Recovery