Deadlocks 0
Deadlocks 0
Introduction
In multitasking or multiprogramming system
Several processes can compete for finite number of resources
A process requests resources
If not available process placed in wait state
If resources never become available
Process remains waiting
Called deadlock
Illustration
Law passed in Kansas around turn of century
When two trains approach each other at crossing
Each shall come to full stop
Neither shall start up again until the other has gone
System Model
System has finite number of resources
These distributed among number of competing processes
Resources partitioned into several types
Identical resources
May have multiple instances of same resource
Printers
Telecomm channels
Memory space
Allocation of any one may be sufficient
Note printers may be identical
If convenience to user compromised
May not be considered identical
Printer on 1 and 9th floors of office building
Dissimilar resources
Second kind of resource
Those that are unique for one reason or another
Single copy
Identical printers for example may not be identical
If convenience to user compromised
May not be considered identical
Printer on 1 and 9th floors of office building
- 1 of 13 -
To use resource process must request resource
May request as many resources as it wishes
May not exceed total number
Deadlock Characterization
Set of processes in deadlock state
Every process in set is waiting for event
Can be caused only be another process in set
Events of concern
Resource acquisition and release
Necessary Conditions
For deadlock to occur following must hold simultaneously
Note these are necessary not sufficient
Will see shortly
Note also these are not independent
Mutual Exclusion
A least one resource held in non-sharable mode
No Preemption
Resources cannot be preempted
Circular Wait
Set of processes {P0...Pn} such that
P0 waiting for resource held by P1
P1 waiting for resource held by P2
...
- 2 of 13 -
Resource Allocation Graph
Can understand deadlock formally using
Resource Allocation Graph
Graphically R1 R2
Process
Circle
Resource
Rectangle P1 P2 P3
Multiple copies
Signified by dot in rectangle
R3 R4
Consider following RAG
We have the following situation
Sets
P = {P1, P2, P3}
R = {R1, R2, R3}
E = {P1 R1, P2 R2, R1P2, R2 P3, R3 P1, R3 P2}
Resource Instances
1 of R1
1 of R1
2 of R3
3 of R4
- 3 of 13 -
Process States
P1
Holding 1 R3
Waiting for R1
P2
Holding 1 R1 and 1 R3
Waiting for R2
P3
Holding 1 R2
P3 is the problem
Handling Deadlocks P1 P2
Let’s now look at some ways of dealing with
Deadlock problem
R3
Several ways P4
Use protocol to ensure deadlock will never happen
Allow system to enter deadlock state and recover
Ignore problem
Solution used by most operating systems
Including UNIX
- 4 of 13 -
Ensuring No Deadlock
Have two methods … can use
Deadlock prevention
Ensure one of necessary conditions cannot occur
Deadlock avoidance
Requires additional information
Which resources process will request and use
During lifetime
Deadlock Prevention
Let’s look at prevention first
Easiest solution
Mutual Exclusion
Must hold for non sharable resources
Single printer
Sharable resources
Mutual exclusion not required
Read only files
Cannot prevent deadlocks by denying mutual exclusion
Some resources inherently non-sharable
Protocol 2
Can only request resources when have none
Can request resources and be allocated
To request additional
Must give up what have
- 5 of 13 -
Two main disadvantages
Resource utilization low
Allocated but not used for long time
Starvation possible
Process needing popular resources may have to wait indefinitely
No Preemption
To prevent no preemption condition
Protocol 1
1. If holding resources and need more that are not available
Process must wait
2. All resources currently being held
Preempted
Added to list of resources for which process is waiting
3. Process restarted when it can
Regain old resources
Acquire new ones it requested
Protocol 2
Don’t preempt immediately
Circular Wait
To prevent circular wait
Place total ordering on all resources
Require each process to request resources
Increasing order of enumeration
Let R = {R1, R2, ...Rm} be set of resource types
Assign each type unique integer number
Allows ordering relation to be applied and evaluated
Can’t request lower order resource
- 6 of 13 -
Protocol 1
Initially request any desired resources
Additional resource requests
Only in increasing order of enumeration
If multiple copies of single resource needed
Must request all at once
Protocol 2
Initially request any desired resources
Additional resource requests
If request Rj
Must release any resources {Ri} such that i <= j
Deadlock Avoidance
Deadlock prevention algorithms
Loosely analogous to static schedule
Prevent deadlocks by restraining requests
Restraints ensure
At least one of necessary conditions cannot occur
Consequence
Low utilization of resources
Deadlock avoidance
Loosely analogous to dynamic schedule
Knowledge
Knowing need in advance
Permits scheduling to ensure no deadlock
Various algorithms
Require differing amounts of information
Let’s walk through simple one to get idea
- 7 of 13 -
Declare in Advance
Simplest most useful model requires each process to declare
In advance
Maximum number of resources
Of each type it may need
Given such information
Possible to construct algorithm
To ensure system will never enter deadlock state
Such a scheme defines basis for deadlock avoidance
Avoidance algorithm says
Examine resource allocation state – want a safe state
Defined by number of
Available and allocated resources
Max number of demands by processes
Safe State
Resource allocation state is safe
System can
Allocate resources to each process
In some order
Avoid a deadlock
Formally
System is in safe state
If there exists a safe sequence
Safe sequence of execution
Sequence of processes <P1, P2...Pn> is safe sequence
For current allocation state
If
For each Pi
Resources that Pi can still request can be satisfied by
Currently available resources plus
Resources held by all Pj such that j < i
i is earlier in the allocation
Observe
Safe state is not deadlock state Deadlock Unsafe
Safe
- 8 of 13 -
Not all unsafe states are deadlock states
Unsafe state may lead to deadlock
Three spaces illustrated as
Example
12 I/O Ports
3 Processes
Let max and current needs be given as
Max Current
Needs Needs
P0 10 5
P1 4 2
P2 9 2
- 9 of 13 -
At time t1
System can go to unsafe state
Let P2 requests additional port
Only P1 can be allocated all resources
When it returns them
Only 4 total available
P0 allocated 5 ports
Max need of 10
May request 5 more
Not available so block
Avoidance Algorithms
Resource Allocation Graph Algorithms
If we have system with one instance of each resource
Can use variant on resource-allocation graph to avoid deadlocks
Introduce new edge type - claim edge
Claim edge Pi Rj
Indicates Process Pi may claim resource Rj sometime in future
Edge has semantics similar to request edge
Direction same
Notation is dashed line
Observe P1
If P2 requests and is allocated R3 P2
Although available
R3
- 10 of 13 -
Cannot allocate
Will create cycle and thus unsafe state
If P1 requests R3
We have a deadlock
Deadlock Detection
If system does not employ prevention or avoidance algorithm
Deadlock may occur
In such environment
System must provide
Algorithm to determine if deadlock has occurred
Algorithm to recover from deadlock
P5
P5
R1 R3 R4 P1 P2 P3
P4
P1 P2 P3
R2 P4 R5
- 11 of 13 -
Deadlock Recovery
When deadlock algorithm detects deadlock exists
Several possible alternatives
Inform user
Difficult in embedded system
Let system recover automatically
Automatic recovery
Two general schemes
Abort
All processes
One at a time
Preempt resources
Process Termination
All deadlocked processes terminated
Will clearly break deadlock
At great expense
Processes may have computed for long time
All results may be lost
- 12 of 13 -
Resource Preemption
Method requires
Successive preemption of resources
Allocation to other processes
Until deadlock cycle broken
If preemption used
Must consider three issues
1. Selecting a victim
Must determine order of preemption to minimize cost
Factors include
Number or resources deadlocked process holding
Amount of elapsed execution time for deadlocked process
2. Rollback
If resource preempted
What should be done with associated process
Cannot continue
Often cannot determine completely safe state
Simplest solution is complete rollback
Abort process and restart
Can try to roll back as far as necessary to break deadlock
Entails maintaining information on all running processes
3. Starvation
How to ensure starvation will not occur
Want to ensure resources not always preempted from same process
Summary
Deadlock occurs when two or more processes
Waiting for event that can only be caused by one of waiting processes
3 major methods for addressing
Use protocol to ensure will never enter deadlock state
Allow system to enter deadlock state and recover
Ignore problem
Deadlock can only occur
If and only if 4 conditions occur simultaneously
We prevent deadlock
Ensuring one condition will not occur
If system does not employ protocol to ensure deadlock does not occur
Then detection and recovery scheme must be employed
If deadlock detected
Can recover by global or selective termination
Process
Resources
- 13 of 13 -