ch02.5 - Deadlock
ch02.5 - Deadlock
2.5: Deadlocks
Outline
System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
Operating System Concepts – 10th Edition 8.2 Silberschatz, Galvin and Gagne ©2018
1
Chapter Objectives
Illustrate how deadlock can occur when mutex locks are used
Define the four necessary conditions that characterize deadlock
Identify a deadlock situation in a resource allocation graph
Evaluate the four different approaches for preventing deadlocks
Apply the banker’s algorithm for deadlock avoidance
Apply the deadlock detection algorithm
Evaluate approaches for recovering from deadlock
Operating System Concepts – 10th Edition 8.3 Silberschatz, Galvin and Gagne ©2018
System Model
System consists of resources
Resource types R1, R2, . . ., Rm
• CPU cycles, memory space, I/O devices
Each resource type Ri has Wi instances.
Each process utilizes a resource as follows:
• Request: The thread requests the resource
• Use: The thread can operate on the resource
• Release: The thread releases the resource.
Operating System Concepts – 10th Edition 8.4 Silberschatz, Galvin and Gagne ©2018
2
Deadlock Example with Semaphores
Data:
• A semaphore S1 initialized to 1
• A semaphore S2 initialized to 1
Two processes P1 and P2
P1:
wait(s1)
wait(s2)
P2:
wait(s2)
wait(s1)
Operating System Concepts – 10th Edition 8.5 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 8.6 Silberschatz, Galvin and Gagne ©2018
3
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
Operating System Concepts – 10th Edition 8.7 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph
A set of vertices V and a set of edges E.
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 of all resource types in the system
Operating System Concepts – 10th Edition 8.8 Silberschatz, Galvin and Gagne ©2018
4
Resource Allocation Graph Example
One instance of R1 ; Two instances of R2. ; One instance of R3 ;Three instance of R4
T1 holds one instance of R2 and is waiting for an instance of R1
T2 holds one instance of R1, one instance of R2, and is waiting for an instance of R3
T3 holds one instance of R3
Deadlock
- T1 → R1 → T2 → R3 → T3 → R2 → T1
- T2 → R3 → T3 → R2 → T2
Operating System Concepts – 10th Edition 8.9 Silberschatz, Galvin and Gagne ©2018
T1 → R1 → T3 → R2 → T1
Observe that:
• Thread T4 (holding one instance of R2) may
release its instance of resource type R2. That
resource can then be allocated to T3, breaking
the cycle => no deadlock
In summary
• If graph contains no cycles no deadlock
• If graph contains a cycle
If only one instance per resource type, then
deadlock
If several instances per resource type,
possibility of deadlock
=> This observation is important when we deal
with the deadlock problem.
Operating System Concepts – 10th Edition 8.10 Silberschatz, Galvin and Gagne ©2018
5
Methods for Handling Deadlocks
Ensure that the system will never enter a deadlock state:
• Deadlock prevention
Mutual Exclusion: One or more than one resource are non-shareable (Only
one process can use at a time)
Hold and Wait: A process is holding at least one resource and waiting for
resources.
No Preemption: A resource cannot be taken from a process unless the
process releases the resource.
Circular Wait: A set of processes are waiting for each other in circular form.
• Deadlock avoidance
Allow the system to enter a deadlock state and then recover
Ignore the problem and pretend that deadlocks never occur in the system.
• What is the consequence?
Operating System Concepts – 10th Edition 8.11 Silberschatz, Galvin and Gagne ©2018
Deadlock Prevention
Invalidate one of the four necessary conditions for deadlock:
Mutual Exclusion –
• sharable resources (e.g., read-only files): not required
• non-sharable resources (e.g: printer): do not
Hold and Wait – must guarantee that whenever a process requests a resource,
it does not hold any other resources
• Require processes to request and be allocated all its resources before it
begins execution, or
• Allow processes to request resources only when the process has none
allocated to it.
• Limit: Low resource utilization; starvation possible
Operating System Concepts – 10th Edition 8.12 Silberschatz, Galvin and Gagne ©2018
6
Deadlock Prevention (Cont.)
No Preemption:
• If a process that is holding some resources requests another resource that
cannot be immediately allocated to it, then all the resources currently being
held by the process are released
• Preempted resources are added to the list of resources for which the
process is waiting
• Process will be restarted only when it can regain its old resources, as well
as the new ones that it is requesting
Circular Wait:
• Impose a total ordering of all resource types, and require that each
process requests resources in an increasing order of enumeration
Operating System Concepts – 10th Edition 8.13 Silberschatz, Galvin and Gagne ©2018
Circular Wait
Cách 1: mỗi process yêu cầu thực thể của tài nguyên theo thứ tự tăng dần
(định nghĩa bởi hàm F) của loại tài nguyên. Ví dụ
• Chuỗi yêu cầu thực thể hợp lệ: tape drive -> disk drive -> printer
• Chuỗi yêu cầu thực thể không hợp lệ: disk drive -> tape drive
Cách 2: Khi một process yêu cầu một thực thể của loại tài nguyên Rj thì nó
phải trả lại các tài nguyên Ri với F(Ri) > F(Rj).
Ví dụ
“Chứng minh” cho cách 1: phản chứng
• P1: F(R4) < F(R1)
• P2: F(R1) < F(R2)
• P3: F(R2) < F(R3)
• P4: F(R3) < F(R4)
Vậy F(R4) < F(R4), mâu thuẫn!
Operating System Concepts – 10th Edition 8.14 Silberschatz, Galvin and Gagne ©2018
7
Circular Wait
Invalidating the circular wait condition is most commonly used.
Simply assign each resource (i.e., mutex locks) a unique number.
Resources must be acquired in order.
Example: two mutex locks
first_mutex = 1
second_mutex = 5
Operating System Concepts – 10th Edition 8.15 Silberschatz, Galvin and Gagne ©2018
Deadlock Avoidance
Requires that the system has some additional a priori information available
Simplest and most useful model requires that each process declare
the maximum number of resources of each type that it may need
The deadlock-avoidance algorithm dynamically examines the
resource-allocation state to ensure that there can never be a circular-
wait condition
Resource-allocation state is defined by the number of available and
allocated resources, and the maximum demands of the processes
Operating System Concepts – 10th Edition 8.16 Silberschatz, Galvin and Gagne ©2018
8
Safe State
When a process requests an available resource, system must decide if
immediate allocation leaves the system in a safe state
System is in safe state if there exists a sequence
P1, P2, …, Pn
of ALL the processes in the systems such that for each Pi, the resources
that Pi can still request can be satisfied by currently available resources +
resources held by all the Pj, with j < I
That is:
• If Pi resource needs are not immediately available, then Pi can wait
until all Pj have finished
• When Pj is finished, Pi can obtain needed resources, execute, return
allocated resources, and terminate
• When Pi terminates, Pi +1 can obtain its needed resources, and so on
Operating System Concepts – 10th Edition 8.17 Silberschatz, Galvin and Gagne ©2018
Safe State ex
Ví dụ: Hệ thống có 12 tape drive và 3 quá trình P0, P1, P2
Tại thời điểm t0, dữ liệu cho như bảng, Process Max - Alloca-
• => hệ thống còn 3 tape drive sẵn sàng need tion
(12-5-2-2). P0 10 5
• Sequence P1, P0, P2 : safe: P1 4 2
P1: get and return => avail: 3+2=5 P2 9 2
P0: get and return => avail: 5+5=10
P2: get and return =>avail: 10+2=12
system is in a safe state
Tại thời điểm t1, P2 yêu cầu và được cấp Process Max - Alloca-
phát 1 tape drive còn 2 tape drive sẵn sang need tion
P1: get and return => avail: 2+2=4 P0 10 5
P0: get 5: wait
P1 4 2
P2: get
P2 9 3
Sequence P1, P0, P2 : no safe
system is in a no safe state
Operating System Concepts – 10th Edition 8.18 Silberschatz, Galvin and Gagne ©2018
9
Safe, Unsafe, Deadlock State
If a system is in safe state no deadlocks
If a system is in unsafe state possibility of deadlock
Avoidance algorithm:
• Ensure that a system will never enter an unsafe state
Operating System Concepts – 10th Edition 8.19 Silberschatz, Galvin and Gagne ©2018
Avoidance Algorithms
Single instance of a resource type
• Use a modified resource-allocation graph
Multiple instances of a resource type
• Use the Banker’s Algorithm
Operating System Concepts – 10th Edition 8.20 Silberschatz, Galvin and Gagne ©2018
10
Modified Resource-Allocation Graph Scheme
Operating System Concepts – 10th Edition 8.21 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph
Operating System Concepts – 10th Edition 8.22 Silberschatz, Galvin and Gagne ©2018
11
Resource-Allocation Graph Algorithm
Suppose that process Pi requests a resource Rj
The request can be granted only if converting the request edge to an
assignment edge does not result in the formation of a cycle in the
resource allocation graph
Banker’s Algorithm
• Multiple instances of resources
• 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
Operating System Concepts – 10th Edition 8.23 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 8.24 Silberschatz, Galvin and Gagne ©2018
12
Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1
4. If Finish [i] == true for all i, then the system is in a safe state
Operating System Concepts – 10th Edition 8.25 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 8.26 Silberschatz, Galvin and Gagne ©2018
13
Expl: based on Need
Operating System Concepts – 10th Edition 8.27 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 8.28 Silberschatz, Galvin and Gagne ©2018
14
Example: P1 Request (1,0,2)
P1 Request (1,0,2): Check that Request Available (that is, (1,0,2) (3,3,2) true
Before:
Update: Work:
Pro_ A B C
cess 2 3 0
P1 5 3 2
P3 7 4 3
P4 7 4 5
P0 7 5 5
P2 10 5 7
Sequence < P1, P3, P4, P0, P2> satisfies safety requirement
• => R1 can be allocated resource
Operating System Concepts – 10th Edition 8.29 Silberschatz, Galvin and Gagne ©2018
Example:
Operating System Concepts – 10th Edition 8.30 Silberschatz, Galvin and Gagne ©2018
15
Deadlock Detection
Detection algorithm
Recovery scheme
Operating System Concepts – 10th Edition 8.31 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 8.32 Silberschatz, Galvin and Gagne ©2018
16
Resource-Allocation Graph and Wait-for Graph
Operating System Concepts – 10th Edition 8.33 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 8.34 Silberschatz, Galvin and Gagne ©2018
17
Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively Initialize:
a) Work = Available
b) For i = 1,2, …, n, if Allocationi 0, then
Finish[i] = false; otherwise, Finish[i] = true
Pro_ A B C
Sequence <P0, P2, P3, P4, P1> or
cess 0 0 0
Sequence <P0, P2, P3, P1, P4> P0 0 1 0
will result in Finish[i] = true for all I P2 3 1 3
=> The system is in safe
P3 5 2 4
P4 5 2 6
Operating System Concepts – 10th Edition 8.36 P1 7 2Silberschatz, Galvin
6 and Gagne ©2018
18
Example (Cont.)
P2 requests an additional instance of type C
State of system?
• Can reclaim resources held by process P0, but insufficient resources to
fulfill other processes; requests
• Deadlock exists, consisting of processes P1, P2, P3, and P4
Operating System Concepts – 10th Edition 8.37 Silberschatz, Galvin and Gagne ©2018
Detection-Algorithm Usage
When, and how often, to invoke depends on:
• How often a deadlock is likely to occur?
• How many processes will need to be rolled back?
One for each disjoint cycle
Operating System Concepts – 10th Edition 8.38 Silberschatz, Galvin and Gagne ©2018
19
When Deadlock
Operating System Concepts – 10th Edition 8.39 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 8.40 Silberschatz, Galvin and Gagne ©2018
20
Recovery from Deadlock: Resource Preemption
Operating System Concepts – 10th Edition 8.41 Silberschatz, Galvin and Gagne ©2018
End of Chapter 2
Operating System Concepts – 10h Edition Silberschatz, Galvin and Gagne ©2018
21