deadlock in operating system.
prevention of deadlock and types of deadlock. deadlock avoidance in operating system
Size: 379.7 KB
Language: en
Added: May 03, 2024
Slides: 26 pages
Slide Content
7: Deadlocks 1
Jerry Breecher
OPERATING SYSTEMS
DEADLOCKS
7: Deadlocks 2
What Is In This Chapter?
•What is a deadlock?
•Staying Safe: Preventing and Avoiding Deadlocks
•Living Dangerously: Let the deadlock happen, then
detect it and recover from it.
OPERATING SYSTEM
Deadlocks
7: Deadlocks 4
•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.
DEADLOCKS
Bridge Crossing
Example
7: Deadlocks 6
DEADLOCKS
Avisual(mathematical)waytodetermineifadeadlockhas,ormayoccur.
G=(V,E)Thegraphcontainsnodesandedges.
VNodesconsistofprocesses={P1,P2,P3,...}andresourcetypes
{R1,R2,...}
EEdgesare(Pi,Rj)or(Ri,Pj)
Anarrowfromtheprocesstoresourceindicatestheprocessisrequestingthe
resource.Anarrowfromresourcetoprocessshowsaninstanceoftheresource
hasbeenallocatedtotheprocess.
Processisacircle,resourcetypeissquare;dotsrepresentnumberofinstancesof
resourceintype.Requestpointstosquare,assignmentcomesfromdot.
RESOURCE
ALLOCATION GRAPH
P
i
R
j
P
i
R
j
P
i
7: Deadlocks 13
NOTE:Alldeadlocksareunsafe,butallunsafesareNOTdeadlocks.
SAFE
DEADLOCK
UNSAFE
Only with luck will
processes avoid
deadlock.
O.S. can avoid
deadlock.
DEADLOCKS
Deadlock
Avoidance
7: Deadlocks 14
Let'sassumeaverysimplemodel:eachprocessdeclaresitsmaximum
needs.Inthiscase,algorithmsexistthatwillensurethatnounsafestateis
reached.
EXAMPLE:
Thereexistsatotalof12tapedrives.Thecurrentstatelookslikethis:
Inthisexample,<p1,p0,p2>
isaworkablesequence.
Supposep2requestsandis
givenonemoretapedrive.
Whathappensthen?
ProcessMax NeedsAllocatedCurrent
Needs
P0 10 5 5
P1 4 2 2
P2 9 2 7
DEADLOCKS
Deadlock
Avoidance
There are multiple instances of
the resource in these examples.
7: Deadlocks 17
Dotheseexamples:
Considerasystemwith:fiveprocesses,P0P4,threeresourcetypes,A,B,C.
TypeAhas10instances,Bhas5instances,Chas7instances.
AttimeT0thefollowingsnapshotofthesystemistaken.
Is the system
in a safe state?
DEADLOCKS Deadlock
Avoidance
Safety Algorithm
134200P4
110112P3
006203P2
020002P1
233347010P0
CBACBACBA
AvailReqAlloc
Max Needs = allocated + can-be-requested
7: Deadlocks 18
Dotheseexamples:
NowtryitagainwithonlyaslightchangeintherequestbyP1.
P1requestsoneadditionalresourceoftypeA,andtwomoreoftypeC.
Request1=(1,0,2).
IsRequest1<available?
Alloc Req Avail
A B C AB C A B C
P0 0 1 0 74 3 1#30#
P1 3# 02# 02 0
P2 3 0 2 60 0
P3 2 1 1 01 1
P4 0 0 2 43 1
Produce the state
chart as if the
request is Granted
and see if it’s safe.
(We’ve drawn the
chart as if it’s
granted.
DEADLOCKS Deadlock
Avoidance
Safety Algorithm
Can the request
be granted?
7: Deadlocks 22
EXAMPLE
Wehavethreeresources,A,B,andC.Ahas7instances,Bhas2instances,andChas6
instances.Atthistime,theallocation,etc.lookslikethis:
Is there a
sequence that will
allow deadlock to
be avoided?
Is there more than
one sequence that
will work?
200200P4
001112P3
000303P2
202002P1
000000010P0
CBACBACBA
AvailReqAlloc
DEADLOCKS
Deadlock Detection
7: Deadlocks 23
EXAMPLE
SupposetheRequestmatrixischangedlikethis.Inotherwords,themaximumamountstobe
allocatedareinitiallydeclaredsothatthisrequestmatrixresults.
USAGE OF THIS
DETECTION ALGORITHM
Frequency of check
depends on how often a
deadlock occurs and how
many processes will be
affected.
Is there now a
sequence that will
allow deadlock to be
avoided?
200200P4
001112P3
1#00303P2
202002P1
000000010P0
CBACBACBA
AvailReqAlloc
DEADLOCKS
Deadlock Detection