04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand,...
Post on 08-Jul-2020
15 Views
Preview:
TRANSCRIPT
Synchroniza+onBasicsandSemaphores
CS4410Opera+ngSystems
Spring2017CornellUniversityLorenzoAlvisi
AnneBracy
See:Ch5&6inOSPPtextbook
TheslidesaretheproductofmanyroundsofteachingCS4410byProfessorsSirer,Bracy,Agarwal,George,andVanRenesse.
Stack 2
ThreadsandtheirData
2
Data
Insns
Stack 3
Stack 3
PCSP
Thread 3
PCSP
Thread 2
PCSP
Thread 1
Threadshave:•Privatestack•Privateregisters•Sharedglobals•Whataboutdata?
(1)IndependentThreadsData
t1 working set
t2 working set
t3 working set
(2)CooperaPveThreadsData
t2t1
t3
(privatebyagreement)
2possibili+es:
Codelikethis:
2Threads,1SharedVariable
3
...amount-=10,000;...
...amount*=0.5;...
T1 T2
...r1=loadamountr1=r1–10,000storer1toamount...
...r2=loadamount
...
r2=0.5*r2storer2toamount...
T1T2
Mightexecutelikethis:
RaceCondiPons
4
ALLpossibleinterleavingsshouldbesafe!
Whenthebehaviorofaprogramdependsontheinterleavingofopera+onsofdifferentthreads.(Oncethreadstarts,itneedsto“race”tofinish.)
Numberofpossibleinterleavingsishuge•Someinterleavingsaregood•Someinterleavingsarebad:
•Badinterleavingsmayberare!•Works100;mes≠correct•CaseStudy:Therac-25
ProblemswithSequenPalReasoning
5
1.Programexecu+ondependsonthepossibleinterleavingsofthreads’accesstosharedstate.
2.Programexecu+oncanbenondeterminis+c.
3.Compilersandprocessorhardwarecanreorderinstruc+ons.
6
SemaphoresLocks Condi+onVariables
Mul+pleProcessors HardwareInterruptsHARDWARE
InterruptDisable AtomicR/WInstruc+onsATOMIC INSTRUCTIONS
SYNCHRONIZATION VARIABLES
CONCURRENT APPLICATIONS. . .
RaceCondiPonRevisited
7
disable_interrupts();r1=loadamountr1=r1–10,000storer1toamountenable_interrupts();
disable_interrupts();r2=loadamountr2=0.5*r2storer2toamountenable_interrupts();
T1 T2
Thatwaseasy….classdismissed?
r1
r2
T2 Register File
TestandSet
8
tasr1,0(r2):r1⬅ 0(r2)#test0(r2)⬅ 1#set
0-free1-takenr1
r2
T1 Register File Memory
110
MIPS version:
0
•atomichardwareprimi+ve•typicallyamul+-cyclebusopera+onthatatomicallyreadsandupdatesamemoryloca+on
•supportsmutualexclusion
TestandSettoprovideMutualExclusion
9
acquire(int*lock){while(test_and_set(lock)) /*donothing*/;}
release(int*lock){ *lock=0;}
ATOMICintTestAndSet(int*var){ intoldVal=*var; *var=1; returnoldVal;}
CsemanPcsofTest-And-Set
RaceCondiPonRevisited
10
acquire();while(test_and_set(lock))while(test_and_set(lock))while(test_and_set(lock))
while(test_and_set(lock))r1=loadamountr1=r1–10,000storer1toamountrelease();
acquire();r2=loadamount
r2=0.5*r2storer2toamountrelease();
T1 T2
Now with Locks!Isthisagood
soluPon?
yield()
11
Thou shalt not busy-wait!
12
SemaphoresLocks Condi+onVariables
Mul+pleProcessors HardwareInterruptsHARDWARE
InterruptDisable AtomicR/WInstruc+onsATOMIC INSTRUCTIONS
SYNCHRONIZATION VARIABLES
CONCURRENT APPLICATIONS. . .
Semaphores
13
DijkstraintroducedintheTHEOpera+ngSystem
•Stateful:•asemaphorehasanonnega+veVALUEassociatedwithit
•valueisincrementedanddecrementedatomically
•Interface•Twoopera+ons:P()andV()•Noopera+ontoreadthevalue!
[Dijkstra 1962]
P(S):•waitun+lvalueisposi+ve•whenso,atomicallydecrementVALUEby1
V(S):•incrementVALUEby1•resumeathreadwai+ngonP(ifany)
SemaphoreOperaPons:PandV
14
P(S){while(S<=0);S-=1;}
V(S){S+=1;}
Dutch 4410: P = Probeer (‘Try') and V = Verhoog ('Increment', 'Increase by one')
BinarySemaphore
15
Semaphorevalueiseither0or1• Usedformutualexclusion
(semaphoreasamoreefficientlock)• Ini+ally1inthatcase
P(S)CriticalSection()V(S)
P(S)CriticalSection()V(S)
T1 T2
SemaphoreSS.init(1)
Semacountcanbeanyinteger• Usedforsignalingorcoun+ngresources• Typically:
• onethreadperformsP()toawaitevent• anotherthreadperformsV()toalertwai+ngthreadthateventhasoccurred
CounPngSemaphores
16
PacketProcessor():x=get_packet_from_card()enqueue(packetq,x);V(packetarrived);
NetworkingThread():P(packetarrived);x=dequeue(packetq);print_contents(x);
T1 T2
Semaphorepacketarrivedpacketarrived.init(0)
17
P(Sema*s){ifcountbigenough
decrementcount else{
makenoteofthreadstopthread
}}
PossibleSemaphoreimplementaPon
P1Context:nopreempPon(threadsrununPltheyyield)
P(S){while(S<=0);S-=1;}
V(Sema*s){ ifnoonewaitingonsincrementcount elsewakeaninterestedthread
}}
V(S){S+=1;}
Producer-ConsumerProblem
18
2+threadscommunicate:somethreadsproducedatathatothersconsume
Boundedbuffer:sizeNProducerprocesswritesdatatobuffer• Writestoinandmovesrightwards• Don’twritemorethanN!Consumerprocessreadsdatafrombuffer• Readsfromoutandmovesrightwards• Don’tconsumeifthereisnodata!
Example:“pipe”(|)inUnix>catfile|sort|uniq|more
0 N-1
in out
SoluPon#1:NoProtecPon
19
//additemtobuffervoidproduce(intitem){buf[in]=item;in=(in+1)%N;}
//removeitemintconsume(){intitem=buf[out];out=(out+1)%N;returnitem;}
Problems:1.Unprotectedsharedstate(mul+pleproducers/consumers)2.Inventory:•Consumercouldconsumewhennothingisthere!•Producercouldoverwritenot-yet-consumeddata!
Shared:intbuf[N];intin,out;
SoluPon#2:AddMutexSemaphores
20
//additemtobuffervoidproduce(intitem){P(mutex_prod);
buf[in]=item;in=(in+1)%N;V(mutex_prod);
}
//removeitemintconsume(){P(mutex_cons);intitem=buf[out];out=(out+1)%N;V(mutex_cons);returnitem;}
Shared:intbuf[N];intin,out;Semaphoremutex_prod(1),mutex_cons(1);
now atomic
SoluPon#3:AddCommunicaPonSemaphores
21
voidproduce(intitem){P(enoughRoom);//space?P(mutex_prod);
buf[in]=item;in=(in+1)%N;V(mutex_prod);V(dataThere);//item!
}
intconsume(){P(dataThere);//need itemP(mutex_cons);intitem=buf[out];out=(out+1)%N;V(mutex_cons);V(enoughRoom);//space!
returnitem;}
Shared:intbuf[N];intin,out;Semaphoremutex_prod(1),mutex_cons(1);SemaphoreenoughRoom(N),dataThere(0);
DONEFORTODAY!
22
ClassicSemaphoreMistakes
23
P(S)CSP(S)
I
V(S)CSV(S)
P(S)CS
J
K
P(S)if(x)return;CSV(S)
L
Istuckon2ndP().Subsequentprocessesfreezeupon1stP().
Underminesmutex:•Jdoesn’tgetpermissionviaP()•“extra”V()sallowotherprocessesintotheCSinappropriately
NextcalltoP()willfreezeup.Confusingbecausetheotherprocesscouldbecorrectbuthangswhenyouuseadebuggertolookatitsstate!
ConditionalcodecanchangecodeflowintheCS.Causedbycode
updates(bugfixes,etc.)bysomeoneotherthanoriginalauthorofcode.
⬅typo
⬅typo
⬅omission
SemaphoresConsideredHarmful
24
“Duringsystemconcep+onittranspiredthatweusedthesemaphoresintwocompletelydifferentways.Thedifferenceissomarkedthat,lookingback,onewonderswhetheritwasreallyfairtopresentthetwowaysasusesoftheverysameprimi+ves.Ontheonehand,wehavethesemaphoresusedformutualexclusion,ontheotherhand,theprivatesemaphores.”
—Dijkstra“Thestructureofthe’THE’-Mul+programmingSystem”Communica+onsoftheACMv.11n.5May1968.
SemaphoresNOTtotherescue!
25
Semaphoresare“low-level”primi+ves.Smallerrors:• Easilybringsystemtogrindinghalt• Verydifficulttodebug
Twousagemodels:• Mutualexclusion:“real”abstrac+onisacri+calsec+on• Communica:on:threadsusesemaphorestocommunicate(e.g.,boundedbufferexample)
Simplifica:on:Provideconcurrencysupportincompiler➙EnterMonitors
top related