0% found this document useful (0 votes)
5 views26 pages

ch8 - Deadlocks - 2022

Chapter 8 discusses deadlocks in operating systems, defining the conditions under which they occur and methods for handling them. It covers deadlock prevention, avoidance, detection, and recovery strategies, emphasizing the importance of resource allocation and management. The chapter illustrates concepts with examples, such as resource allocation graphs and semaphore usage.

Uploaded by

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

ch8 - Deadlocks - 2022

Chapter 8 discusses deadlocks in operating systems, defining the conditions under which they occur and methods for handling them. It covers deadlock prevention, avoidance, detection, and recovery strategies, emphasizing the importance of resource allocation and management. The chapter illustrates concepts with examples, such as resource allocation graphs and semaphore usage.

Uploaded by

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

Chapter 8: Deadlocks

Operating System Concepts – 10th 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
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
 Evaluate approaches for recovering from deadlock

Operating System Concepts – 10th Edition 8.3 Silberschatz, Galvin and Gagne ©2018
The Deadlock Problem
 If a resource requested by a process is unavailable, the process enters
a wait state. If the waiting processes never again change state because
the resources they request are held by other waiting processes, we
have a deadlock.
 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 disk drives
• P1 and P2 each hold one disk drive and each needs another one

Operating System Concepts – 10th Edition 8.4 Silberschatz, Galvin and Gagne ©2018
Bridge Crossing Example

 Traffic only in one direction


 Each section of a bridge can be viewed as a resource
 If a deadlock occurs, it can be resolved if one car backs up (preempt
resources and rollback)
 Several cars may have to be backed up if a deadlock occurs
 Starvation is possible
 Note – Most OSs do not prevent or deal with deadlocks

Operating System Concepts – 10th Edition 8.5 Silberschatz, Galvin and Gagne ©2018
System Model
 A system consists of a finite number of resources to be distributed among a
number of competing threads
 These resource can be of different types R1, R2, . . ., Rm
• e.g. CPU cycles, memory space, I/O devices
 Each resource type Ri has number of identical instances Wi .
 Each process/thread utilizes a resource as follows:
1. request
2. use
3. release

 The request and release of resources may be system calls. For example:
• request() and release() of a device,
• open() and close() of a file,
• allocate() and free() memory system calls.
• wait() and signal() operations on semaphores,
• acquire() and release() of a mutex lock

Operating System Concepts – 10th Edition 8.6 Silberschatz, Galvin and Gagne ©2018
Deadlock 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.7 Silberschatz, Galvin and Gagne ©2018
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously in a system:

 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: Resources cannot be pre-empted. 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.8 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 = {T1, T2, …, Tn}, the set consisting of all the active
threads in the system

• R = {R1, R2, …, Rm}, the set consisting of all resource


types in the system

 request edge – directed edge Ti  Rj

 assignment edge – directed edge Rj  Ti

Operating System Concepts – 10th Edition 8.9 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph (Cont.)
 Process/ Thread

 Resource Type with 4 instances

 Ti requests instance of Rj

Ti

 Ti is holding an instance of Rj

Ti

Operating System Concepts – 10th Edition 8.10 Silberschatz, Galvin and Gagne ©2018
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 is holds one instance of R3

R = {R1, R2, R3, R4}


E = {T1 → R1, T2 → R3, R1 → T2, R2 → T2, R2 → T1, R3 → T3}

Operating System Concepts – 10th Edition 8.11 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph with a Deadlock

 T1 is holding an instance of
resource type R2 and is
waiting for an instance of
resource type R1.

 T2 is holding an instance of
R1 and an instance of R2 and
is waiting for an instance of R3.

 T3 is holding an instance of
R3 and is waiting for an
instance of R2.

 2 cycles:
• T1 → R1 → T2 → R3 → T3 → R2 →
T1
• T2 → R3 → T3 → R2 → T2
Operating System Concepts – 10th Edition 8.12 Silberschatz, Galvin and Gagne ©2018
Graph with a Cycle But no Deadlock

 If graph contains no cycles 


no deadlock
 If graph contains a cycle 
1. if only one instance per
resource type, then
deadlock
2. if several instances per
resource type,
possibility of deadlock

Operating System Concepts – 10th Edition 8.13 Silberschatz, Galvin and Gagne ©2018
Basic Facts
 If graph contains no cycles  no deadlock
 If graph contains a cycle 
1. if only one instance per resource type, then deadlock
2. if several instances per resource type, possibility of deadlock

Operating System Concepts – 10th Edition 8.14 Silberschatz, Galvin and Gagne ©2018
Methods for Handling Deadlocks
Generally, we can deal with the deadlock problem in one of three
ways:

1. We can ignore the problem altogether and pretend that deadlocks


never occur in the system.

2. We can use a protocol to prevent or avoid deadlocks, ensuring that


the system will never enter a deadlocked state.
• Deadlock prevention (provides a set of methods to ensure that at
least one of the necessary conditions cannot hold. )
• Deadlock avoidance (requires that the OS be given additional
information in advance concerning which resources a thread will request
and use during its lifetime)

3. We can allow the system to enter a deadlocked state, detect it, and
recover (Deadlock detection)

Operating System Concepts – 10th Edition 8.15 Silberschatz, Galvin and Gagne ©2018
Deadlock Prevention
Invalidate one of the four necessary conditions for deadlock:

 Mutual Exclusion – not required for sharable resources (e.g., read-


only files); must hold for non-sharable resources
• The non-sharable resource here reflect the resource that can't be
accessed by more than one process at a time.

 Hold and Wait – To ensure that this condition never occurs in the
system, we must guarantee that whenever a process requests a resource,
it does not hold any other resources
 How?
• Require process to request and be allocated all its resources before
it begins execution.
• or allow process to request resources only when the process has
none allocated to it. (the process can’t take another resource
until it leaves the current resource)
In both protocols, we may get low resource utilization and
starvation is possible

Operating System Concepts – 10th Edition 8.16 Silberschatz, Galvin and Gagne ©2018
Deadlock Prevention (Cont.)
 No Preemption:
• To ensure that this condition does not hold, we can use the following
protocol:
1. If a process that is holding some resources requests another resource that
cannot be immediately allocated to it (i.e. it must wait), then all resources
currently being held are released (preempted)
2. Preempted resources are added to the list of resources for which the
process is waiting
3. 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.17 Silberschatz, Galvin and Gagne ©2018
Deadlock Prevention (Cont.)
 Circular Wait (cont.)
• We let R = {R1, R2, ..., Rm} be the set of resource
types.
• Each resource type is assigned a unique integer
number, which allows us to compare two resources and
to determine whether one precedes another in our
ordering.
• We define a one-to-one function F: R→N, where N is
the set of natural numbers

 Example:
 F(tape drive) = 1 , F(disk drive) = 5, F(printer) = 12
 A process can initially request any number of instances of
a resource type Ri and now sending the request for Rj, the
resource will be assigned if and only if F(Rj) > F(Ri).
Side effects of deadlock prevention:
low device utilization and reduced system throughput.

Operating System Concepts – 10th Edition 8.18 Silberschatz, Galvin and Gagne ©2018
Deadlock Avoidance
 Look at every single resource request and decide whether to allow that
request to process.
 The action will be taken before occurring deadlock.

 Requires that the system has some additional a priori information


available

 Simplest and most useful model requires that each process to


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 a circular-wait condition can
never exist
 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.19 Silberschatz, Galvin and Gagne ©2018
Deadlock Detection
 Allow system to enter deadlock state

 Detection algorithm

 Recovery scheme

Operating System Concepts – 10th Edition 8.20 Silberschatz, Galvin and Gagne ©2018
Single Instance of Each Resource Type
 As we know, a deadlock exists in the system if and only if the wait-for
graph contains a cycle.
 To detect deadlocks, the system needs to:

1. Maintain wait-for graph


 Nodes are processes
 Pi  Pj if Pi is waiting for Pj

2. 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.21 Silberschatz, Galvin and Gagne ©2018
Resource-Allocation Graph and Wait-for Graph

(a) Resource-Allocation Graph (b) Corresponding wait-for graph

Operating System Concepts – 10th Edition 8.22 Silberschatz, Galvin and Gagne ©2018
Detection-Algorithm Usage
 When, and how often, to invoke detection algorithm depends on:
1. How often is a deadlock likely to occur?
2. How many threads will be affected by deadlock
when it happens?
(i.e. 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.23 Silberschatz, Galvin and Gagne ©2018
Recovery from Deadlock: Process/Thread Termination

 To eliminate deadlocks by aborting a process or thread, we use one of two


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

 In which order should we choose to abort?


1. Priority of the process
2. How long process has computed, and how much longer to completion
3. Resources the process has used
4. Resources process needs to complete
5. How many processes will need to be terminated
6. Is process interactive or batch?

Operating System Concepts – 10th Edition 8.24 Silberschatz, Galvin and Gagne ©2018
Recovery from Deadlock: Resource Preemption

 To eliminate deadlocks using resource preemption, we successively preempt


some resources from processes and give these resources to other processes
until the deadlock cycle is broken.
 If preemption is required to deal with deadlocks, then three issues need to be
addressed:

• Selecting a victim – Which resources and which processes are to be


preempted? As in process termination, we must determine the order of
preemption to minimize cost

• Rollback – If we preempt a resource from a process, what should be done


with that process? return to some safe state, restart process from that
state

• Starvation – same process may always be picked as a victim.


 we must ensure that a process can be picked as a victim only a (small)
finite number of times. The most common solution is to include the
number of rollbacks in the cost factor.

Operating System Concepts – 10th Edition 8.25 Silberschatz, Galvin and Gagne ©2018
End of Chapter 8

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy