Download Deadlocks: Prevention, Detection, and Recovery and more Slides Computer Science in PDF only on Docsity!
Deadlocks
Chapter 7
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.
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.
System Model
• Resource types R 1 , R 2 ,.. ., R m
CPU cycles, memory space, I/O devices
• Each resource type R i has W i instances.
• Each process utilizes a resource as follows:
Resource-Allocation Graph
A set of vertices V and a set of edges E.
- V is partitioned into two types:
- P = { P 1 , P 2 , …, P (^) n }, the set consisting of all the processes in the system.
- R = { R 1 , R 2 , …, Rm }, the set consisting of all resource types in the system.
- request edge – directed edge P 1 → R (^) j
- assignment edge – directed edge R (^) j → Pi
Resource-Allocation Graph (Cont.)
- Process
- Resource Type with 4 instances
- P (^) i requests instance of Rj
- P (^) i is holding an instance of R (^) j
Pi
Pi Rj
Rj
Resource Allocation Graph With A
Deadlock
Graph With A Cycle But No Deadlock
Methods for Handling Deadlocks
• Ensure that the system will never enter a
deadlock state.
• Allow the system to enter a deadlock state
and then recover.
• Ignore the problem and pretend that
deadlocks never occur in the system; used by
most operating systems, including UNIX.
Deadlock Prevention
Restrain the ways request can be made.
Make sure at least one of the four conditions for deadlock
cannot hold:
- Mutual Exclusion – not required for sharable resources; must hold for non-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. - Low resource utilization; starvation possible.
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.
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 < P 1 , P 2 , …, P (^) n > of all the processes in the systems such that for each P (^) i , the resources that P (^) i can still request can be satisfied by currently available resources + resources held by all the P (^) j , with j < i.
- That is:
- If P (^) i resource needs are not immediately available, then P (^) i can wait until all P (^) j have finished.
- When P (^) j is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate.
- When P (^) i terminates, P (^) i +1 can obtain its needed resources, and so on.
Safe, Unsafe , Deadlock State
Example
Suppose a system has 12 tape drives and 3 processes.
At time t0, the system is as follows:
Process Max need Current allocated P0 10 5 P1 4 2 P2 9 2
3 tape drives are unallocated.
Is the system safe?
Suppose process P2 is allocated another tape drive at time t1.
Is the system safe?