7/23/2019 Lec09 Deadlock
1/35
CS162
Operating Systems andSystems ProgrammingLecture 9
Tips for Working in a Project Team/Cooperating Processes and ead!ock
Septem"er 29# 2$1$
Prof% &o'n (u"iato)ic*'ttp+//inst%eecs%"erke!ey%edu/,cs162
7/23/2019 Lec09 Deadlock
2/35
Lec 9%29/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
eie)+ efinition of 3onitor
Semap'ores are confusing "ecause dua! purpose+ ot' mutua! e4c!usion and sc'edu!ing constraints C!eaner idea+ .se !ocksfor mutua! e4c!usion andcondition aria"!esfor sc'edu!ing constraints
3onitor+ a !ock and *ero or more condition aria"!esfor managing concurrent access to s'ared data
.se of 3onitors is a programming paradigm Lock+ proides mutua! e4c!usion to s'ared data+
5!)ays acuire "efore accessing s'ared data structure 5!)ays re!ease after finis'ing )it' s'ared data
Condition 7aria"!e+ a ueue of t'reads )aiting forsomet'ing insidea critica! section (ey idea+ a!!o) s!eeping inside critica! section "yatomica!!y re!easing !ock at time )e go to s!eep
Contrast to semap'ores+ Can8t )ait inside critica!
section
7/23/2019 Lec09 Deadlock
3/35
Lec 9%9/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
eie)+ Programming )it' 3onitors
3onitors represent t'e !ogic of t'e program Wait if necessary Signa! )'en c'ange somet'ing so any )aiting t'readscan proceed
asic structure of monitor:"ased program+lock
while (need to wait) { condvar.wait();}unlock
do something so no need to wait
lock
condvar.signal();
unlock
C'eck and/or update
state aria"!esWait if necessary
C'eck and/or updatestate aria"!es
7/23/2019 Lec09 Deadlock
4/35
Lec 9%;9/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
Si!"ersc'at*#
7/23/2019 Lec09 Deadlock
5/35
Lec 9%>9/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
Tips for Programming in a Project Team
ig projects reuire more t'an oneperson ?or !ong# !ong# !ong time@ ig OS+ t'ousands of person:yearsA
Bt8s ery 'ard to make soft)areproject teams )ork correct!y oesn8t seem to "e as true of "ig
construction projects mpire state "ui!ding finis'ed in
oneyear+ staging iron productiont'ousands of mi!es a)ay
Or t'e Dooer dam+ "ui!t to)ns to
'o!d )orkers Bs it O( to miss dead!inesE
We make it free ?s!ip days@ ea!ity+ t'ey8re ery e4pensie as
time:to:market is one of t'e most
important t'ingsA
FGou just 'aeto get your
sync'roni*ation rig'tAH
7/23/2019 Lec09 Deadlock
6/35
Lec 9%69/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
ig Projects
W'at is a "ig projectE Time/)ork estimation is 'ard Programmers are eterna! optimistics?it )i!! on!y take t)o days@A T'is is )'y )e "ug you a"out
starting t'e project ear!y Dad a grad student )'o used to say 'e just needed
F1$ minutesH to fi4 somet'ing% T)o 'ours !aterI Can a project "e efficient!y partitionedE
Partitiona"!e task decreases in time asyou add peop!e
ut# if you reuire communication+
Time reac'es a minimum "ound Wit' comp!e4 interactions# time increasesA
3yt'ica! person:mont' pro"!em+Gou estimate 'o) !ong a project )i!! take Starts to fa!! "e'ind# so you add more peop!e
Project takes een more timeA
7/23/2019 Lec09 Deadlock
7/35Lec 9%J9/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
Tec'niues for Partitioning Tasks
0unctiona! Person 5 imp!ements t'reads# Person imp!ementssemap'ores# Person C imp!ements !ocksI
Pro"!em+ Lots of communication across 5PBs Bf c'anges t'e 5PB# 5 may need to make c'anges Story+ Large air!ine company spent K2$$ mi!!ion on a ne)
sc'edu!ing and "ooking system% T)o teams F)orking
toget'er%H 5fter t)o years# )ent to merge soft)are%0ai!edA Bnterfaces 'ad c'anged ?documented# "ut no onenoticed@% esu!t+ )ou!d cost anot'er K2$$ mi!!ion to fi4%
Task Person 5 designs# Person )rites code# Person C tests 3ay "e difficu!t to find rig't "a!ance# "ut can focus oneac' person8s strengt's ?T'eory s systems 'acker@
Since e"ugging is 'ard# 3icrosoft 'as t)otesters foreac'programmer
3ost CS162 project teams are functiona!# "ut peop!e'ae 'ad success )it' task:"ased diisions
7/23/2019 Lec09 Deadlock
8/35Lec 9%9/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
Communication
3ore peop!e mean more communication C'anges 'ae to "e propagated to more peop!e T'ink a"out person )riting code for mostfundamenta! component of system+ eeryone dependson t'emA
3iscommunication is common FBnde4 starts at $E B t'oug't you said 1AH
W'o makes decisionsE Bndiidua! decisions are fast "ut trou"!e
7/23/2019 Lec09 Deadlock
9/35Lec 9%99/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
Coordination
3ore peop!e no one can make a!! meetingsA T'ey miss decisions and associated discussion
4amp!e from ear!ier c!ass+ one person missedmeetings and did somet'ing group 'ad rejected W'y do )e !imit groups to > peop!eE
Gou )ou!d neer "e a"!e to sc'edu!e meetings ot'er)ise W'y do )e reuire ; peop!e minimumE
Gou need to e4perience groups to get ready for rea! )or!d Peop!e 'ae different )ork sty!es
Some peop!e )ork in t'e morning# some at nig't Do) do you decide )'en to meet or )ork toget'erE
W'at a"out project s!ippageE Bt )i!! 'appen# guaranteedA 4+ p'ase ;# eeryone "usy "ut not ta!king% One person)ay "e'ind% =o one kne) unti! ery end M too !ateA
Dard to add peop!e to e4isting group 3em"ers 'ae a!ready figured out 'o) to )ork toget'er
7/23/2019 Lec09 Deadlock
10/35Lec 9%1$9/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
Do) to 3ake it WorkE
Peop!e are 'uman%
7/23/2019 Lec09 Deadlock
11/35Lec 9%119/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
Suggested ocuments for Gou to 3aintain
Project o"jecties+ goa!s# constraints# and priorities
Specifications+ t'e manua! p!us performance specs T'is s'ou!d "e t'e first document generated and t'e!ast one finis'ed
3eeting notes
ocument a!! decisionsGou can often cut paste for t'e design documents
Sc'edu!e+ W'at is your anticipated timingE T'is document is critica!A
Organi*ationa! C'art W'o is responsi"!e for )'at taskE
7/23/2019 Lec09 Deadlock
12/35Lec 9%129/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
.se Soft)are Too!s
Source reision contro! soft)are ?Su"ersion# C7S# ot'ersI@ asy to go "ack and see 'istory/undo mistakes 0igure out )'ere and )'y a "ug got introduced Communicates c'anges to eeryone ?use C7S8s features@
.se automated testing too!s Write scripts for non:interactie soft)are .se Fe4pectH for interactie soft)are &.nit+ automate unit testing 3icrosoft re"ui!ds t'e 7ista kerne! eery nig't )it' t'eday8s c'anges% eryone is running/testing t'e !atestsoft)are
.se :mai! and instant messaging consistent!y to!eae a 'istory trai!
7/23/2019 Lec09 Deadlock
13/35Lec 9%19/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
Test Continuous!y
Bntegration tests a!! t'e time# not at 11pm
on due dateA Write dummy stu"s )it' simp!e functiona!ity Let8s peop!e test continuous!y# "ut more )ork
Sc'edu!e periodic integration tests
7/23/2019 Lec09 Deadlock
14/35Lec 9%1;9/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
5dministriia
Project 1 Code ?and fina! design document@ ue Tuesday 1$/> ?ne4t TuesdayA@# ocument Wednesday
Project 2 starts after you are done )it' Project 1 5utograder issues
5utograder not intended to run freuent!y at "eginning 5ssume running eery ; 'ours or so at "eginning of )eek
We did 'ae pro"!ems oer t'e )eekend Dopefu!!y fi4ed "y no)
3idterm B coming up in t'ree )eeks+
3onday# 1$/1# Location+ 1>> )ine!!e Wi!! "e 'our e4am in eening ?>+$:+$ or 6+$$:9+$$@
S'ou!d "e 2 'our e4am )it' e4tra time C!osed "ook# one page of 'and:)ritten notes ?"ot' sides@ Topics+ eryt'ing up to preious Wednesday
=o c!ass on day of 3idterm B )i!! post e4tra office 'ours for peop!e )'o 'ae uestions a"out
t'e materia! ?or !ife# )'ateer@
7/23/2019 Lec09 Deadlock
15/35Lec 9%1>9/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
C:Language Support for Sync'roni*ation
C !anguage+ Pretty straig'tfor)ard sync'roni*ation
&ust make sure you kno) a!! t'e code pat's out of acritica! sectionint Rtn() {lock.acquire();if (exception) {
lock.release();return errReturnode;}lock.release();return !";}
Watc' out for set#mp/long#mpA Can cause a non:!oca! jump out of procedure Bn e4amp!e# procedure ca!!s !ongjmp# poping stack
"ack to procedure Bf Procedure C 'ad !ock%acuire# pro"!emA
Proc 5
Proc Ca!!s setjmp
Proc C!ock%acuire
Proc
Proc
Ca!!s !ongjmp
Stack
gro
)t'
7/23/2019 Lec09 Deadlock
16/35Lec 9%169/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
CQQ Language Support for Sync'roni*ation
Languages )it' e4ceptions !ike CQQ Languages t'at support e4ceptions are pro"!ematic ?easy
to make a non:!oca! e4it )it'out re!easing !ock@ Consider+
void Rtn() {lock.acquire();
$o%oo();lock.release();
}void $o%oo() {
if (exception) throw err&xception;
}
=otice t'at an e4ception in o0oo?@ )i!! e4it )it'outre!easing t'e !ock
7/23/2019 Lec09 Deadlock
17/35Lec 9%1J9/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
CQQ Language Support for Sync'roni*ation ?con8t@
3ust catc' a!! e4ceptions in critica! sections Catc' e4ceptions# re!ease !ock# and re:t'ro) e4ception+
void Rtn() {lock.acquire();tr' {
$o%oo();
} catch () { catch exceptionlock.release(); release lockthrow; rethrow the exception
}lock.release();
}
void $o%oo() {if (exception) throw err&xception;
}
en etter+ autoRptrT faci!ity% See CQQ Spec%
Can dea!!ocate/free !ock regard!ess of e4it met'od
7/23/2019 Lec09 Deadlock
18/35Lec 9%19/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
&aa Language Support for Sync'roni*ation
&aa 'as e4p!icit support for t'reads and t'read
sync'roni*ation ank 5ccount e4amp!e+class *ccount {private int +alance; o+#ect constructor
pu+lic *ccount (int initial,alance) {
+alance - initial,alance;}pu+lic synchronizedint get,alance() {
return +alance;}
pu+lic synchronized void deposit(int amount) {+alance - amount;
}}
ery o"ject 'as an associated !ock )'ic' getsautomatica!!y acuired and re!eased on entry and e4itfrom a sync'roni*ed met'od%
7/23/2019 Lec09 Deadlock
19/35Lec 9%199/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
&aa Language Support for Sync'roni*ation ?con8t@
&aa a!so 'as sync'roni*ed statements+s'nchroni/ed (o+#ect) {
}
Since eery &aa o"ject 'as an associated !ock# t'istype of statement acuires and re!eases t'e o"ject8s!ock on entry and e4it of t'e "ody
Works proper!y een )it' e4ceptions+
s'nchroni/ed (o+#ect) {$o%oo();
}void $o%oo() {
throw err&xception;}
7/23/2019 Lec09 Deadlock
20/35Lec 9%2$9/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
&aa Language Support for Sync'roni*ation ?con8t 2@
Bn addition to a !ock# eery o"ject 'as a sing!econdition aria"!e associated )it' it
Do) to )ait inside a sync'roni*ation met'od of "!ock+ void wait(long timeout); 0ait for timeout void wait(long timeout1 int nanoseconds); variant void wait();
Do) to signa! in a sync'roni*ed met'od or "!ock+
void notif'(); wakes up oldest waiter void notif'*ll(); like +roadcast1 wakes ever'one
Condition aria"!es can )ait for a "ounded !engt' oftime% T'is is usefu! for 'and!ing e4ception cases+
t2 - time.now();while (3*45Request()) {
wait (6&"7&R8!$);t9 - time.new();if (t9 : t2 ?485&) check5achine();
}
=ot a!! &aa 73s euia!entA ifferent sc'edu!ing po!icies# not necessari!y preemptieA
7/23/2019 Lec09 Deadlock
21/35Lec 9%219/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
7/23/2019 Lec09 Deadlock
22/35
Lec 9%229/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
esources M passie entities needed "y t'reads to do t'eir)ork
CP. time# disk space# memory
T)o types of resources+
Preempta"!e M can take it a)ay
CP.# m"edded security c'ip
=on:preempta"!e M must !eae it )it' t'e t'read
isk space# p!otter# c'unk of irtua! address space
3utua! e4c!usion M t'e rig't to enter a critica! section
esources may reuire e4c!usie access or may "e s'ara"!e
ead:on!y fi!es are typica!!y s'ara"!e
Printers are not s'ara"!e during time of printing
One of t'e major tasks of an operating system is tomanage resources
esources
7/23/2019 Lec09 Deadlock
23/35
Lec 9%29/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
Staration s ead!ock
Staration s% ead!ock Staration+ t'read )aits indefinite!y
4amp!e# !o):priority t'read )aiting for resources constant!y inuse "y 'ig':priority t'reads ead!ock+ circu!ar )aiting for resources
T'read 5 o)ns es 1 and is )aiting for es 2T'read o)ns es 2 and is )aiting for es 1
ead!ock Staration "ut not ice ersa Staration can end ?"ut doesn8t 'ae to@
ead!ock can8t end )it'out e4terna! interention
es 2es 1
T'read
T'read5
Wait0or
Wait0or
O)nedy
O)nedy
7/23/2019 Lec09 Deadlock
24/35
Lec 9%2;9/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
Conditions for ead!ock
ead!ock not a!)ays deterministic M 4amp!e 2 mute4es+4hread * 4hread ,
x.7(); '.7();'.7(); x.7();
'.@(); x.@();
x.@(); '.@();
ead!ock )on8t a!)ays 'appen )it' t'is code Dae to 'ae e4act!y t'e rig't timing ?F)rongH timingE@ So you re!ease a piece of soft)are# and you tested it# and
t'ere it is# contro!!ing a nuc!ear po)er p!antI
ead!ocks occur )it' mu!tip!e resources
3eans you can8t decompose t'e pro"!em Can8t so!e dead!ock for eac' resource independent!y
4amp!e+ System )it' 2 disk dries and t)o t'reads ac' t'read needs 2 disk dries to function
ac' t'read gets one disk and )aits for anot'er one
7/23/2019 Lec09 Deadlock
25/35
Lec 9%2>9/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
ridge Crossing 4amp!e
ac' segment of road can "e ie)ed as a resource Car must o)n t'e segment under t'em
3ust acuire segment t'at t'ey are moing into 0or "ridge+ must acuire "ot' 'a!es
Traffic on!y in one direction at a time Pro"!em occurs )'en t)o cars in opposite directions on"ridge+ eac' acuires one segment and needs ne4t
Bf a dead!ock occurs# it can "e reso!ed if one car "acksup ?preempt resources and ro!!"ack@ Seera! cars may 'ae to "e "acked up
Staration is possi"!e ast:going traffic rea!!y fast no one goes )est
7/23/2019 Lec09 Deadlock
26/35
Lec 9%269/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
Train 4amp!e ?Worm'o!e:outed =et)ork@
Circu!ar dependency ?ead!ockA@ ac' train )ants to turn rig't
!ocked "y ot'er trains Simi!ar pro"!em to mu!tiprocessor net)orks
0i4E Bmagine grid e4tends in a!! four directions 0orce ordering of c'anne!s?tracks@
Protoco!+ 5!)ays go east:)est first# t'en nort':sout'
Ca!!ed Fdimension orderingH ?U t'en G@
isa!!o)
ed
/y1u!e
7/23/2019 Lec09 Deadlock
27/35
Lec 9%2J9/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
ining La)yers Pro"!em
0ie c'opsticks/0ie !a)yers ?rea!!y c'eap restaurant@ 0ree:for a!!+ La)yer )i!! gra" any one t'ey can =eed t)o c'opsticks to eat
W'at if a!! gra" at same timeE ead!ockA
Do) to fi4 dead!ockE 3ake one of t'em gie up a c'opstick ?Da'A@ entua!!y eeryone )i!! get c'ance to eat
Do) to preent dead!ockE =eer !et !a)yer take !ast c'opstick if no 'ungry
!a)yer 'as t)o c'opsticks after)ards
7/23/2019 Lec09 Deadlock
28/35
Lec 9%29/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
0our reuirements for ead!ock
3utua! e4c!usion On!y one t'read at a time can use a resource%
Do!d and )ait T'read 'o!ding at !east one resource is )aiting toacuire additiona! resources 'e!d "y ot'er t'reads
=o preemption
esources are re!eased on!y o!untari!y "y t'e t'read'o!ding t'e resource# after t'read is finis'ed )it' it
Circu!ar )ait T'ere e4ists a set VT1# I# Tn of )aiting t'reads
T1is )aiting for a resource t'at is 'e!d "y T
2 T2is )aiting for a resource t'at is 'e!d "y T I Tnis )aiting for a resource t'at is 'e!d "y T1
7/23/2019 Lec09 Deadlock
29/35
Lec 9%299/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
Sym"o!s
esource:5!!ocation
7/23/2019 Lec09 Deadlock
30/35
Lec 9%$9/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
esource 5!!ocation
7/23/2019 Lec09 Deadlock
31/35
Lec 9%19/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
3et'ods for Dand!ing ead!ocks
5!!o) system to enter dead!ock and t'en recoer euires dead!ock detection a!gorit'm
Some tec'niue for forci"!y preempting resourcesand/or terminating tasks
nsure t'at system )i!! neerenter a dead!ock =eed to monitor a!! !ock acuisitions
Se!ectie!y deny t'ose t'at mig't!ead to dead!ock
Bgnore t'e pro"!em and pretend t'at dead!ocksneer occur in t'e system .sed "y most operating systems# inc!uding .=BU
7/23/2019 Lec09 Deadlock
32/35
Lec 9%29/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
T1
T2
T
2
1
T;
ead!ock etection 5!gorit'm
On!y one of eac' type of resource !ook for !oops 3ore
7/23/2019 Lec09 Deadlock
33/35
Lec 9%9/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
W'at to do )'en detect dead!ockE
Terminate t'read# force it to gie up resources Bn ridge e4amp!e#
7/23/2019 Lec09 Deadlock
34/35
Lec 9%;9/29/1$ (u"iato)ic* CS162 -.C 0a!! 2$1$
Summary
Suggestions for dea!ing )it' Project Partners Start ar!y# 3eet Often
ee!op
7/23/2019 Lec09 Deadlock
35/35
Summary ?2@
Tec'niues for addressing ead!ock 5!!o) system to enter dead!ock and t'en recoer nsure t'at system )i!! neerenter a dead!ock Bgnore t'e pro"!em and pretend t'at dead!ocks neeroccur in t'e system
ead!ock detection
5ttempts to assess )'et'er )aiting grap' can eermake progress
=e4t Time+ ead!ock preention 5ssess# for eac' a!!ocation# )'et'er it 'as t'epotentia! to !ead to dead!ock
anker8s a!gorit'm gies one )ay to assess t'is
Top Related