Chapter6:ProcessSynchronization沈卓炜h@dzwshen@seu.edu.cn九龙湖校区计算机楼347房间52090919Chapter6:ProcessShitiSynchronizationBackgrondBackgroundTheCritical-SectionProblemSynchronizationHardwareShSemaphoresClassicalProblemsofSynchronizationyMonitorsSSynchronizationExamplesSoutheastUniversity6.2OperatingSystemConceptsBackgroundConcurrentaccesstoshareddatamayresultindatainconsistency.yMaintainingdataconsistencyrequiresmechanismstoensuretheorderlyexecutionofmechanismstoensuretheorderlyexecutionofcooperatingprocesses.Shared-memorysolutiontobounded-bufferproblem(Chapter3)allowsatmostn–1itemsproblem(Chapter3)allowsatmostn1itemsinbufferatthesametime.Asolution,whereallNbuffersareusedisnotsimpleNbuffersareusedisnotsimple.Supposethatwemodifytheproducer-consumerdbddiiblSoutheastUniversity6.3OperatingSystemConceptscodebyaddingavariablecounterBounded-BufferShareddata#defineBUFFER_SIZE10typedefstruct{...}item;itembuffer[BUFFER_SIZE];intin=0;intout=0;intcounter=0;SoutheastUniversity6.4OperatingSystemConceptsBounded-BufferoudedueProducerprocessitemnextProduced;itemnextProduced;while(1){while(counter==BUFFERSIZE)while(counter==BUFFER_SIZE);/*donothing*/buffer[in]=nextProduced;in=(in+1)%BUFFERSIZE;in=(in+1)%BUFFER_SIZE;counter++;SoutheastUniversity6.5OperatingSystemConcepts}Bounded-BufferoudedueConsumerprocessitemnextConsumed;while(1){while(counter==0)while(counter==0);/*donothing*/tCdbff[t]nextConsumed=buffer[out];out=(out+1)%BUFFER_SIZE;counter--;}SoutheastUniversity6.6OperatingSystemConcepts}BoundedBufferoudedueThestatementscounter++;counter--;mustbeperformedatomically.Atomicoperationmeansanoperationthatcompletesinitsentiretywithoutinterruption.SoutheastUniversity6.7OperatingSystemConceptsBoundedBufferThttt“t”biltdThestatement“count++”maybeimplementedinmachinelanguageas:register1=counterregister1counterregister1=register1+1tit1counter=register1Thestatement“count--”maybeimplementedypas:register2=counterregister2=counterregister2=register2–1counter=register2SoutheastUniversity6.8OperatingSystemConceptscounter=register2BoundedBufferoudedueIfboththeprodcerandconsmerattemptIfboththeproducerandconsumerattempttoupdatethebufferconcurrently,theassemblylanguagestatementsmaygetinterleaved.ItliddhthdInterleavingdependsuponhowtheproducerandconsumerprocessesarescheduled.SoutheastUniversity6.9OperatingSystemConceptsBoundedBufferAtiiitill5OitliAssumecounterisinitially5.Oneinterleavingofstatementsis:producer:register1=counter(register1=5)producer:register1=register1+1(register1=producer:register1register11(register16)consumer:register2=counter(register2=5)consumer:register2=counter(register2=5)consumer:register2=register2–1(register24)=4)producer:counter=register1(counter=6)consumer:counter=register2(counter=4)Thevalueofcountmaybeeither4or6whereSoutheastUniversity6.10OperatingSystemConceptsThevalueofcountmaybeeither4or6,wherethecorrectresultshouldbe5.ProducerConsumerregister1=counterregister2=counterregister1=counterregister1=register1+1register2=counterregister2=register2–1egsteegstecounter=register1register2=register2–1counter=register2counterregister2SoutheastUniversity6.11OperatingSystemConceptsRaceConditionaceCodtoRaceconditionoccrsifRaceconditionoccurs,if:twoormoreprocesses/threadsaccessandmanipulatethesamedataconcurrently,andtheoutcomeoftheexecutiondependsonptheparticularorderinwhichtheaccesstakesplace.pTtdititTopreventraceconditions,concurrentprocessesmustbesynchronized.SoutheastUniversity6.12OperatingSystemConceptsChapter6:ProcessShitiSynchronizationBackgrondBackgroundTheCritical-SectionProblemSynchronizationHardwareShSemaphoresClassicalProblemsofSynchronizationyMonitorsSSynchronizationExamplesSoutheastUniversity6.13OperatingSystemConceptsTheCritical-SectionProblemeCtcaSectoobenprocessesallcompetingtousesomenprocessesallcompetingtousesomeshareddataEachprocesshasacodesegment,calledcriticalsection,inwhichtheshareddatais,accessed.ProblemensurethatwhenoneprocessisProblem–ensurethatwhenoneprocessisexecutinginitscriticalsection,nootherilldttiititilprocessisallowedtoexecuteinitscriticalsection.SoutheastUniversity6.14OperatingSystemConceptsCriticalSectionandMutualExclusionThstheeectionofcriticalsectionsmstThus,theexecutionofcriticalsectionsmustbemutuallyexclusive(e.g.,atmostoneprocesscanbeinitscriticalsectionatanytime).)Thecritical-sectionproblemistodesignaprotocolthatprocessescanusetoprotocolthatprocessescanusetocooperate.SoutheastUniversity6.15OperatingSystemConceptsTheCriticalSectionProtocoleCtcaSectootocoAcriticalsectionAcriticalsectionprotocolconsistsoftwoparts:anentrytwoparts:anentrysectionandanexitsectionsection.Betweenthemisthecriticalsectionthatmustruninamutuallyexclusiveway.SoutheastUniversity6.16OperatingSystemConceptsSolutiontoCritical-SectionPblProblemAnysolutiontothecriticalsectionproblemmustsatisfythefollowingthreeconditions:MutualExclusionMutualExclusionProgressBoundedWaitingBoundedWaitingMoreover,thesolutioncannotdependonrelativespeedofprocessesandschedulingpolicy.policy.Southeas