11 deadlock
TRANSCRIPT
Operating SystemsECE344
Ashvin GoelECE
University of Toronto
Deadlock
2
Resources and Deadlocks
Processes access resources to make progressExamples of computer resourceso
Printerso
Disk driveso
Kernel data structures (e.g., process table, file table)o
Locks and semaphores to protect critical sectionsSuppose there are two processes and two resourceso
Process 1 holds resource A and requests resource Bo
At the same time, Process 2 holds B and requests Ao
Both will become blocked and remain so!o
This is a deadlock
3
Types of Resources
Non-preemptable or Preemptableo
If a non-preemptable
resource is taken away from a process, the process fails
o
A
preemptable
resource can be taken away from a process with no ill effects
Exclusive or Sharedo
Only one process can access an exclusive
resource at a time
o
One or more processes access a shared
resource simultaneously
4
Resource Usage Model
Resource use eventso
Request
the resource (like acquiring a mutex
lock)o
Use
the resourceo
Release
the resource (like releasing a mutex
lock)Response on request denialo
Wait
(either block or busy wait)o
Fail
with error code
5
Deadlock Conditions (informally)
Deadlocks occur when processes are granted exclusive access to non-preemptable resources and wait when a resource request is denied
6
Definition of Deadlock
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 performUsually the event is the release of a currently held resourceAs a result, none of the processes cano
Be awakenedo
Runo
Release resources
7
Deadlock Conditions
A deadlock situation can occur if and only if the following conditions hold simultaneouslyo
Mutual exclusion condition
–
each resource is assigned to one process
o
Hold and wait condition
–
processes can get more than one resource
o
No preemption condition –
acquired resources cannot be preempted
o
Circular wait condition
–
chain of two or more processes must be waiting for a resource from next process in circular chain
8
Examples of Deadlock
9
Resource Use Examples
Thread_A() {acquire(resource_1);use resource 1;release(resource_1);
}
Mutex mylock1;
lock(mylock1);// Use resource_1unlock(mylock1);
Semaphore sem1 = 1;
wait(sem1);// Use resource_1signal(sem1);
10
One Resource Acquisition Scenario
Thread_A() {acquire(resource_1);use resource 1;release(resource_1);
}
Thread_B() {acquire(resource_2);use resource 2;release(resource_2);
}
No deadlock is possible
11
Two Resource Acquisition Scenario -
1
Thread_A() {acquire(resource_1);acquire(resource_2);use resource 1 and 2;release(resource_2);release(resource_1);
}
Thread_B() {acquire(resource_1);acquire(resource_2);use resource 1 and 2;release(resource_2);release(resource_1);
}
No deadlock is possible
12
Two Resource Acquisition Scenario -
2
Thread_A() {acquire(resource_1);use resource 1;release(resource_1);acquire(resource_2);use resource 2;release(resource_2);
}
Thread_B() {acquire(resource_2);use resource 2;release(resource_2);acquire(resource_1);use resource 1;release(resource_1);
}
No deadlock is possible
13
Two Resource Acquisition Scenario -
3
Thread_A() {acquire(resource_2);acquire(resource_1);use resource 1 and 2;release(resource_1);release(resource_2);
}
Thread_B() {acquire(resource_1);acquire(resource_2);use resource 1 and 2;release(resource_2);release(resource_1);
}
Deadlock is possible!
14
Consequences of Deadlock
Deadlock occurs in a single programo
Kill the programDeadlock occurs in the OSo
User programs can freezeo
System hangs or crasheso
Must restart the system and kill all applications
15
Dealing with Deadlock -
Four Strategies
Ignore the problemo
Advantages, disadvantages?Detection and recoveryAvoidancePrevention
16
Deadlock Detection
Let the problem happen, then detect and recoverHow should a deadlock be detected?Use a resource allocation graph
ResourceR1
P1Process/Thread
“is held by”
“is requesting”
ResourceR2
P2
Deadlock Cycle in the graph
17
Deadlock Detection
Do a depth-first-search for cycles on the resource allocation graph starting at each process node
Deadlock!
18
Deadlock Detection, Multiple Resources
Previous algorithm works with single resource of each type (e.g., printer, scanner)What if there are multiple resources of each type that can be used interchangeably?Need to use a resource allocation matrix
19
Resource Allocation Matrix
Invariant property: Σi=1
Cij
+ Aj
= Ej
20
Detection Algorithm, Multiple Resources
Step 1: Look for some unmarked process Pi, for which the i-th row of R is less or equal to A (using vector comparison)Step 2: If such a process is found, add the i-throw of C to A, mark the process and go back to Step 1If no such process is found, terminate algorithmWhen the algorithm terminates, any unmarkedprocess is deadlocked!
21
Detection Algorithm -
Example
22
Deadlock Detection Issues
How often should the algorithm run?o
On every resource request?o
Periodically?o
When CPU utilization is low?o
When we suspect deadlock because some thread has been asleep for a long period of time?
23
Recovery from Deadlock
If deadlock is detected, what should be done?o
Abort deadlocked processes and reclaim resourceso
Abort one process at a time until deadlock cycle is eliminated
Where should the abort start?o
Lowest priority process?o
Shortest running process?o
Process with fewest resources held?o
Batch processes before interactive processes?o
Minimize number of processes to be terminated?
24
Deadlock Avoidance
Detection vs. Avoidanceo
Detection –
“optimistic” approachAllocate resourcesDetect and fix problem
o
Avoidance –
“pessimistic” approachDon’t allocate resource if it may lead to deadlockIf a process requests a resource make it wait until it is know that there will be no problem
Which one should be used depends upon how easy is it to recover from deadlock?
25
Banker’s Scheduling Algorithm
An extension of the multiple resource detection algorithmUses the notion of a “safe” stateA safe state is one in which there is1.
No deadlock2.
There is some scheduling order by which every process can run to completion even if all of them request their maximum number of units immediately
Banker’s algorithm schedules threads so as to avoid unsafe states => avoids deadlock
26
Banker’s Scheduling Algorithm
These are the max. possiblerequests,
which we assumeare known ahead of time!
Invariant property: Σi=1
Cij
+ Aj
= Ej
27
Banker’s Scheduling Algorithm
When a resource request comes in, the resource matrices are temporarily updated and then the multiple resource, deadlock detection algorithm is runIf there is no deadlock, the resource is grantedIf there is a deadlock, the resource matrices are rolled back and the request is deferred (the thread needs to wait)
28
Problems with Deadlock Avoidance
The algorithm needs to be run on every resource request!Deadlock avoidance is often impossible because it is hard to know in advance what resources a process will need!Alternative approach is deadlock preventiono
Make deadlock impossible!o
Attack one of the four conditions that are necessary for deadlock to be possible
29
Deadlock Prevention
Conditions necessary for deadlocko
Mutual exclusion condition
–
each resource is assigned to one process
o
Hold and wait condition
–
processes can get more than one resource
o
No preemption condition –
acquired resources cannot be preempted
o
Circular wait condition
–
chain of two or more processes must be waiting for a resource from next process in circular chain
30
Deadlock Prevention
Attacking mutual exclusion?o
A bad idea for most resource types since resource could be corrupted
Attacking hold and wait?o
Require processes to request all resources before they begin!
o
When problems occur a process must release all its resources and start again
Attacking no preemption?o
A bad idea for most resource types since resource could be left in an inconsistent state
31
Deadlock Prevention
Attacking circular waiting?o
Number each of the resourceso
Require each process to acquire lower numbered resources before higher numbered resources
o
More precisely: A process is not allowed to request a resource whose number is lower than the highest numbered resource it currently holds
o
Why does this method work?
32
Recall this Deadlock
Assume that ordering of resources is such that Order(Resource_1) = 1, Order(Resource_2) = 2Thread A violates the ordering!
Thread_A() {acquire(resource_2);acquire(resource_1);use resource 1 and 2;release(resource_1);release(resource_2);
}
Thread_B() {acquire(resource_1);acquire(resource_2);use resource 1 and 2;release(resource_2);release(resource_1);
}
33
Resource Ordering
The main problem with ordering resources is that it may be hard to come up with an acceptable ordering of resources!o
Think about third-party softwareStill,this is the most useful approach in an OSExample ordering of resources in Blitz:1.
ProcessControlBlock2.
FileControlBlock3.
Page FramesHowever, the problem of resources with multiple units is not addressed
34
Deadlock, Starvation, Livelock
Deadlocko
A particular set of threads perform no work because of a circular wait condition
o
Once a deadlock occurs, it does not go awayStarvationo
A particular set of threads perform no work because the resources they need are being used by others constantly
o
Starvation can be a temporary conditionLivelocko
A particular set of threads continue to run but make no progress
35
Think Time
What is deadlock?What conditions must hold for deadlock to be possible?What are the main approaches for dealing with deadlock?Why does resource ordering help?
36
Before Next Class
This set of slides cover pages 159-188Reading for next class - pages 189-202