[go: up one dir, main page]

0% found this document useful (0 votes)
50 views21 pages

ch02.5 - Deadlock

The document discusses deadlocks in operating systems. It defines the four conditions required for deadlock, describes methods to prevent deadlocks like mutual exclusion and limiting resource requests, and provides examples of deadlock situations and their representation using resource allocation graphs.

Uploaded by

22110187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views21 pages

ch02.5 - Deadlock

The document discusses deadlocks in operating systems. It defines the four conditions required for deadlock, describes methods to prevent deadlocks like mutual exclusion and limiting resource requests, and provides examples of deadlock situations and their representation using resource allocation graphs.

Uploaded by

22110187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Chapter 2: Process Synchronization

2.5: Deadlocks

GV: Nguyễn Thị Thanh Vân


Operating System Concepts – 10h Edition Silberschatz, Galvin and Gagne ©2018

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

Deadlock Example on a one-way Bridge

Operating System Concepts – 10th Edition 8.6 Silberschatz, Galvin and Gagne ©2018

3
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.

 Mutual exclusion: only one process at a time can use a resource


 Hold and wait: a process holding at least one resource is waiting to acquire
additional resources held by other processes
 No preemption: a resource can be released only voluntarily by the process
holding it, after that process has completed its task
 Circular wait: there exists a set {P0, P1, …, Pn} of waiting processes such that
P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is
held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is
waiting for a resource that is held by P0.

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

 request edge – directed edge Pi  Rj


 assignment edge – directed edge Rj  Pi

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

Graph with a Cycle But no Deadlock

 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

code for thread_two could not be


written as follows:

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

 Claim edge Pi ----> Rj indicates that process Pi may request resource Rj

 Request edge Pi  Rj indicates that process Pi requests resource Rj


• Claim edge converts to request edge when a process requests a
resource
 Assignment edge Rj  Pi indicates that resource Rj was allocated to
process Pi
• Request edge converts to an assignment edge when the resource is
allocated to the process
 When a resource is released by a process, assignment edge reconverts to a
claim edge
 Resources must be claimed a priori in the system

Operating System Concepts – 10th Edition 8.21 Silberschatz, Galvin and Gagne ©2018

Resource-Allocation Graph

 Modified Resource-Allocation Graph Example

 Unsafe State In 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

Data Structures for the Banker’s Algorithm

Let n = number of processes, and m = number of resources types.


 Available: Vector of length m. If available [j] = k, there are k instances of
resource type Rj available

 Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k


instances of resource type Rj

 Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k


instances of Rj

 Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj


to complete its task

Need [i,j] = Max[i,j] – Allocation [i,j]

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

2. Find an i such that both:


(a) Finish [i] = false
(b) Needi  Work
If no such i exists, go to step 4

3. Work = Work + Allocationi


Finish[i] = true
go to step 2

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

Example of Banker’s Algorithm


 5 processes P0 through P4;
 3 resource types: A (10 instances), B (5 instances), and C (7 instances)
 Snapshot at time T0:

Need [i,j] = Max[i,j] – Allocation [i,j]

The system is in a safe state since


the sequence < P1, P3, P4, P0, P2 >
satisfies safety criteria

Operating System Concepts – 10th Edition 8.26 Silberschatz, Galvin and Gagne ©2018

13
Expl: based on Need

Tính toán an toàn dựa vào ma trận Need:


 Khởi tạo work=avail => Work(3,3,2)
 Nếu Need <=Work: Work= Work+Allo (so sánh với Work cập nhật mới nhất theo Pi)
• P0: Need_P0(7,4,3) > Work(3,3,2) => ko update Work
• P1: Need_P1(1,2,2) < Work(3,3,2) =>Work = Work(3,3,2)+Allo_P1(2,0,0) =>update: Work(5,3,2)
• P2: Need_P2(6,0,0) > Work(5,3,2) => ko update Work
• P3: Need_P3(0,1,1) < Work(5,3,2) => Work= Work(5,3,2) + Allo_P3(2,1,1) => update Work(7,4,3)
• P4: Need_P4(4,3,1) < Work(7,4,3) => Work= Work(7,4,3) + Allo_P4(0,0,2) => update Work(7,4,5)
 Còn P0, P2 chưa cấp phát. Finish = True thì quay lại xem xét cấp phát lại (Step 2)
• P0: Need_P0(7,4,3) < Work(7,4,5) => Work= Work(7,4,5) + Allo_P0(0,1,0) => update Work(7,5,5)
• P2: Need_P2(6,0,0) < Work(7,5,5) => Work= Work(7,5,5) + Allo_P0(3,0,2) => update Work(10,5,7)
 Hệ thống trên ở trạng thái an toàn vì nó tồn tại thứ tự an toàn P1,3,4,0,2
 Cách check:
• So sánh Work ở trạng thái cuối với đề ra có bao nhiêu instant, nếu khớp thì OK
• Nếu ko cho instant ban đầu thì so Work ở trạng thái cuối với tổng từng kiểu instant ở các Process
+ instant ở Available, nếu khớp thì OK

Operating System Concepts – 10th Edition 8.27 Silberschatz, Galvin and Gagne ©2018

Resource-Request Algorithm for Process Pi

Requesti = request vector for process Pi.


If Requesti [j] = k then process Pi wants k instances of resource type Rj
1. If Requesti  Needi go to step 2. Otherwise, raise error condition, since
process has exceeded its maximum claim
2. If Requesti  Available, go to step 3. Otherwise Pi must wait, since
resources are not available
3. Pretend to allocate requested resources to Pi by modifying the state as
follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
• If safe  the resources are allocated to Pi
• If unsafe  Pi must wait, and the old resource-allocation state is restored
Available = Available + Requesti;
Allocationi = Allocationi - Requesti;
Needi = Needi + Requesti;

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:

Avail = Avail – Requesti;


Alloci = Alloci + Requesti;
Needi = Needi – Requesti;

 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:

 Can request for (3,3,0) by P4 be granted?

 Can request for (0,2,0) by P0 be granted?

Operating System Concepts – 10th Edition 8.30 Silberschatz, Galvin and Gagne ©2018

15
Deadlock Detection

 Allow system to enter deadlock state

 Detection algorithm

 Recovery scheme

Operating System Concepts – 10th Edition 8.31 Silberschatz, Galvin and Gagne ©2018

Single Instance of Each Resource Type

 a deadlock detection algorithm that uses a variant of the resource-


allocation graph, called wait-for graph
• Nodes are processes
• Pi  Pj if Pi is waiting for Pj to release a resource Pi needs
We obtain this graph from the resource-allocation graph by removing the
resource nodes and collapsing the appropriate edges
 To detect deadlocks:
• Maintain the wait-for graph
• Periodically invoke an algorithm that searches for a cycle in the
graph. If there is a cycle, there exists a deadlock

 An algorithm to detect a cycle in a graph requires an order of n2


operations, where n is the number of vertices in the graph

Operating System Concepts – 10th Edition 8.32 Silberschatz, Galvin and Gagne ©2018

16
Resource-Allocation Graph and Wait-for Graph

Resource-Allocation Graph Corresponding wait-for graph

Operating System Concepts – 10th Edition 8.33 Silberschatz, Galvin and Gagne ©2018

Several Instances of a Resource Type

 Available: A vector of length m indicates the number of available


resources of each type
 Allocation: An n x m matrix defines the number of resources of each
type currently allocated to each process
 Request: An n x m matrix indicates the current request of each
process.
• If Request [i][j] = k, then process Pi is requesting k more instances
of resource type Rj.

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

2. Find an index i such that both:


a) Finish[i] == false
b) Requesti  Work
If no such i exists, go to step 4
3. Work = Work + Allocationi
Finish[i] = true
go to step 2

4. If Finish[i] == false, for some i, 1  i  n, then the system is in deadlock


state. Moreover, if Finish[i] == false, then Pi is deadlocked
If Finish[i] == true, then the system is in safe
Algorithm requires an order of O(m x n2) operations to detect whether the
system is in deadlocked state
Operating System Concepts – 10th Edition 8.35 Silberschatz, Galvin and Gagne ©2018

Example of Detection Algorithm


 Five processes P0 through P4; three resource types
A (7 instances), B (2 instances), and C (6 instances)
 Snapshot at time T0:

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

 If detection 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.

Operating System Concepts – 10th Edition 8.38 Silberschatz, Galvin and Gagne ©2018

19
When Deadlock

 When a detection algorithm determines that a deadlock exists, several


alternatives are available.
• One possibility is to inform the operator
• Another possibility is to let the system recover from the deadlock
automatically.

 recover from breaking a deadlock


• Abort all deadlocked processes
• Abort one process at a time until the deadlock cycle is eliminated

Operating System Concepts – 10th Edition 8.39 Silberschatz, Galvin and Gagne ©2018

Recovery from Deadlock: Process Termination

 In which order should we choose to abort?


• Priority of the process
• How long process has computed, and how much longer to completion
• Resources the process has used
• Resources process needs to complete
• How many processes will need to be terminated
• Is process interactive or batch?

Operating System Concepts – 10th Edition 8.40 Silberschatz, Galvin and Gagne ©2018

20
Recovery from Deadlock: Resource Preemption

 Selecting a victim – minimize cost


 Rollback – return to some safe state, restart process for that state
 Starvation – same process may always be picked as victim, include
number of rollback in cost factor

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

You might also like