Upload
sat0912
View
228
Download
0
Embed Size (px)
Citation preview
8/12/2019 07. Deadlock
1/51
SIT222Operating SystemsSession 07. Deadlock
8/12/2019 07. Deadlock
2/51
Outline
Introducing deadlock
Deadlock detection and recovery Deadlock avoidance Deadlock prevention
2SIT222 Session 7
8/12/2019 07. Deadlock
3/51
Introducing deadlock
What is deadlock?
Two or more processes are blocked waiting foradditional resources Those resources are held by other processes
that are also waiting, i.e., The processes are waiting for each other
No processes can continue/progress until theresource/s held by the other/s are released
3SIT222 Session 7
8/12/2019 07. Deadlock
4/51
Introducing deadlock
We already identified a possible deadlock using semaphoreslast week:
P 0 P 1 wait ( A ); wait ( B);wait ( B); wait ( A );
signal ( A ); signal ( B);signal ( B) signal ( A );
Deadlock is possible in this example where: P 0 holds resource A and is waiting for resource B P 1 holds resource B and is waiting for resource A Both wait indefinitely
4SIT222 Session 7
8/12/2019 07. Deadlock
5/51
Introducing deadlock
This occurs because the resources in the previousexample are non-preemptable It is not possible to forcibly remove the resources
from the process without causing an error in theprocess
E.g., semaphores protect critical regions,taking semaphores away would allow twoprocesses in the critical region resulting in raceconditions
Non-preemptable resources also include resourcessuch as the console, files, printer, keyboard, etc.
5SIT222 Session 7
8/12/2019 07. Deadlock
6/51
Introducing deadlock
Note however that there are also preemptableresources
Can be safely taken away from a process andgiven to another We have already seen two preemptable resources:
CPU is switched between processes regularlywithout causing any problems
CPU state is saved and restored each switch
Memory using virtual memory and pagereplacement, physical memory can be sharedbetween processes even when demand exceedsphysical capacity
6SIT222 Session 7
8/12/2019 07. Deadlock
7/51
Introducing deadlock
There are four conditions that are required for deadlock1. Mutual exclusion condition Each resource is either
currently assigned to exactly one process or isavailable.2. Hold and wait condition Processes currently holding
resource that were granted earlier can request newresources.
3. No preemption condition Resources previouslygranted cannot be forcibly taken away from a process.They must be explicitly released by the process holdingthem.
4. Circular wait condition There must be a circular chainof two or more processes, each of which is waiting fora resource held by the next member of the chain.
7SIT222 Session 7Ref: Tanenbaum, A.S., Modern Operating Systems 3/e, Pearson Education, 2008: Section 6.2.1, pp438.
8/12/2019 07. Deadlock
8/51
Introducing deadlock
Example problem: Dining Philosophers A table is laid out for n philosophers to dine
There are n plates of spaghetti There are n forks to eat the spaghetti Two forks are required to eat spaghetti Philosopher alternates between thinking and eating
Philosopher considers philosophical problems(thinking)
Philosopher picks up the left fork, then the right fork Philosopher eats some spaghetti (eating) Philosopher drops both forks Repeat
8SIT222 Session 7
8/12/2019 07. Deadlock
9/51
Introducing deadlock
Here we see places forfive philosophers
Deadlock conditions: Mutual exclusion
a fork can only beheld by onephilosopherotherwise it mustbe available
9SIT222 Session 7
8/12/2019 07. Deadlock
10/51
Introducing deadlock
Here we see places forfive philosophers
Deadlock conditions: Hold and wait a
philosopher maypick up one forkthen try to pick upanother
10SIT222 Session 7
8/12/2019 07. Deadlock
11/51
Introducing deadlock
Here we see places forfive philosophers
Deadlock conditions: No preemption
there is no processdefined to remove afork from aphilosopher
11SIT222 Session 7
8/12/2019 07. Deadlock
12/51
Introducing deadlock
Here we see places forfive philosophers
Deadlock conditions: Circular wait if
each philosopherpicks up the leftfork, no philosophercan get a secondfork to start eating
12SIT222 Session 7
8/12/2019 07. Deadlock
13/51
Introducing deadlock
Solution 1: Allow only four
(n-1) philosophersto sit at the table
At least onephilosopher willbe able to pickup second fork,eat, and returnto thinking
13SIT222 Session 7
8/12/2019 07. Deadlock
14/51
Introducing deadlock
Solution 2: Allow a philosopher
to pick up only twoforks at a time
Requires asemaphore orsimilarmechanism toprotect criticalregion
14SIT222 Session 7
8/12/2019 07. Deadlock
15/51
Introducing deadlock
Solution 3: Use an asymmetric
solution, e.g., Philosophers are
numbered Odds pick up left
fork first Evens pick upright fork first
15SIT222 Session 7
8/12/2019 07. Deadlock
16/51
Introducing deadlock
There are four ways to deal with deadlock:1. Do nothing allows deadlock to occur and
manually recover2. Detection and recovery deadlocks occur with
automatic actions to recover3. Avoidance manage allocation of resources to
avoid deadlock4. Prevention eliminate one of the fourconditions for deadlock
16SIT222 Session 7
8/12/2019 07. Deadlock
17/51
Introducing deadlock
The first solution initially seems quite strange: Do nothing allow deadlocks to occur andmanually recover
This can be quite valid however, e.g., consider thefollowing scenario
Deadlock occurs only once a year
System crashes on a daily basis What is the cost of introducing other deadlocksolutions instead of handling deadlock asanother system crash?
17SIT222 Session 7
8/12/2019 07. Deadlock
18/51
Deadlock detection and recovery
Deadlock detection is possible by producing aresource graph:
Boxes = resources Circles = processes
18SIT222 Session 7
8/12/2019 07. Deadlock
19/51
Deadlock detection and recovery
Deadlock detection is possible by producing aresource graph:
(a) Holding a resource, (b) Requesting a resource,and (c) deadlock (circular wait)
19SIT222 Session 7
8/12/2019 07. Deadlock
20/51
Deadlock detection and recovery
Deadlocks are apparent wherever there is a cycle inthe graph, e.g.,
20SIT222 Session 7
8/12/2019 07. Deadlock
21/51
Deadlock detection and recovery
Three approaches to recovery: Preemption (for preemptable resources)
Take resource/s from one process and give it toanother, which will complete and free resources
RollbackTake checkpoints/snapshots of processesregularly and rewind a process in the deadlock,
freeing unused resources at the last snapshot Process terminationTerminate one or more processes to free theirresources (preferably one that can be re-run!)
21SIT222 Session 7
8/12/2019 07. Deadlock
22/51
Deadlock detection and recovery
Deadlock detection and recovery has problemshowever
What is the cost of the chosen recovery option/s? e.g., lost computation time when terminating aprocess
In undertaking recovery, is it possible deadlockmay occur again
e.g., terminating one process allows
computation to continue for a short whilebefore deadlock occurs a second/third/etc. time If the recovery requires intervention by a person,
who will do this? Will someone need to behired/paid to do this?
22SIT222 Session 7
8/12/2019 07. Deadlock
23/51
Deadlock avoidance
It is possible to avoid deadlock by choosingwhether to allocate resources to a process or toblock their request, suspending the process
Requires the following information Number of resources available Number of resources allocated
Number of resources required (maximumnumber required)
23SIT222 Session 7
8/12/2019 07. Deadlock
24/51
Deadlock avoidance
Definitions: Safe state: Where there exists a sequence of
execution of processes sharing resources suchthat deadlock will not occur
Unsafe state: Where there does not exist such asequence and deadlock will occur if processes
Do not free the resources they have Require their maximum resources tocomplete
24SIT222 Session 7
8/12/2019 07. Deadlock
25/51
Deadlock avoidance
The Bankers Algorithm is used to determine whichrequests for resources can be fulfilled Begin with:
Resources already allocated to processes(Allocated matrix)
Maximum resources required by processes(Maximum matrix)
The number of resources currently available(Available vector)
Example
25SIT222 Session 7
8/12/2019 07. Deadlock
26/51
Deadlock avoidance
Allocation Max Available A B C A B C A B C
P 0 0 1 0 7 5 3 3 3 2P 1 2 0 0 3 2 2P 2 3 0 2 9 0 2P 3 2 1 1 2 2 2
P 4 0 0 2 4 3 3 Calculate the resources required (matrix need) for
each process to complete (= Max Allocation)
26SIT222 Session 7
8/12/2019 07. Deadlock
27/51
Deadlock avoidance
Allocation Max Need Available A B C A B C A B C A B C
P 0 0 1 0 7 5 3 7 4 3 3 3 2P 1 2 0 0 3 2 2P 2 3 0 2 9 0 2P 3 2 1 1 2 2 2
P 4 0 0 2 4 3 3 Example:
P 0 : Max (7 5 3) Allocation (0 1 0) = Need (7 4 3)
27SIT222 Session 7
8/12/2019 07. Deadlock
28/51
Deadlock avoidance
Allocation Max Need Available A B C A B C A B C A B C
P 0 0 1 0 7 5 3 7 4 3 3 3 2P 1 2 0 0 3 2 2 1 2 2P 2 3 0 2 9 0 2P 3 2 1 1 2 2 2
P 4 0 0 2 4 3 3 Example:
P 1 : Max (7 5 3) Allocation (0 1 0) = Need (7 4 3)
28SIT222 Session 7
8/12/2019 07. Deadlock
29/51
Deadlock avoidance
Allocation Max Need Available A B C A B C A B C A B C
P 0 0 1 0 7 5 3 7 4 3 3 3 2P 1 2 0 0 3 2 2 1 2 2P 2 3 0 2 9 0 2 6 0 0P 3 2 1 1 2 2 2
P 4 0 0 2 4 3 3 Example:
P 2 : Max (9 0 2) Allocation (3 0 2) = Need (6 0 0)
29SIT222 Session 7
8/12/2019 07. Deadlock
30/51
Deadlock avoidance
Allocation Max Need Available A B C A B C A B C A B C
P 0 0 1 0 7 5 3 7 4 3 3 3 2P 1 2 0 0 3 2 2 1 2 2P 2 3 0 2 9 0 2 6 0 0P 3 2 1 1 2 2 2 0 1 1
P 4 0 0 2 4 3 3 Example:
P 3 : Max (2 2 2) Allocation (2 1 1) = Need (0 1 1)
30SIT222 Session 7
8/12/2019 07. Deadlock
31/51
Deadlock avoidance
Allocation Max Need Available A B C A B C A B C A B C
P 0 0 1 0 7 5 3 7 4 3 3 3 2P 1 2 0 0 3 2 2 1 2 2P 2 3 0 2 9 0 2 6 0 0P 3 2 1 1 2 2 2 0 1 1
P 4 0 0 2 4 3 3 4 3 1 Example:
P 4 : Max (4 3 3) Allocation (0 0 2) = Need (4 3 1)
31SIT222 Session 7
8/12/2019 07. Deadlock
32/51
Deadlock avoidance
Allocation Need Available A B C A B C A B C
P 0 0 1 0 7 4 3 3 3 2P 1 2 0 0 1 2 2P 2 3 0 2 6 0 0P 3 2 1 1 0 1 1
P 4 0 0 2 4 3 1 Now identify processes which can be allocated their
remaining resources and be allowed to complete
32SIT222 Session 7
8/12/2019 07. Deadlock
33/51
Deadlock avoidance
Allocation Need Available A B C A B C A B C
P 0 0 1 0 7 4 3 3 3 2P 1 2 0 0 1 2 2P 2 3 0 2 6 0 0P 3 2 1 1 0 1 1
P 4 0 0 2 4 3 1 Either of these two could (need < available),
returning their (allocated) resources to the pool
33SIT222 Session 7
Need (1 2 2) is lessthan available (3 3 2)
Need (0 1 1) is lessthan available (3 3 2)
8/12/2019 07. Deadlock
34/51
8/12/2019 07. Deadlock
35/51
Deadlock avoidance
Allocation Need Available A B C A B C A B C
P 0 0 1 0 7 4 3 5 3 2P 1 0 0 0 0 0 0P 2 3 0 2 6 0 0P 3 2 1 1 0 1 1
P 4 0 0 2 4 3 1 Repeat until all processes can complete
Sequence: P 1
35SIT222 Session 7
8/12/2019 07. Deadlock
36/51
Deadlock avoidance
Allocation Need Available A B C A B C A B C
P 0 0 1 0 7 4 3 7 4 3P 1 0 0 0 0 0 0P 2 3 0 2 6 0 0P 3 0 0 0 0 0 0
P 4 0 0 2 4 3 1 Repeat until all processes can complete
Sequence: P 1 P 3
36SIT222 Session 7
8/12/2019 07. Deadlock
37/51
Deadlock avoidance
Allocation Need Available A B C A B C A B C
P 0 0 0 0 0 0 0 7 5 3P 1 0 0 0 0 0 0P 2 3 0 2 6 0 0P 3 0 0 0 0 0 0
P 4 0 0 2 4 3 1 Repeat until all processes can complete
Sequence: P 1 P 3 P 0
37SIT222 Session 7
8/12/2019 07. Deadlock
38/51
Deadlock avoidance
Allocation Need Available A B C A B C A B C
P 0 0 0 0 0 0 0 10 5 5P 1 0 0 0 0 0 0P 2 0 0 0 0 0 0P 3 0 0 0 0 0 0
P 4 0 0 2 4 3 1 Repeat until all processes can complete
Sequence: P 1 P 3 P 0 P 2
38SIT222 Session 7
8/12/2019 07. Deadlock
39/51
8/12/2019 07. Deadlock
40/51
Deadlock avoidance
By using the Bankers Algorithm, we determinedthat the system is in a safe state because there is asequence in which processes can complete withoutdeadlock: P 1 P 3 P 0 P 2 P 4 P 1 can complete, freeing its resources P 2 can then complete, freeing its resources There are now enough resources for P 4 to
complete, i.e., the system is in a safe state!
40SIT222 Session 7
8/12/2019 07. Deadlock
41/51
Deadlock avoidance
The fact that other sequences are possible does notmatter, there is at least one
The system does not need to execute according toany determined sequence Instead, when a request for a resource comes in
Assume the request is granted Use the Bankers Algorithm to determine if the
system is in a safe state If yes, grant the resource If no, block the process until more resources
are available
41SIT222 Session 7
8/12/2019 07. Deadlock
42/51
Deadlock avoidance
Allocation Need Available A B C A B C A B C
P 0 0 1 0 7 4 3 3 3 2P 1 2 0 0 1 2 2P 2 3 0 2 6 0 0P 3 2 1 1 0 1 1
P 4 0 0 2 4 3 1 Consider what happens if a request arrives from P 0
for three type B resources
42SIT222 Session 7
Subtract three type Bresources from Needand add to Allocation
8/12/2019 07. Deadlock
43/51
Deadlock avoidance
Allocation Need Available A B C A B C A B C
P 0 0 4 0 7 1 3 3 0 2P 1 2 0 0 1 2 2P 2 3 0 2 6 0 0P 3 2 1 1 0 1 1
P 4 0 0 2 4 3 1 Assume the request is granted
43SIT222 Session 7
8/12/2019 07. Deadlock
44/51
Deadlock avoidance
Allocation Need Available A B C A B C A B C
P 0 0 4 0 7 1 3 3 0 2P 1 2 0 0 1 2 2P 2 3 0 2 6 0 0P 3 2 1 1 0 1 1
P 4 0 0 2 4 3 1 Clearly, the system is in an unsafe state, no
process can now complete its execution
44SIT222 Session 7
8/12/2019 07. Deadlock
45/51
Deadlock avoidance
Unfortunately there is a major problem toovercome to successfully apply avoidance: How do you know how many resources a
process will need to complete its work?i.e., how to determine the Maximum matrix?
45SIT222 Session 7
8/12/2019 07. Deadlock
46/51
Deadlock prevention
Deadlock prevention is somewhat straightforward Recall the conditions for deadlock:
1. Mutual exclusion condition Each resource is eithercurrently assigned to exactly one process or is available.
2. Hold and wait condition Processes currently holdingresource that were granted earlier can request newresources.
3. No preemption condition Resources previously grantedcannot be forcibly taken away from a process. They mustbe explicitly released by the process holding them.
4. Circular wait condition There must be a circular chain oftwo or more processes, each of which is waiting for aresource held by the next member of the chain.
All you have to do is eliminate one!
46SIT222 Session 7
8/12/2019 07. Deadlock
47/51
Deadlock prevention
1. Mutual exclusion condition Each resource iseither currently assigned to exactly one process
or is available.
For preemptable resources, this is already the case For non-preemptable resources, there can often be
other solutions, e.g., virtualize the resource: Two processes printing will cause corrupt output
Spool jobs to a daemon which then prints jobsin sequence to the printer
Not always possible!!!
47SIT222 Session 7
8/12/2019 07. Deadlock
48/51
8/12/2019 07. Deadlock
49/51
Deadlock prevention
3. No preemption condition Resourcespreviously granted cannot be forcibly takenaway from a process. They must be explicitlyreleased by the process holding them.
Not a problem for preemptable resources theyare just preempted!
For non-preemptable resources, again virtualizationmay provide a solution Remember, not always possible!
49SIT222 Session 7
8/12/2019 07. Deadlock
50/51
Deadlock prevention
4. Circular wait condition There must be a circular chainof two or more processes, each of which is waiting fora resource held by the next member of the chain.
Only allow a process to hold one resource at any one time Impose an ordering on resources, i.e., number every
resource, two alternatives: A process can only request resources in order of the
sequence More generally, a process holding one resource can only
request resources appearing later in the sequence
50SIT222 Session 7
8/12/2019 07. Deadlock
51/51
Summary
Introducing deadlock Deadlock detection and recovery Deadlock avoidance Deadlock prevention