Docsity

Slides on Deadlock Avoidance Algorithm | N 1, Study notes of Health sciences

Toggle sidebar
bg1
12
ÿ
resource allocation state matrix
<n1, n2, n3, ..., nr>
Dealing With Deadlock
Deadlock avoidance
Examine each resource request and determine whether or not
granting the request can lead to deadlock
R1 R2 R3 ... Rr
P1
P2
P3
Pp
n1,1 n1,2 n1,3 ... n1,r
n2,1
n3,1
np,1 np,r
n2,2
...
...
...
...
...
Define a set of vectors and matrices that characterize the
current state of all resources and processes
ÿ
maximum claim matrix
Maxij
=
the maximum number of units
of resource
j
that the process
i
will
ever require simultaneously
ÿ
available vector
Allocij
=
the number of units of
resource
j
held by process
i
Availj
=
the number of units of
resource
j
that are unallocated
13
A computer system with 5 processes and 4 resources
P4
Deadlock Avoidance
State matrices example
ALLOCATION MAX_CLAIM AVAILABLE
R1 R2 R3 R4
0 0 1 2
1 7 5 0
2 3 5 6
0 6 5 2
0 6 5 6
0 0 1 2
1 7 5 0
2 3 5 6
0 6 5 2
0 6 5 6
1 5 2 0
1 5 2 0
R1 R2 R3 R4
R1 R2 R3 R4
P1
P2
P3
P4
P5
0 0 1 2
1 0 0 0
1 3 5 3
0 6 3 2
0 0 1 4
0 0 1 2
1 0 0 0
1 3 5 3
0 6 3 2
0 0 1 4
P1P2P3P5
P5
R1R2R3R4
14
Deadlock Avoidance
Concept
The OS will examine each resource request and determine
whether or not granting the request can lead to deadlock
ÿIf, after we grant this request, all processes simultaneously make
their maximum claim, will the system deadlock?
ÿCan we satisfy the maximum claims of processes in some order?
ALLOCATION MAX_CLAIM AVAILABLE
R1 R2 R3 R4
0 0 1 2
1 7 5 0
2 3 5 6
0 6 5 2
0 6 5 6
1 5 2 0
R1 R2 R3 R4
R1 R2 R3 R4
P1
P2
P3
P4
P5
0 0 1 2
1 0 0 0
1 3 5 3
0 6 3 2
0 0 1 4
15
Deadlock Avoidance Algorithm
State definitions
A
resource allocation state
is
safe
if the system can allocate
resources to each process up to its maximum claim such that
the system can not deadlock
MAX_CLAIMPi – ALLOCATIONPi ≤ AVAIL + SALLOCATIONPj
Term. j
The largest request for resources
that process Pi can make
The resources available to
process Pi after processes
P1 through Pi–1 terminate
This ordering of processes is called a
safe sequence
If a safe sequence exists then there exists a process (
P
1) that can
execute to completion in the current state, and
Pj
can execute to completion at worst after processes
P
1 -
Pj–
1
complete
There must be an ordering of the processes
P
1,
P
2, ...,
Pn
, such
that for all processes
Pi
,
pf3
pf4

Partial preview of the text

Download Slides on Deadlock Avoidance Algorithm | N 1 and more Study notes Health sciences in PDF only on Docsity!

12 ÿ resource allocation state matrix

< n 1 , n 2 , n 3 , ..., nr >

Dealing With Deadlock

Deadlock avoidance Examine each resource request and determine whether or not granting the request can lead to deadlock

R 1 R 2 R 3 ... Rr

P 1

P 2

P 3

Pp

n 1,1 n 1,2 n 1,3 ... n 1, r

n 2,

n 3,

np ,1 np , r

n 2,

Define a set of vectors and matrices that characterize the current state of all resources and processes ÿ maximum claim matrix Maxij = the maximum number of units of resource j that the process i will ever require simultaneously ÿ available vector Allocij = the number of units of resource j held by process i Availj = the number of units of resource j that are unallocated 13

A computer system with 5 processes and 4 resources

P 4

Deadlock Avoidance

State matrices example ALLOCATION MAX_CLAIM AVAILABLE R 1 R 2 R 3 R 4 0 0 1 2 1 7 5 0 2 3 5 6 0 6 5 2 0 6 5 6

R 1 R 2 R 3 R 4 R 1 R 2 R 3 R 4

P 1

P 2

P 3

P 4

P 5

P 1 P 2 P 3 PP 55

R 1 R 2 R 3 R 4

Deadlock Avoidance

Concept The OS will examine each resource request and determine whether or not granting the request can lead to deadlock ÿ If, after we grant this request, all processes simultaneously make their maximum claim, will the system deadlock? ÿ Can we satisfy the maximum claims of processes in some order? ALLOCATION MAX_CLAIM AVAILABLE R 1 R 2 R 3 R 4 0 0 1 2 1 7 5 0 2 3 5 6 0 6 5 2 0 6 5 6

R 1 R 2 R 3 R 4 R 1 R 2 R 3 R 4

P 1

P 2

P 3

P 4

P 5

Deadlock Avoidance Algorithm

State definitions A resource allocation state is safe if the system can allocate resources to each process up to its maximum claim such that the system can not deadlock

MAX_CLAIMPi – ALLOCATIONPi ≤ AVAIL + S ALLOCATIONPj

Term. j The largest request for resources that process Pi can make The resources available to process Pi after processes P 1 through Pi– 1 terminate This ordering of processes is called a safe sequence If a safe sequence exists then there exists a process ( P 1 ) that can execute to completion in the current state, and Pj can execute to completion at worst after processes P 1 - Pj– 1 complete There must be an ordering of the processes P 1 , P 2 , ..., Pn, such that for all processes Pi,

16

A computer system with 5 processes and 4 resources

P 4

Deadlock Avoidance Algorithm

Example ALLOCATION MAX_CLAIM AVAILABLE R 1 R 2 R 3 R 4 0 0 1 2 1 7 5 0 2 3 5 6 0 6 5 2 0 6 5 6

R 1 R 2 R 3 R 4 R 1 R 2 R 3 R 4

P 1

P 2

P 3

P 4

P 5

P 1 P 2 P 3 PP 55

R 1 R 2 R 3 R 4

MAX_CLAIMPi – ALLOCATIONPi ≤ AVAIL + (^) S j =1 ALLOCATIONPj i –

Is this system in a safe state?

Does there exist a safe sequence? 17

Deadlock Avoidance Example

Safe sequence computation

  1. Compute the largest possible resource request a process can make P 1 P 2 P 3 P 4 P 5

R 1 R 2 R 3 R 4

ALLOCATION MAX_REQUEST

R 1 R 2 R 3 R 4

MAX_CLAIM

R 1 R 2 R 3 R 4

  • =

P 1 P 2 P 3 P 4 P 5

R 1 R 2 R 3 R 4

  1. Attempt to build a safe sequence

Deadlock Avoidance Example

Safe sequence computation ÿ Does there exist a process Pi such that MAX_REQUESTPi ≤ AVAILABLE? ÿ If no, then there is no safe sequence, the state is unsafe ÿ If yes, add Pi to the sequence ÿ Set AVAILABLE = AVAILABLE + ALLOCATIONPi

ALLOCATION MAX_CLAIM AVAILABLE

R 1 R 2 R 3 R 4

P 1

P 2

P 3

P 4

P 5

R 1 R 2 R 3 R 4

R 1 R 2 R 3 R 4 R 1 R 2 R 3 R 4

MAX_REQUEST

Deadlock Avoidance Example

Safe sequence computation What if P 2 wants to change its allocation to < 0 , 4 , 2 , 0 >? ÿ Is the resulting allocation state safe?

ALLOCATION MAX_CLAIM AVAILABLE

R 1 R 2 R 3 R 4

P 1

P 2

P 3

P 4

P 5

R 1 R 2 R 3 R 4

R 1 R 2 R 3 R 4 R 1 R 2 R 3 R 4

MAX_REQUEST

Deadlock Detection & Recovery

Recovering from deadlock Abort all deadlocked processes & reclaim their resources Abort one process at a time until all cycles in the RAG are eliminated Where to start? ÿ Select low priority process ÿ Processes with most allocation of resources Caveat: ensure that system is in consistent state (e.g., transactions) Optimization: ÿ Checkpoint processes periodically; rollback processes to checkpointed state

P 1 P 2 P 3 P 4 PP 55

R 1 R 2 R 3 R 4