0% found this document useful (0 votes)
20 views

Lecture Handling Deadlock

Uploaded by

Nishanth M.S
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)
20 views

Lecture Handling Deadlock

Uploaded by

Nishanth M.S
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/ 69

BITS, PILANI – K. K.

BIRLA GOA CAMPUS

Operating Systems
CS F372
by

Dr. Shubhangi
Dept. of CS and IS

27 November 2021 1
OPERATING SYSTEMS

LECTURE
HANDLING DEADLOCKS

27 November 2021 2
OS as a Resource Manager
 Processes compete for finite number of
resources

 Resources
 Memory space
 CPU Cycles
 Files
 I/O Devices

 Instances
27 November 2021 3
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 OSes do not prevent or deal with
deadlocks
27 November 2021 4
27 November 2021 5
Examples
 Example 1
 System has 2 disk drives
 P1 and P2 each hold one disk drive and each needs another one

 Example 2
 System has 1 disk drive and 1 CD drive
 P1 and P2 each hold one resource and each needs another one

 Example 3
 Multithreading
Thread1 Thread2
Lock(data item1) Lock(data item2)
Lock(data item2) Lock(data item1)
. .
. .
UnLock(data item2) UnLock(data item1)
UnLock(data item1) UnLock(data item2)
27 November 2021 6
Example of Deadlock
 Space is available for allocation of
200Kbytes, and the following sequence of
events occur
P1 P2
... ...
Request 80 Kbytes; Request 70 Kbytes;
... ...
Request 60 Kbytes; Request 80 Kbytes;

 Deadlock occurs if both processes progress


to their second request
27 November 2021 7
The Deadlock Problem
 A set of blocked processes each holding a
resource and waiting to acquire a resource
held by another process in the set
 Permanent blocking of a set of processes that
either compete for system resources or
communicate with each other
 No efficient solution
 Involve conflicting needs for resources by two
or more processes
27 November 2021 8
System Model
 Processes P1, P2, …, Pn
 Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
 Each resource type Ri has Wi instances.

27 November 2021 9
Resource Allocation
 Request
 If the request cannot be granted immediately then the
requesting process must wait until it can acquire the
resource.
 Use
 Process operates on resource.

 Release
 Process release the resource.

 Resources
 Physical resource -- Printer,Memory space…..

 Logical resource --- Semaphore,Files…..

27 November 2021 10
Reusable Resources
 Used by only one process at a time and not
depleted by that use
 Processes obtain resources that they later
release for reuse by other processes
 Examples: Processors, I/O channels, main and
secondary memory, devices, and data structures
such as files, databases, and semaphores
 Deadlock occurs if each process holds one
resource and requests the other

27 November 2021 11
Consumable Resources
 Created (produced) and destroyed (consumed)

 Interrupts, signals, messages, and information in


I/O buffers

 There is no limit on the number of consumable


resources of a particular type.

27 November 2021 12
Example of Deadlock
 Deadlock occurs if receive is blocking

P1 P2
... ...

Receive(P2); Receive(P1);
... ...

Send(P2, M1); Send(P1, M2);

27 November 2021 13
Deadlock Characterization
 Mutual exclusion:
 At least one resource must be in a non sharable
mode; that is only one process at a time can use a
resource.
 If any other process request the resource, the
requesting process must be delayed until the
resource has been released.
 Hold and wait:
 a process holding at least one resource is waiting to
acquire additional resources held by other processes

27 November 2021 14
Deadlock Characterization
Deadlock can arise if all four conditions hold simultaneously.
 No preemption:
 a resource can be released only voluntarily by the
process holding it, after that process has completed its
task.(The resource cannot be preempted).
 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.
27 November 2021 15
Traffic Deadlock

27 November 2021 16
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 P1 → Rj


 assignment edge – directed edge Rj → Pi

27 November 2021 17
Resource-Allocation Graph (Cont.)
 Process

 Resource Type with 4 instances

Pi
 Pi requests instance of Rj
Rj
 Pi is holding an instance of Rj
Pi

Rj
27 November 2021 18
Example of a Resource Allocation Graph

27 November 2021 19
Resource Allocation Graph With A Deadlock

27 November 2021 20
Graph With A Cycle But No Deadlock

27 November 2021 21
Basic Facts
 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

 Most of the Operating Systems are not


handling Deadlock problems
 It is too costly to prevent / avoid / detect – recover
deadlock
27 November 2021 22
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

27 November 2021 23
Deadlock Prevention Schemes

27 November 2021 24
Deadlock Prevention
 Ensure that at least one of the 4 necessary
condition be prevented.

 Mutual Exclusion
 not required for sharable resources; must hold for
non-sharable resources
 Only one process may use a resource at a time

27 November 2021 25
Deadlock Prevention (Cont.)
 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.

 Low resource utilization; Starvation possible

27 November 2021 26
Deadlock Prevention (Cont.)
 Alternative method:
 Allow a process to request resources only when
the process has no resources.
 Release all resources before requesting for a new
one

 This method is inefficient


 A process may be held up for a long time waiting for
all of its resource request be filled, when it could have
proceeded with only some of the resources.
 A process may not know in advance all of the
resources that it will require.

27 November 2021 27
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 only when it can regain its old
resources, as well as the new ones that it is requesting.
 Alternate Approach:
 If a process request some resources,
 Allot to the process if the resource is free

 Else, check whether they are allocated to some other process


that is waiting for additional resource. If so, preempt the
resource from the waiting process and allot them to the
requesting process.

27 November 2021 28
Deadlock Prevention (Cont.)

 Circular Wait
 Impose a total ordering of all resource types, and
require that each process requests resources in
an increasing order of enumeration

27 November 2021 29
 Example
 Assign a unique integer number to individual resource which allows

us to compare two resources and determine one proceed other.


 Define a one – to – one function F: RN

 F(tape drive)=1

 F(disk drive)=5

 F(printer) =12
 Process can request resources only in an increasing order of

enumeration.
 So R(j) is acceptable to a process if and only if F(R(i))< F(R(j)). If

F(R(i))> F(R(j)) then release (R(i)) before R(j) entered in.


 Example: Process needs tape drive and printer must request the

tape driver first then printer.

27 November 2021 30
Disadvantage of Deadlock Prevention

 Low device Utilization


 Reduced system throughput.

27 November 2021 31
Deadlock Avoidance

 A decision is made dynamically whether the


current resource allocation request will, if
granted, potentially lead to a deadlock

 Requires knowledge of future process


request

27 November 2021 32
Two Approaches to Deadlock Avoidance

 Do not start a process if its demands might


lead to deadlock

 Do not grant an incremental resource request


to a process if this allocation might lead to
deadlock

27 November 2021 33
Avoidance algorithms
 Single instance of a resource type
 Use a resource-allocation graph

 Multiple instances of a resource type


 Use the banker’s algorithm

27 November 2021 34
Resource-Allocation Graph Algorithm
 3 types of edges
 Request Edge, Assignment Edge, Claim Edge

 Claim edge Pi → Rj indicated that process Pi may request


resource Rj; represented by a dashed line.
 Claim edge converts to request edge when a process
requests a resource.
 Request edge converted 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.
 Request Pi → Rj can be granted only if converting the request
edge Pi → Rj to assignment edge Rj → Pi does not result in
the formation of cycle in the resource allocation graph.

27 November 2021 35
Resource-Allocation Graph

27 November 2021 36
Unsafe State In Resource-Allocation Graph

27 November 2021 37
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

27 November 2021 38
Banker’s Algorithm
 Applicable to multiple instance of each resource
type.
 This algorithm is less efficient.
 Each process must a priori claim maximum use.
 When a process requests a resource the system
must check whether allocation of these resources
will leave the system in safe state.
 When a process gets all its resources it must
return them in a finite amount of time.

27 November 2021 39
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]

27 November 2021 40
Example of Banker’s Algorithm
 5 processes P0 through P4;
 3 resource types: A (10 instances),

B (5instances) and C (7 instances)


Snapshot at time T0:
Allocation Max Available
ABC ABC ABC
P0 0 1 0 753 332
P1 2 0 0 322
P2 3 0 2 902
P3 2 1 1 222
P4 0 0 2 433

27 November 2021 41
Example (Cont.)
 The content of the matrix Need is defined to be
Need = Max – Allocation
Need
ABC
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1

27 November 2021 42
Example of Banker’s Algorithm
Allocation Max Need Available
ABC ABC ABC ABC
P0 0 1 0 753 743 332
P1 2 0 0 322 122
P2 3 0 2 902 600
P3 2 1 1 222 011
P4 0 0 2 433 431

 The system is in a safe state since the sequence


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

27 November 2021 43
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

27 November 2021 44
Example: P1 Request (1,0,2)
 Check that Request ≤ Need (that is, (1,0,2) ≤ (1,2,2) ⇒ true

 Check that Request ≤ Available


(that is, (1,0,2) ≤ (3,3,2) ⇒ true

 Snapshot Before allocation


Allocation Need Available
ABC ABC ABC
P0 0 1 0 743 332
P1 2 0 0 122
P2 3 0 2 600
P3 2 1 1 011
P4 0 0 2 431

27 November 2021 45
Example: P1 Request (1,0,2)
 After allocation

Allocation Need Available


ABC ABC ABC
P0 0 1 0 743 332
P1 2 0 0 122
P2 3 0 2 600
P3 2 1 1 011
P4 0 0 2 431

27 November 2021 46
Example: P1 Request (1,0,2)
 After allocation

Allocation Need Available


ABC ABC ABC
P0 0 1 0 743 332
P1 3 0 2 122
P2 3 0 2 600
P3 2 1 1 011
P4 0 0 2 431

27 November 2021 47
Example: P1 Request (1,0,2)
 After allocation

Allocation Need Available


ABC ABC ABC
P0 0 1 0 743 332
P1 3 0 2 122
P2 3 0 2 600
P3 2 1 1 011
P4 0 0 2 431

27 November 2021 48
Example: P1 Request (1,0,2)
 After allocation

Allocation Need Available


ABC ABC ABC
P0 0 1 0 743 332
P1 3 0 2 020
P2 3 0 2 600
P3 2 1 1 011
P4 0 0 2 431

27 November 2021 49
Example: P1 Request (1,0,2)
 After allocation

Allocation Need Available


ABC ABC ABC
P0 0 1 0 743 332
P1 3 0 2 020
P2 3 0 2 600
P3 2 1 1 011
P4 0 0 2 431

27 November 2021 50
Example: P1 Request (1,0,2)
 After allocation

Allocation Need Available


ABC ABC ABC
P0 0 1 0 743 230
P1 3 0 2 020
P2 3 0 2 600
P3 2 1 1 011
P4 0 0 2 431

 Executing safety algorithm shows that sequence < P1, P3,


P4, P0, P2> satisfies safety requirement
27 November 2021 51
Resource-Request Algorithm for Process Pi
Request = 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 – Request;
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

27 November 2021 52
Execute resource request algorithm for
following
 Can request for (3,3,0) by P4 be granted?
 Can request for (0,2,0) by P0 be granted?

27 November 2021 53
Deadlock Detection
 Allow system to enter deadlock state

 Detection algorithm

 Recovery scheme

 Includes run time cost of maintaining the


necessary information and executing the
detection algorithm

27 November 2021 54
Single Instance of Each Resource Type
 Maintain wait-for graph
 Nodes are processes
 Pi → Pj if Pi is waiting for Pj

 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

27 November 2021 55
Resource-Allocation Graph and Wait-for Graph

Resource-Allocation Graph Corresponding wait-for graph

27 November 2021 56
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

27 November 2021 57
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:
Allocation Request Available
ABC ABC ABC
P0 0 1 0 000 000
P1 2 0 0 202
P2 3 0 3 000
P3 2 1 1 100
P4 0 0 2 002

27 November 2021 58
Example (Cont.)
 P2 requests an additional instance of type C
Request
ABC
P0 0 0 0
P1 2 0 1
P2 0 0 0
P3 1 0 0
P4 0 0 2

27 November 2021 59
Example (Cont.)
 P2 requests an additional instance of type C
Request
ABC
P0 0 0 0
P1 2 0 1
P2 0 0 1
P3 1 0 0
P4 0 0 2

27 November 2021 60
Example

of Detection Algorithm
Five processes P through P ; three resource types
0 4
A (7 instances), B (2 instances), and C (6 instances)
 Snapshot at time T0:

Allocation Request Available


ABC ABC ABC
P0 0 1 0 000 000
P1 2 0 0 202
P2 3 0 3 001
P3 2 1 1 100
P4 0 0 2 002
 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
27 November 2021 61
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
Algorithm requires an order of O(m x n2) operations to
detect whether the system is in deadlocked state

27 November 2021 62
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
 Practical approach about deadlock detection algo.
 Invoke the algorithm at less frequent intervals
 Invoke whenever CPU utilization drops below 40%.

27 November 2021 63
Recovery from Deadlock

 2 methods to recover from deadlock


 Recover manually (by the operator)
 Recover automatically

 Automatic recovery
 Process Termination
 Resource Preemption

27 November 2021 64
Recovery from Deadlock: Process Termination
 Abort all deadlocked processes (Very expensive)
 Abort one process at a time until the deadlock cycle is
eliminated
 Considerable overhead.
 In which order should we choose to abort?
 Priority of the process
 How long process has computed, and how much longer to
completion
 Types of Resources the process has used
 No. of Resources process needs to complete
 How many processes will need to be terminated
 Is process interactive or batch?

27 November 2021 65
Recovery from Deadlock: Resource Preemption
 Selecting a victim –
 Which resources and from which processes are to be

preempted?
 Determine the order of preemption to minimize cost.
 Cost factors may include parameters as the number of

resources a deadlocked process is holding and the


amount of time the process has thus far consumed
during its execution.
 Rollback –We must roll back the process to some safe
state and restart it from that state.
 Total rollback
 Partial rollback

 this method requires the system to keep more


information about the state of all running processes.
 checkpoint a process periodically

27 November 2021 66
Recovery from Deadlock: Resource
Preemption contd…
 Starvation – same process may always be
picked as victim
 ensure that a process can be picked as a victim
(preemption) only a (small) finite number of times.
 The most common solution is to include the number of
rollbacks in the cost factor.

27 November 2021 67
Combined Approach to Deadlock Handling
 Combine the three basic approaches
 Prevention
 Avoidance
 Detection
allowing the use of the optimal approach for
each of resources in the system.
 Partition resources into hierarchically ordered
classes.
 Use most appropriate technique for handling
deadlocks within each class

27 November 2021 68
Strengths and Weaknesses of the Strategies

27 November 2021 69

You might also like