25
Synchroniza+on Basics and Semaphores CS 4410 Opera+ng Systems Spring 2017 Cornell University Lorenzo Alvisi Anne Bracy See: Ch 5&6 in OSPP textbook The slides are the product of many rounds of teaching CS 4410 by Professors Sirer, Bracy, Agarwal, George, and Van Renesse.

04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

  • Upload
    others

  • View
    19

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

Synchroniza+onBasicsandSemaphores

CS4410Opera+ngSystems

Spring2017CornellUniversityLorenzoAlvisi

AnneBracy

See:Ch5&6inOSPPtextbook

TheslidesaretheproductofmanyroundsofteachingCS4410byProfessorsSirer,Bracy,Agarwal,George,andVanRenesse.

Page 2: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

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:

Page 3: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

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:

Page 4: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

RaceCondiPons

4

ALLpossibleinterleavingsshouldbesafe!

Whenthebehaviorofaprogramdependsontheinterleavingofopera+onsofdifferentthreads.(Oncethreadstarts,itneedsto“race”tofinish.)

Numberofpossibleinterleavingsishuge•Someinterleavingsaregood•Someinterleavingsarebad:

•Badinterleavingsmayberare!•Works100;mes≠correct•CaseStudy:Therac-25

Page 5: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

ProblemswithSequenPalReasoning

5

1.Programexecu+ondependsonthepossibleinterleavingsofthreads’accesstosharedstate.

2.Programexecu+oncanbenondeterminis+c.

3.Compilersandprocessorhardwarecanreorderinstruc+ons.

Page 6: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

6

SemaphoresLocks Condi+onVariables

Mul+pleProcessors HardwareInterruptsHARDWARE

InterruptDisable AtomicR/WInstruc+onsATOMIC INSTRUCTIONS

SYNCHRONIZATION VARIABLES

CONCURRENT APPLICATIONS. . .

Page 7: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

RaceCondiPonRevisited

7

disable_interrupts();r1=loadamountr1=r1–10,000storer1toamountenable_interrupts();

disable_interrupts();r2=loadamountr2=0.5*r2storer2toamountenable_interrupts();

T1 T2

Thatwaseasy….classdismissed?

Page 8: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

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

Page 9: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

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

Page 10: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

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()

Page 11: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

11

Thou shalt not busy-wait!

Page 12: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

12

SemaphoresLocks Condi+onVariables

Mul+pleProcessors HardwareInterruptsHARDWARE

InterruptDisable AtomicR/WInstruc+onsATOMIC INSTRUCTIONS

SYNCHRONIZATION VARIABLES

CONCURRENT APPLICATIONS. . .

Page 13: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

Semaphores

13

DijkstraintroducedintheTHEOpera+ngSystem

•Stateful:•asemaphorehasanonnega+veVALUEassociatedwithit

•valueisincrementedanddecrementedatomically

•Interface•Twoopera+ons:P()andV()•Noopera+ontoreadthevalue!

[Dijkstra 1962]

Page 14: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

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')

Page 15: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

BinarySemaphore

15

Semaphorevalueiseither0or1• Usedformutualexclusion

(semaphoreasamoreefficientlock)• Ini+ally1inthatcase

P(S)CriticalSection()V(S)

P(S)CriticalSection()V(S)

T1 T2

SemaphoreSS.init(1)

Page 16: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

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)

Page 17: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

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;}

Page 18: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

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

Page 19: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

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;

Page 20: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

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

Page 21: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

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);

Page 22: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

DONEFORTODAY!

22

Page 23: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

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

Page 24: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

SemaphoresConsideredHarmful

24

“Duringsystemconcep+onittranspiredthatweusedthesemaphoresintwocompletelydifferentways.Thedifferenceissomarkedthat,lookingback,onewonderswhetheritwasreallyfairtopresentthetwowaysasusesoftheverysameprimi+ves.Ontheonehand,wehavethesemaphoresusedformutualexclusion,ontheotherhand,theprivatesemaphores.”

—Dijkstra“Thestructureofthe’THE’-Mul+programmingSystem”Communica+onsoftheACMv.11n.5May1968.

Page 25: 04.sync and semaphores - cs.cornell.edu · semaphores used for mutual exclusion, on the other hand, the private semaphores.” — Dijkstra “The structure of the ’THE’-Mul+programming

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