Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CSE321 - 7. Deadlocks

Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

Chapter 7: Deadlocks

Narzu Tarannum
Lecturer
BRACU
Covered in chapter 7

● The Deadlock Problem


● System Model
● Deadlock Characterization
● Methods for Handling Deadlocks
● Deadlock Prevention
● Deadlock Avoidance
● Deadlock Detection
● Recovery from Deadlock

Operating System Concepts 1.2 Silberschatz, Galvin and Gagne ©2005


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 1.3 Silberschatz, Galvin and Gagne ©2005


Bridge Crossing Example
Deadlock cause (a situation or opposing parties) to come to a point where no progress
can be made because of fundamental disagreement.

● Each section of a bridge can be viewed as a resource.


● Traffic moves only in one direction on the Bridge.
● Deadlock occurs!
● If deadlock occurs, it can be resolved if one car backs up.
● Several cars may have to be backed up.

● Deadlock is a situation where a set of processes are blocked because each process is
holding a resource and waiting for another resource acquired by some other process.

Operating System Concepts 1.4 Silberschatz, Galvin and Gagne ©2005


The Deadlock Problem
● In a multiprogramming environment, several processes may compete
for a finite number of resources.
● A process requests resources; if the resources are not available at that
time, the process enters a wait state. It may happen that waiting
process will never gain change state, because the resources they have
requested are held by other waiting processes. This situation is called a
deadlock.

R1

P1R1 Need R2
P1 P2
P2R2 Need R1
R2
● Example
● System has 2 Resources: R1, R2.
● P1 and P2 each hold one resource and each needs another one.

Operating System Concepts 1.5 Silberschatz, Galvin and Gagne ©2005


System Model
● A system consist of a finite number of resources to be distributed
among a number of competing processes.
● Resource types R1, R2, . . ., Rm
● Process are P1, P2, . . ., Pn

● Resources are partitioned into several types

● Physical resource for example, CPU cycles, memory


space, I/O devices .
● Logical resources for example, semaphores, mutex locks,
and files.
● Each resource type consisting of some number of identical
instance.
● Each resource type Ri has Wi instances.

Operating System Concepts 1.6 Silberschatz, Galvin and Gagne ©2005


System Model
● Under the normal mode of operation, a process
may utilize a resource in only the following
sequence:
 Request: The process requests the resource.
 Use: The process can operate on the resource
 Release: The process releases the resource.

● A process must request a resource before


using it and must release the resource after
using it.

Operating System Concepts 1.7 Silberschatz, Galvin and Gagne ©2005


Necessary Conditions for Deadlock

Deadlock can arise if four conditions hold simultaneously in a system:

1. Mutual exclusion
2. Hold and wait
3. No preemption
4. Circular wait
If one of them is not present in a system, no deadlock will arise

Operating System Concepts 1.8 Silberschatz, Galvin and Gagne ©2005


Necessary Conditions

R1

1. Mutual exclusion:
At least one resource P1 P2

must be held in a
non-sharable mode; R1

only one process at a P1 P2

time can use a


resource. R1

P1 P2

Operating System Concepts 1.9 Silberschatz, Galvin and Gagne ©2005


Necessary Conditions

2. Hold and wait: a R1

process holding at
least one resource is P1 P2

waiting to acquire R2
additional resources
held by other
processes.

Operating System Concepts 1.10 Silberschatz, Galvin and Gagne ©2005


Necessary Conditions

3. No preemption:
a resource can be
released only
voluntarily by the
process holding it,
after that process has
completed its task.

Operating System Concepts 1.11 Silberschatz, Galvin and Gagne ©2005


Necessary Conditions

4. Circular wait:
There must be a R1

circular chain of two


or more processes P1 P2

each of which is
waiting for a resource R2

held by the next


member of the chain.

Operating System Concepts 1.12 Silberschatz, Galvin and Gagne ©2005


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

G  ( V, E)
Process Request edge Pi  Rj

Resource Allocation edge Rj  Pi

Operating System Concepts 1.13 Silberschatz, Galvin and Gagne ©2005


Resource-Allocation Graph (Cont.)
● Process

● Resource Type with 4 instances

● Pi requests instance of Rj
Pi

Rj

● Pi is holding an instance of Rj

Pi

Rj

Operating System Concepts 1.14 Silberschatz, Galvin and Gagne ©2005


Example of a Resource Allocation Graph

● P ={P1, P2, P3}


● R ={R1, R2, R3, R4}
● E ={P1 → R1, P2 → R3,
R1 → P2, R2 → P2,
R2 → P1, R3 → P3}

Operating System Concepts 1.15 Silberschatz, Galvin and Gagne ©2005


Basic Facts

● If graph contains no cycles  no deadlock.

● If graph contains a cycle  a deadlock may occur


 if only one instance per resource type, then
deadlock.
 if several instances per resource type,
possibility of deadlock.

Operating System Concepts 1.16 Silberschatz, Galvin and Gagne ©2005


Resource Allocation Graph With A Deadlock

● In the figure At this point, two minimal cycles exist in the system:
● P1 → R1 → P2 → R3 → P3 → R2 → P1
● P2 → R3 → P3 → R2 → P2

Operating System Concepts 1.17 Silberschatz, Galvin and Gagne ©2005


Resource Allocation Graph With A Cycle But No Deadlock

Operating System Concepts 1.18 Silberschatz, Galvin and Gagne ©2005


Methods for Handling Deadlocks

● Ensure that the system will never enter a deadlock state


(Deadlock prevention).
● In deadlock avoidance, the request for any resource will be
granted if the resulting state of the system doesn't cause
deadlock in the system. In order to avoid deadlocks, the process
must tell OS, the maximum number of resources a process can
request to complete its execution.(Deadlock avoidance).
● Allow the system to enter a deadlock state and then recover
(Deadlock detection and recovery).
● Ignore the problem and pretend that deadlocks never occur in
the system; used by most operating systems, including UNIX.
(Ignorance)

Operating System Concepts 1.19 Silberschatz, Galvin and Gagne ©2005


Methods for Handling Deadlocks

● Deadlock prevention
● Deadlock avoidance
● Deadlock detection and recovery
● Ignorance

Operating System Concepts 1.20 Silberschatz, Galvin and Gagne ©2005


Deadlock Prevention
Eliminate one of the four conditions.

1.Mutual Exclusion – not required for sharable resources; must


hold for non-sharable resources.

R1 R2

P1 P2

Operating System Concepts 1.21 Silberschatz, Galvin and Gagne ©2005


Deadlock Prevention (Cont.)
2.Hold and Wait – must guarantee that whenever a process
requests a resource, it does not hold any other resources.
● First Protocol or conservative approach: A
process is allowed to start execution if and only if
it has acquired all the resources.
● Second Protocol or do not hold approach: A
process will acquire only desired resources but
before making any fresh request, it must release
all the resources that it currently hold.
● Third Protocol or wait-time out approach:
We place a maximum time bound up to which a
process can wait for resources, after which the
must release all the holding resources.

R1, R2, … R4, R5, ………R10

Operating System Concepts 1.22 Silberschatz, Galvin and Gagne ©2005


Deadlock Prevention (Cont.)
3.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.

Operating System Concepts 1.23 Silberschatz, Galvin and Gagne ©2005


Deadlock Prevention (Cont.)
4.Circular Wait – impose a total ordering of all resource types, and
require that each process requests resources in an increasing order of
enumeration.

R1 R2 P1 P2 P1 P2
R1 R2 R1 R1
R2 R1 R2 R2
P1 P2

P1 R2 P2 R1

Operating System Concepts 1.24 Silberschatz, Galvin and Gagne ©2005


Deadlock Prevention (Cont.)
4.Circular Wait – impose a total ordering of all resource types, and
require that each process requests resources in an increasing order of
enumeration. Let R = R1, R2, ……., Rm are the resources.
-Circular wait can be eliminated by just giving the natural number of
every resource. So, we can define the function F: R  N

F(Tape drives) = 2

P1(2,4)
F(Disk drives) = 3

P1(3,4,5) F(Scanners) = 4

F(Printers) = 5

Operating System Concepts 1.25 Silberschatz, Galvin and Gagne ©2005


Methods for Handling Deadlocks

● Deadlock prevention
● Deadlock avoidance
● Deadlock detection and recovery
● Ignorance

Operating System Concepts 1.26 Silberschatz, Galvin and Gagne ©2005


Deadlock Avoidance

● Banker’s Algorithm requires the information of each process


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

Operating System Concepts 1.27 Silberschatz, Galvin and Gagne ©2005


Safe-Unsafe State
● When a process requests an available resource, system must
decide if immediate allocation leaves the system in a safe
state.
● A state is said to be safe, if there is some scheduling
order in which every process can run to completion that
means system is in safe state if there exists a safe
sequence of all processes.
● An unsafe state does not have to lead to a deadlock; it
could lead to a deadlock.

ENSURE SYSTEM NEVER REACHES AN UNSAFE STATE

Operating System Concepts 1.28 Silberschatz, Galvin and Gagne ©2005


Example of a Safe-unsafe state

Let there are 3 processes (A, B, C) where total number of resources


are 10. Every process has to declare maximum number of resource
they need in advance. In present scenario system allocated 3 units of
resources to A where maximum requirement of A is 9 units. In the
same way system allocated 2 units to B where maximum requirement
of B is 4 and system allocated 2 units to C where maximum
requirement of C is 7

Has Max
Total: 10 units A 3 9
B 2 4
C 2 7
Free: 3 units

Operating System Concepts 1.29 Silberschatz, Galvin and Gagne ©2005


Safe state
Allocate 2 units to B B completes
Has Max Has Max Has Max
A 3 9 A 3 9 A 3 9
B 2 4 B 4 4 B 0 -
C 2 7 C 2 7 C 2 7
Free: 3 units Free: 1 units Free: 5 units

Allocate 6 units to A C completes Allocate 5 units to C


Has Max Has Max Has Max
A 9 9 A 3 9 A 3 9
B 0 - B 0 - B 0 -
C 0 - C 0 - C 7 7
Free: 1 unit Free: 7 units Free: 0 units

This is a safe state because there is some scheduling order in which


every process executes. Here the order is: BCA

Operating System Concepts 1.30 Silberschatz, Galvin and Gagne ©2005


Unsafe state
Allocate 2 units to B B completes
Has Max Has Max Has Max
A 4 9 A 4 9 A 4 9
B 2 4 B 4 4 B 0 -
C 2 7 C 2 7 C 2 7
Free: 2 units Free: 0 units Free: 4 units

5 units to C or A can not be allocated


Has Max
A ? 9
B 0 -
C ? 7
Free: 0 units

This is an unsafe state because there exist no scheduling order in


which every process executes.

Operating System Concepts 1.31 Silberschatz, Galvin and Gagne ©2005


Safe, Unsafe , Deadlock State
• 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 1.32 Silberschatz, Galvin and Gagne ©2005


Banker’s Algorithm
● Multiple instances of each resource type.

● Each process must a priori claim maximum use.

● When a process requests a resource it may have to wait.

● When a process gets all its resources it must return them in


a finite amount of time.

Operating System Concepts 1.33 Silberschatz, Galvin and Gagne ©2005


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


X<=Y if X=(1,7,3,2) and Y=(0,3,2,1) then X≠Y.

Operating System Concepts 1.34 Silberschatz, Galvin and Gagne ©2005


Banker's 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 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.

Operating System Concepts 1.35 Silberschatz, Galvin and Gagne ©2005


Example of Banker’s Algorithm
● 3 resource types:
A (10 instances), B (5instances), and C (7 instances).
● 5 processes P0 through P4;
● Snapshot at time T0:

Allocation Max Available Need = Max– Allocation


ABC ABC ABC ABC
P0 010 753 332 743
P1 200 322 122

P2 302 902 600


011
P3 211 222
431
P4 002 433

Operating System Concepts 1.36 Silberschatz, Galvin and Gagne ©2005


Example of Banker’s safety Algorithm
1. Let Work and Finish be vectors of Process Allocation Max Availabl Need
length m and n, respectively. e
Initialize:
Work = Available A B C A B C A B C A B C
Finish [i] = false for i=0,1,…, n-1
2. Find and i such that both: P0 0 1 0 7 5 3 3 3 2 7 4 3
(a) Finish [i] = false
(b) Needi  Work P1 2 0 0 3 2 2 1 2 2
If no such i exists, go to step 4.
3. Work = Work + Allocationi P2 3 0 2 9 0 2 6 0 0
Finish[i] = true
go to step 2.
4. If Finish [i] == true for all i, then the P3 2 1 1 2 2 2 0 1 1
system is in a safe state.
P4 0 0 2 4 3 3 4 3 1
Work = Available = 3 3 2
Finish[0]=Finish[1]=Finish[2]=Finish[3]=Finish[4]=False
Need(P0)>Work  Finish[0]=F  Do Nothing 7 4 3  3 3 2  F  do nothing

Need(P1)<=Work  Finish[1]=T  Work=Work + AllocationP1 1 2 2  3 3 2  T: Work = 3 3 2 + 2 0 0 = 5 3 2

Need(P2)>Work  Finish[2]=F  Do Nothing 6 0 0  5 3 2  F  do nothing

Need(P3)<=Work  Finish[3]=T  Work=Work + AllocationP3 0 1 1  5 3 2  T: Work = 5 3 2 + 2 1 1 = 7 4 3

Need(P4)<=Work  Finish[4]=T  Work=Work + AllocationP4 4 3 1  7 4 3  T: Work = 7 4 3 + 0 0 2 = 7 4 5

Need(P0)<=Work  Finish[0]=T  Work=Work + AllocationP0 7 4 3  7 4 5  T: Work = 7 4 5 + 0 1 0 = 7 5 5

Need(P2)<=Work  Finish[2]=T  Work=Work + AllocationP2 6 0 0  7 5 5  T: Work = 7 5 5 + 3 0 2 = 10 5


7

Found the safe sequence <P1, P3, P4, P0, P2>


Operating System Concepts 1.37 Silberschatz, Galvin and Gagne ©2005
● Request for resources (1 0 2) for P1
will be granted at this state?

Operating System Concepts 1.38 Silberschatz, Galvin and Gagne ©2005


Banker’s Resource-request Algorithm
Request = request vector for process Pi. If Requesti [j] = k then
process Pi wants k instances of resource type Rj.
Pi Requesti
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 - Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
4. Check Bankers safety Algorithm, if this new state is safe
If safe  the resources are allocated to Pi.
If unsafe  Pi must wait, and the old resource-allocation
state is restored Silberschatz, Galvin and Gagne ©2005
Operating System Concepts 1.39
Example of Banker’s Resource-request Algorithm

Request = request vector for process Pi. If Requesti [j] = Process Allocation Max Available Need
k then process Pi wants k instances of resource type Rj.
A B C A B C A B C A B C
1. If Requesti  Needi go to step 2. Otherwise, raise
error condition, since process has exceeded its P0 0 1 0 7 5 3 3 3 2 7 4 3
maximum claim.
2. If Requesti  Available, go to step 3. Otherwise Pi P1 2 0 0 3 2 2 1 2 2
must wait, since resources are not available.
P2 3 0 2 9 0 2 6 0 0
3. Pretend to allocate requested resources to Pi by
modifying the state as follows: P3 2 1 1 2 2 2 0 1 1
Available = Available - Requesti; P4 0 0 2 4 3 3 4 3 1
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
Check a new additional request for P1 (1 0 2) can be granted now?

1. if Request of P1  Need of P1 1 0 2 <= 1 2 2  T


2. if Request of P1  Available 102332T
3. Available = Available – Requesti Available = 3 3 2 – 1 0 2 = 2 3 0
Allocationi = Allocationi + Requesti Allocation = 2 0 0 + 1 0 2 = 3 0 2
Needi = Needi – Requesti Need = 1 2 2 – 1 0 2 = 0 2 0

Operating System Concepts 1.40 Silberschatz, Galvin and Gagne ©2005


Example of Banker’s Resource-request Algorithm
3. Available = 3 3 2 – 1 0 2 = 2 3 0
Allocation = 2 0 0 + 1 0 2 = 3 0 2
Need = 1 2 2 – 1 0 2 = 0 2 0

Update table after additional request for P1 (1 0 2)

Process Allocation Max Available Need Process Allocation Max Available Need

A B C A B C A B C A B C A B C A B C A B C A B C

P0 0 1 0 7 5 3 3 3 2 7 4 3 P0 0 1 0 7 5 3 2 3 0 7 4 3

P1 2 0 0 3 2 2 1 2 2 P1 3 0 2 3 2 2 0 2 0

P2 3 0 2 9 0 2 6 0 0 P2 3 0 2 9 0 2 6 0 0

P3 2 1 1 2 2 2 0 1 1 P3 2 1 1 2 2 2 0 1 1

P4 0 0 2 4 3 3 4 3 1 P4 0 0 2 4 3 3 4 3 1

Operating System Concepts 1.41 Silberschatz, Galvin and Gagne ©2005


Verify with Banker’s safety Algorithm
Process Allocation Max Available Need
1. Let Work and Finish be vectors of
length m and n, respectively.
Initialize: A B C A B C A B C A B C
Work = Available
Finish [i] = false for i=0,1,…, n-1 P0 0 1 0 7 5 3 2 3 0 7 4 3
2. Find and i such that both:
(a) Finish [i] = false P1 3 0 2 3 2 2 0 2 0
(b) Needi  Work
If no such i exists, go to step 4. P2 3 0 2 9 0 2 6 0 0
3. Work = Work + Allocationi
Finish[i] = true P3 2 1 1 2 2 2 0 1 1
go to step 2.
4. If Finish [i] == true for all i, then the
system is in a safe state. P4 0 0 2 4 3 3 4 3 1

Need(P0)>Work  Finish[1]=F  Do Nothing 7 4 3  2 3 0  F  do nothing


Need(P1)<=Work  Finish[1]=T  Work=Work + Allocation 0 2 0  2 3 0  T: Work = 2 3 0 + 3 0 2 = 5 3 2
Need(P2)>Work  Finish[2]=F  Do Nothing 6 0 0  5 3 2  F  do nothing
Need(P3)<=Work  Finish[3]=T  Work=Work + Allocation 0 1 1  5 3 2  T: Work = 5 3 2 + 2 1 1 = 7 4 3
Need(P4)<=Work  Finish[4]=T  Work=Work + Allocation 4 3 1  7 4 3  T: Work = 7 4 3 + 0 0 2 = 7 4 5
Need(P0)<=Work  Finish[0]=T  Work=Work + Allocation 7 4 3  7 4 5  T: Work = 7 4 5 + 0 1 0 = 7 5 5
Need(P2)<=Work  Finish[2]=T  Work=Work + Allocation 6 0 0  7 5 5  T: Work = 7 5 5 + 3 0 2 = 10 5 7

Since there is a safe sequence <P1, P3, P4, P0, P2> ,


Additional request P1 (1 0 2) can be granted
Operating System Concepts 1.42 Silberschatz, Galvin and Gagne ©2005
Are following requests get accepted?

● Request for (3 3 0) resources for P4


● Request for (0 2 0) resources for P0

Process Allocation Max Available Need

A B C A B C A B C A B C

P0 0 1 0 7 5 3 3 3 2 7 4 3

P1 2 0 0 3 2 2 1 2 2

P2 3 0 2 9 0 2 6 0 0

P3 2 1 1 2 2 2 0 1 1

P4 0 0 2 4 3 3 4 3 1

Operating System Concepts 1.43 Silberschatz, Galvin and Gagne ©2005


Are following requests get accepted?

● Request for (3 3 0) resources for P4


● Request for (0 2 0) resources for P0

Process Allocation Max Available Need

A B C A B C A B C A B C

P0 0 1 0 7 5 3 3 3 2 7 4 3

P1 2 0 0 3 2 2 1 2 2

P2 3 0 2 9 0 2 6 0 0

P3 2 1 1 2 2 2 0 1 1

P4 0 0 2 4 3 3 4 3 1

Operating System Concepts 1.44 Silberschatz, Galvin and Gagne ©2005


Operating System Concepts 1.45 Silberschatz, Galvin and Gagne ©2005

You might also like