Lecture 6 OS

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 17

Chapter 7: Deadlocks

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 7: Deadlocks
 System Model
 Deadlock Characterization
 Methods for Handling Deadlocks
 Deadlock Prevention
 Deadlock Avoidance
 Deadlock Detection
 Recovery from Deadlock

Operating System Concepts – 9th Edition 7.2 Silberschatz, Galvin and Gagne ©2013
Chapter Objectives

 To develop a description of deadlocks, which prevent


sets of concurrent processes from completing their
tasks
 To present a number of different methods for
preventing or avoiding deadlocks in a computer
system

Operating System Concepts – 9th Edition 7.3 Silberschatz, Galvin and Gagne ©2013
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
 use
 release

Operating System Concepts – 9th Edition 7.4 Silberschatz, Galvin and Gagne ©2013
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 – 9th Edition 7.5 Silberschatz, Galvin and Gagne ©2013
Methods for Handling Deadlocks
 Ensure that the system will never enter a deadlock
state--Deadlock prevention
 Allow the system to enter a deadlock state and then
recover--Deadlock avoidence
 Ignore the problem and pretend that deadlocks never
occur in the system; used by most operating systems,
including UNIX

Operating System Concepts – 9th Edition 7.6 Silberschatz, Galvin and Gagne ©2013
Deadlock Prevention
Restrain the ways request can be made

 Mutual Exclusion – must not hold for sharable resources


 Hold and Wait – must guarantee that whenever a process
requests a resource, it does not hold any other resources
 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.

Operating System Concepts – 9th Edition 7.7 Silberschatz, Galvin and Gagne ©2013
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 resources currently being held are released
 Preempted resources are added to the list of resources
for which the process is waiting
 Process will be restarted to regain its old resources, as
well as the new ones that it is requesting
 Circular Wait – impose a total ordering of all resource types

Operating System Concepts – 9th Edition 7.8 Silberschatz, Galvin and Gagne ©2013
Deadlock Avoidance
Requires that the the operating system have a priori information about
how each process will utilize system resources
 When a process requests an available resource, system
must decide if immediate allocation leaves the system in a
safe state
 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 – 9th Edition 7.9 Silberschatz, Galvin and Gagne ©2013
Basic Facts

 If a system is in safe state  no deadlocks

 If a system is in unsafe state  possibility of deadlock

 Avoidance  ensure that a system will never enter an


unsafe state.

Operating System Concepts – 9th Edition 7.10 Silberschatz, Galvin and Gagne ©2013
Avoidance Algorithms
 Single instance of a resource type
 Use a resource-allocation graph

 Multiple instances of a resource type


 Use the banker’s algorithm

• Allow system to enter deadlock state, detect the


deadlock, and recovery scheme

Operating System Concepts – 9th Edition 7.11 Silberschatz, Galvin and Gagne ©2013
Detection-Algorithm Usage
 When, and how often, to invoke depends on:
 How often a deadlock is likely to occur?

 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.

Recovery from Deadlock: Resource Preemption


 Selecting a victim – minimize cost
 Rollback – return to some safe state, restart process for that
state

Operating System Concepts – 9th Edition 7.12 Silberschatz, Galvin and Gagne ©2013
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 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 – 9th Edition 7.13 Silberschatz, Galvin and Gagne ©2013
Summary
 If a system does not employ a protocol to ensure that deadlocks will never
occur, then a detection-and-recovery scheme may be employed. A dead
lock detection algorithm must be invoked to determine whether a deadlock
has occurred.
 If a deadlock is detected, the system must recover either by terminating
some of the deadlocked processes or by preempting resources from some
of the deadlocked processes.
 A dead lock can occur only if four necessary conditions hold simultaneously
in the system: mutual exclusion, hold and wait, no preemption, and circular
wait.
 To prevent deadlocks, we can ensure that at least one of the necessary
conditions never holds.
 A dead locked state occurs when two or more processes are waiting
indefinitely

Operating System Concepts – 9th Edition 7.14 Silberschatz, Galvin and Gagne ©2013
Summary
 For an event that can be caused only by one of the waiting processes.
There are three principal methods for dealing with deadlocks:

1. Use some protocol to prevent or avoid deadlocks, ensuring that the system
will never enter a deadlocked state.
2. Allow the system to enter a deadlocked state, detect it, and then recover.
3. Ignore the problem altogether and pretend that deadlocks never occur in the
system.

Operating System Concepts – 9th Edition 7.15 Silberschatz, Galvin and Gagne ©2013
Practice Exercises
 If a system does not employ a protocol to ensure that deadlocks will never
occur, what will be employed?

 A dead lock can occur only if four necessary conditions hold simultaneously
in the system, state the conditions.

 Write three principal methods for dealing with deadlocks

Operating System Concepts – 9th Edition 7.16 Silberschatz, Galvin and Gagne ©2013
End of Chapter 7

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013

You might also like