Professional Documents
Culture Documents
Alokasi
Alokasi
Alokasi
Lecture #3
Deadlocks
▪ What is a deadlock?
2
Resources
3
The Deadlock Problem
▪ A set of blocked processes each holding a resource and
waiting to acquire a resource held by another process
in the set.
▪ Example:
System has 2 tape drives.
P1 and P2 each hold one tape drive and each needs another
one.
▪ Example:
Semaphores A and B, initialized to 1
P0 P1
5
Roundabout (Traffic Circle) Example
6
Crossing Example
c b 2 3 2
4 d a 4 1
8
Resources (1)
9
Resources (2): System Model
10
Resources (3)
11
Resource Acquisition (1)
12
Resource Acquisition (2)
▪ (a) Deadlock-free
code.
13
Resource Acquisition (3)
14
Resource Categories
Reusable
• Can be safely used by only one process at a time
and is not depleted by that use
• Processors, I/O channels, main and secondary
memory, devices, and data structures such as
files, databases, and semaphores
Consumable
• One that can be created (produced) and
destroyed (consumed)
• Interrupts, signals, messages, and
information
• In I/O buffers
15
16
Example:
Memory Request
▪ Space is available for allocation of 200Kbytes, and the
following sequence of events occur:
P1 P2
... ...
Request 80 Kbytes; Request 70 Kbytes;
... ...
Request 60 Kbytes; Request 80 Kbytes;
17
Consumable Resources Deadlock
18
Introduction to Deadlocks
▪ Formal definition :
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
▪ Usually the event is release of a currently held resource
▪ None of the processes can …
run
release resources
be awakened
19
Deadlock
▪ The permanent blocking of a set of processes that either
compete for system resources or communicate with each
other
▪ A set of processes is deadlocked when each process in
the set is blocked awaiting an event that can only be
triggered by another blocked process in the set
▪ Permanent because none of the events is
ever triggered
▪ No efficient solution in the general case
20
Four Conditions for Deadlocks (1)
21
Four Conditions for Deadlock (2)
22
Conditions for Deadlock
Mutual
Hold-and-Wait No Pre-emption Circular Wait
Exclusion
• Only one • A process • No resource • A closed
process may may hold can be chain of
use a resource allocated forcibly processes
at a time resources removed from exists, such
• No process while a process that each
may access a awaiting holding it process holds
resource until assignment of at least one
that has been other resource
allocated to resources needed by the
another next process
process in the chain
23
Resource Allocation Graph
24
Resource Allocation Graph (Cont.)
▪ Process
▪ Pi requests instance of Rj
Pi
Rj
▪ Pi is holding an instance of Rj Pi
Rj
25
Example of a Graph with No Cycles
R1 R3
P1 P2 P3
R2 R4
26
Example of a Graph with a Cycle
R1 R3
P1 P2 P3
R2 R4
27
Fundamental Facts
28
Deadlock Modeling (1)
▪ Modeled with directed graphs
29
Deadlock Modeling (2)
A B C
32
The Ostrich Algorithm
33
The Ostrich Algorithm (2)
▪ This method used by most operating systems. User
programs are expected to behave properly. The
system does not interface. This is understandable :
it is very hard to make general rules for every
situations which might arise.
34
Deadlock Detection
35
Single Instance of Each Resource Type
36
Detection with One Resource of Each Type
38
Detection with Multiple Resources of Each Type
39
Multiple Instances of a Resource Type
C
i =1
ij + Aj = E j
40
Multiple Instances of a Resource Type
Let n = number of processes and m = number of resource types.
41
Detection Algorithm (Short Form)
1. Look for unmarked process Pi for which the i-th row of R is
less than or equal to A
42
Detection Algorithm
44
Example of Detection Algorithm
A B C A B C A B C
0 0 0 P0 0 1 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
45
Example (Cont.)
A B C A B C A B C
0 0 0 P0 0 1 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 1
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
46
Detection Algorithm Usage
▪ When, and how often, to invoke depends on:
How often is deadlock likely to occur?
How many processes will need to be rolled back?
▪ If deadlock algorithm is invoked arbitrarily, there may
be many cycles in the resource graph and so we would
not be able to tell which of the many deadlocked
processes “caused” the deadlock.
47
Recovery from Deadlock (1)
48
Recovery from Deadlock (2)
49
Recovery from Deadlock: Resource Preemption
▪ Selecting a victim — minimize cost.
▪ Rollback — return to some safe state, restart process
from that state.
▪ Starvation — same process may always be picked as
victim; include number of rollbacks in cost factor.
50
Recovery from Deadlock: Process
Termination
▪ Abort all deadlocked processes.
▪ Abort one process at a time until the deadlock cycle is
eliminated.
▪ In which order should be choose to abort?
Priority of the process.
Computation metric — current length or length to
completion
Resources the process has used.
Resources the process is requesting.
Number of process requiring termination.
Interactive versus batch.
51
Deadlock Avoidance
Resource Trajectories
53
Resource Trajectories (1)
56
Safe and Unsafe States (2)
57
The Banker's Algorithm for a Single Resource
58
Banker's Algorithm for Multiple Resources
1. Look for a row R, whose unmet needs are all smaller than
or equal to A. If no such row → system eventually will
deadlock
2. Assume the process of the row chosen rquests all
resources it needs and finishes. Mark that process as
terminated and add all its resources to A.
3. Repeat steps 1 and 2 until either all processes marked as
terminated (safe state) or deadlock occurs
60
Example for Banker's Algorithm (1)
R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 3 2 2 P1 1 0 0 P1 2 2 2
P2 6 1 3 P2 6 1 2 P2 0 0 1
P3 3 1 4 P3 2 1 1 P3 1 0 3
P4 4 2 2 P4 0 0 2 P4 4 2 0
Claim matrix C Allocation matrix A C–A
R1 R2 R3 R1 R2 R3
9 3 6 0 1 1
Resource vector R Available vector V
61
Example for Banker's Algorithm (2)
R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 3 2 2 P1 1 0 0 P1 2 2 2
P2 0 0 0 P2 0 0 0 P2 0 0 0
P3 3 1 4 P3 2 1 1 P3 1 0 3
P4 4 2 2 P4 0 0 2 P4 4 2 0
Claim matrix C Allocation matrix A C–A
R1 R2 R3 R1 R2 R3
9 3 6 6 2 3
Resource vector R Available vector V
62
Example for Banker's Algorithm (3)
R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 0 0 0 P1 0 0 0 P1 0 0 0
P2 0 0 0 P2 0 0 0 P2 0 0 0
P3 3 1 4 P3 2 1 1 P3 1 0 3
P4 4 2 2 P4 0 0 2 P4 4 2 0
Claim matrix C Allocation matrix A C–A
R1 R2 R3 R1 R2 R3
9 3 6 7 2 3
Resource vector R Available vector V
63
Example for Banker's Algorithm (4)
R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 0 0 0 P1 0 0 0 P1 0 0 0
P2 0 0 0 P2 0 0 0 P2 0 0 0
P3 0 0 0 P3 0 0 0 P3 0 0 0
P4 4 2 2 P4 0 0 2 P4 4 2 0
Claim matrix C Allocation matrix A C–A
R1 R2 R3 R1 R2 R3
9 3 6 9 3 4
Resource vector R Available vector V
64
Example for Banker's Algorithm (5)
R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 3 2 2 P1 1 0 0 P1 2 2 2
P2 6 1 3 P2 5 1 1 P2 1 0 2
P3 3 1 4 P3 2 1 1 P3 1 0 3
P4 4 2 2 P4 0 0 2 P4 4 2 0
Claim matrix C Allocation matrix A C–A
R1 R2 R3 R1 R2 R3
9 3 6 1 1 2
Resource vector R Available vector V
R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 3 2 2 P1 2 0 1 P1 1 2 1
P2 6 1 3 P2 5 1 1 P2 1 0 2
P3 3 1 4 P3 2 1 1 P3 1 0 3
P4 4 2 2 P4 0 0 2 P4 4 2 0
Claim matrix C Allocation matrix A C–A
R1 R2 R3 R1 R2 R3
9 3 6 0 1 1
Resource vector R Available vector V
66
Deadlock Avoidance Advantages
67
Deadlock Avoidance Restrictions
68
Deadlock Strategies
69
Deadline Detection Algorithm
Advantages:
▪ A check for deadlock • It leads to early
detection
can be made as
• The algorithm is
frequently as each relatively simple
resource request or,
less frequently,
depending on how
Disadvantage
likely it is for a • Frequent checks
consume
deadlock to occur considerable
processor time
70
Deadlock Prevention
Attacking the Mutual Exclusion Condition
▪ Some devices (such as printer) can be spooled
only the printer daemon uses printer resource
thus deadlock for printer eliminated
▪ Not all devices can be spooled
▪ Principle:
avoid assigning resource when not absolutely necessary
as few processes as possible actually claim the resource
71
Attacking the Hold and Wait Condition
▪ Require processes to request resources before starting
a process never has to wait for what it needs
▪ Problems
may not know required resources at start of run
also ties up resources other processes could be using
▪ Variation:
process must give up all resources
then request all immediately needed
72
Attacking the No Preemption Condition
73
Attacking the Circular Wait Condition
(a) (b)
74
Approaches to Deadlock Prevention
75
Combined Approach to Deadlock Handling
76
Other Issues
▪ Two-phase locking
▪ Nonresource deadlocks
▪ Communication deadlocks
▪ Livelock
▪ Starvation
Two-Phase Locking
▪ Phase One
process tries to lock all records it needs, one at a time
if needed record found locked, start over
(no real work done in phase one)
▪ If phase one succeeds, it starts second phase,
performing updates
releasing locks
▪ Note similarity to requesting all resources at once
▪ Algorithm works where programmer can arrange
program can be stopped, restarted
78
Nonresource Deadlocks
79
Communication Deadlocks
80
Livelock
81
Starvation
▪ Solution:
First-come, first-serve policy
82
Summary
Resources
Preemptable resources
Non-preemptable resources
Resources allocation
What is a deadlock?
Background
Deadlock Conditions
Deadlock prevention
Deadlock avoidance
Deadlock prediction
Starvation
83
Next Lecture
Memory Management
84
References
85