Академический Документы
Профессиональный Документы
Культура Документы
Operating System
B. Tech.
Delhi Technological university
Deadlocks
Instructor: Divyashikha Sethia
Deadlocks
a) The Deadlock Problem
b) System Model
c) Deadlock Characterization
d) Methods for Handling Deadlocks
e) Deadlock Prevention
f) Deadlock Avoidance
g) Deadlock Detection
h) Recovery from Deadlock
1
2/18/2020
Chapter Objectives
a) To develop a description of deadlocks, which prevent
sets of concurrent processes from completing their tasks
2
2/18/2020
System Model
• System may consists of finite number of resources to be distributed among
number of competing processes. Resources are partitioned into several
types, each consisting of some number of identical instances.
• Memory space, CPU cycles, files, and I/O devices (such as printers and
DVD drives) are examples of resources types.
• If system has two CPUs, then instances of CPU are two.
• Process must request resource before using it and must release resource
after using it.
• Process may request as many resources as required for a task.
• Number of resources requested may not exceed the total number of
resources available in the system. i.e process cannot request three printers
if the system has only two.
3
2/18/2020
Deadlock Characterization
In a deadlock, processes never finish executing, and system resources are tied
up preventing other jobs from starting.
Now, we look more closely at features that characterize deadlocks;
Necessary Conditions: A deadlock situation can arise if the following four
conditions hold simultaneously in a system:
a) Mutual exclusion: At least one resource must be held in non-sharable
mode - only one process at a time can use a resource. If another process
requests that resource, requesting process must be delayed until resource
has been released.
b) Hold and wait: Process must be holding at least one resource and is
waiting to acquire additional resources that are currently being held by other
processes.
4
2/18/2020
Resource-Allocation Graph
Deadlocks can be described more precisely in terms of a directed graph called a
system resource-allocation graph. This graph consists of a set of vertices V
and a set of edges E;
a) The set of vertices V is partitioned into two different types of nodes:
– 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.
b) Requested edge: Directed edge from process Pi to resource type Rj Pi Rj;
- signifies process Pi has requested an instance of resource type Rj and is
currently waiting for that resource.
a) Assignment edge: Directed edge from resource type Rj to process Pi Rj Pi;
- signifies that instance of resource type Rj has been allocated to process Pi.
a) Pictorially, represent each process Pi as a circle and each resource type Rj as
rectangle. Since resource type Rj may have more than one instance, we
represent each such instance as a dot within a rectangle. Note that a request
edge points to only the rectangle Rj, whereas an assignment edge must also
designate one of the dots in the rectangle.
5
2/18/2020
g) Pi requests instance of Rj
Pi
Rj
h) Pi is holding an instance of Rj
Pi
Rj
6
2/18/2020
Basic Facts
With the help of following facts, we can detect a deadlock within a
given system;
a) If the system resource-allocation graph contains no cycles
no deadlock .
b) If graph contains a cycle
– if only one instance per resource type, then deadlock.
– if several instances per resource type, then there is a
possibility of deadlock.
7
2/18/2020
b) We can allow the system to enter a deadlock state, detect it, and then
recover.
c) We can ignore the problem altogether and pretend that deadlocks never
occur in the system.
The third solution is the one used by most operating systems, including UNIX
and Windows. – Application developers need to handle deadlocks
8
2/18/2020
Deadlock Prevention
Deadlock prevention provides a set of methods for ensuring that at least one of
the necessary conditions cannot hold. These methods prevent deadlocks by
constraining how requests for resources can be made.
Now, we briefly discuss these methods;
a) Mutual Exclusion
- Condition must hold for non-sharable resources eg:printer cannot be simultaneously
shared by several processes.
- Sharable resources, do not require mutually exclusive access and thus cannot be
involved in a deadlock. Read only files are a good example of a sharable resource. If
several processes attempt to open a read-only file at the same time, they can be
granted simultaneous access to the file.
9
2/18/2020
10
2/18/2020
Similar to the difference between a traffic light and a police officer directing traffic.
Deadlock Avoidance
Requires OS be given in advance additional information concerning which
resources a process will request and use.
OS decides for each request whether or not process should wait.
Must consider resources currently available, resources currently allocated to
each process, and the future requests and releases of each process.
Simple Model:
•Each process must declare maximum num of resources of each type that it may
need.
• Algorithm dynamically examines resource-allocation state to ensure that a
circular-wait condition can never exists
•Resource-allocation state is defined by number of available and allocated
resources and maximum demands of the processes.
Two deadlock avoidance algorithms:
1.Safe state;
2.Resource-Allocation-Graph Algorithm.
11
2/18/2020
Safe State
A state is safe if system can allocate resources to each process (up to its
maximum) in some order and still avoid a deadlock.
System is in safe state only if there exists a safe sequence.
A safe sequence of processes < P1, P2,…, Pn > is a safe sequence for the current
allocation state if, for each Pi, resource requests that Pi can still make can be
satisfied by the currently available resource plus the resources held by all Pj,
with j<i.
If resources that Pi needs are not immediately available ,then Pi can wait until
all Pj has finished.
When they have finished, Pi can obtain all of its needed resources, complete its
designated task, return its allocated resources and terminate
When Pi terminate , Pi+1 can obtain its needed resources, and so on.
If no such sequence exists, then system state is said to be unsafe.
Conversely, a deadlocked state is an unsafe state, but not all unsafe states are deadlocks.
12
2/18/2020
25
13
2/18/2020
Basic Facts
•An unsafe state may lead to a deadlock.
•As long as the state is safe, the operating system can avoid unsafe (and
deadlocked) states.
•In an unsafe state, the operating system cannot prevent processes from
requesting resources such that a deadlock occurs: The behavior of the
processes controls unsafe states.
•Claim edge Pi -- Rj converts to request edge, when a process Pi requests a resource
Rj.
•And we must note that, resources must be claimed a priori in the system. (Before Pi
starts executing, all claim edges must appear in resource allocation graph. We can add
claim edge to process Pi if and only if all edges from Pi are claim edges
14
2/18/2020
Banker’s Algorithm
The resource-allocation graph algorithm is not applicable to a resource-allocation
system with multiple instances of each resource type.
Banker’s algorithm:
- deadlock-avoidance algorithm is applicable to system with multiple resources but is
less efficient than the resource-allocation graph scheme.
Why called bankers algo: Algo could be used in banking system to make sure bank
never allocated its cash in such a way that it could always satisfy needs of customer.
a)Multiple instances of each resource type.
b)Each process must a priori claim maximum use which must be less than number of
system resources
c)When a process requests a resource it may have to wait since system would determine
if it will be in safe state.
d)When a process gets all its resources it must return them in a finite amount of time.
15
2/18/2020
Vector notation used: X and Y vectors of length n X <= Y if for all I X[i] <= Y[i]
X =(1,7,3,2) Y=(0,3,2,1)
Safety Algorithm
Algorithm for finding out whether or not a system is in a safe state.
1. Let Work and Finish be vectors of length m and n, respectively. Initialize:
Work = Available
Finish [i] = false for i =0,1,2,3, …, n-1.
2. Find and 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.
16
2/18/2020
17
2/18/2020
Example (Cont.)
c) The content of the matrix. Need is defined to be Max
– Allocation.
Need
ABC
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
18
2/18/2020
f) Assume Request has been fulfilled and new state arrived is:
( no actual allocation has been done just testing)
Allocation Need Available
ABC ABC ABC
P0 0 1 0 743 230
P1 3 0 2 020
P2 3 0 1 600
P3 2 1 1 011
P4 0 0 2 431
19
2/18/2020
Deadlock Detection
If a system does not employ either a deadlock-prevention or a deadlock-
avoidance algorithm, then a deadlock situation may occur. In this
environment the system must provide :
• An algorithm that examines the state of the system to determine
whether a deadlock has occurred.
• An algorithm to recover from the deadlock.
If all resources have only a single instance, then we can define a deadlock-detection
algorithm that uses a variant of resource-allocation graph, called a wait-for graph.
We obtain this graph from the resource-allocation graph by removing the resource
nodes and collapsing the appropriate edges.
In this method, we perform the following steps;
a) Maintain wait-for graph
– Nodes are processes.
– Pi Pj if Pi is waiting for Pj.
b) Periodically, invoke an algorithm that searches for a cycle in the graph, because
if the wait-for graph contains a cycle, a deadlock exists in the system.
20
2/18/2020
21
2/18/2020
Detection Algorithm
The detection algorithm described here simply investigates every possible allocation
sequence for the processes that remain to be completed. The algorithm has the
following steps:
Step 3 assume Pi uses the Request(i) since it is <= Work and hence is not involved in a
deadlock . We assume optimistically that it will not require more resources and
will release the resources back hence Work is incremented by what had been
Allocated to process i
This algorithm requires an order of O(m x n2) operations to detect whether the
system is in deadlocked state.
22
2/18/2020
Example (Cont.)
d) If P2 requests an additional instance of type C.
Allocation Request Available Work
ABC ABC ABC 000
P0 0 1 0 000 000 0 1 0 (i)
P1 2 0 0 202
P2 3 0 3 0 01
P3 2 1 1 100
P4 0 0 2 002
e) 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.
23
2/18/2020
Detection-Algorithm Usage
When should we invoke detection algorithm?
Two factors:
i) How often a deadlock is likely to occur?
ii) How many processes will be affected by deadlock when it happens?
• If deadlocks occur frequently:
- Detection algorithm should be invoked frequently.
- Resources allocated to deadlocked processes will be idle until deadlock can be
broken.
- Number of processes involved in the deadlock cycle may grow.
Detection-Algorithm Usage…
• Deadlocks occur when a process makes request which cannot be granted.
• Could invoke deadlock detection every time resources cannot be allocated – can
identify deadlocked processes and processes that caused deadlock
24
2/18/2020
c) Starvation: How do we ensure that starvation will not occur? That is, how can we
guarantee that resources will not always be preempted from the same process?
Possible that same process is selected for resource preemption
Cost factor to be used: Number of rollbacks for a process
25
2/18/2020
THANKS
26