您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 操作系统并发性互斥和同步
1Concurrency:MutualExclusionandSynchronizationChapter52ConcurrencyOverviewThecentralthemesofoperatingsystemdesignareallconcernedwiththemanagementofprocessesandthreads:MultiprogrammingMultiprocessingDistributedprocessingFundamentaltooperatingsystemdesignisconcurrency(并发)TheBasicrequirementforsupportofconcurrencyistheabilitytoenforcemutualexclusion(互斥)3ConcurrencyContexts(并发上下文)Concurrencyarisesinthreedifferentcontexts:MultipleapplicationsMultiprogrammingStructuredapplicationApplicationcanbeasetofconcurrentprocessesorthreadsOperating-systemstructureOperatingsystemisasetofprocessesorthreads4DesignIssuesofConcurrencyCommunicationamongprocessesSharingandcompeting(竞争)ofresourcesSynchronizationoftheactivitiesofmultipleprocessesAllocationofprocessortimetoprocesses5PrinciplesofConcurrencyInterleavingOverlappingBasiccharacteristicofmultiprogrammingsystems:relativespeedofexecutionofprocessescannotbepredicted6DifficultiesofConcurrencySharingofglobalresourcesForexample:GlobalvariableOperatingsystemmanagingtheallocationofresourcesoptimallyForexample:I/OchannelDifficulttolocateprogrammingerrorsProgramresultsarenotdeterministicandreproducible(再现)7ASimpleExamplevoidecho(){chin=getchar();chout=chin;putchar(chout);}ProcessP1ProcessP2..chin=getchar();..chin=getchar();chout=chin;chout=chin;putchar(chout);..putchar(chout);..8RaceCondition(竞争条件)WhenmultipleprocessesorthreadsaccesssharedresourcesothatthefinalresultdependsontheorderofexecutionofinstructionsinthemultipleprocessesMurphyLawIfSomethingcangowrong,itwill.SolutiontoRaceCondition:ControlaccesstothesharedresourceMutualExclusion(互斥)9ProcessInteractionThreepossibledegreesofawarenessamongprocesses:ProcessesunawareofeachotherCompetitionProcessesindirectlyawareofeachotherCooperationProcessdirectlyawareofeachotherCooperation10CompetitionAmongProcessesforResourcesCriticalsections(临界区)PortionoftheprogramthataccesssharedresourceOnlyoneprogramatatimeisallowedinitscriticalsectionExampleonlyoneprocessatatimeisallowedtosendcommandtotheprinterEnforcementofmutualexclusionincurstwoadditionalproblems:Deadlock(死锁)Starvation(饥饿)11MutualExclusion/*programmutualexclusion*/constintn=/*numberofprocesses*/;voidP(inti){while(true){entercritical(i);/*criticalsection*/;exitcritical(i);/*remainder*/}}voidmain(){parbegin(P(1),P(2)…,P(n));}12RequirementsforMutualExclusionOnlyoneprocessatatimeisallowedinthecriticalsectionforaresourceAprocessthathaltsinitsnoncriticalsection(非临界区)mustdosowithoutinterferingwithotherprocessesNodeadlockorstarvation13RequirementsforMutualExclusion(Cont.)AprocessmustnotbedelayedaccesstoacriticalsectionwhenthereisnootherprocessusingitNoassumptionsaremadeaboutrelativeprocessspeedsornumberofprocessesAprocessremainsinsideitscriticalsectionforafinitetimeonly14LockVariables(锁变量)ThesimplestapproachwecanimagineHowdoesitwork?15MutualExclusion:HardwareSupportInterruptDisabling(关中断)AprocessrunsuntilitinvokesanoperatingsystemserviceoruntilitisinterruptedDisablinginterruptsguaranteesmutualexclusionProcessorislimitedinitsabilitytointerleaveprogramsMultiprocessingdisablinginterruptsononeprocessorwillnotguaranteemutualexclusion16MutualExclusion:HardwareSupportSpecialMachineInstructionsPerformedinasingleinstructioncycleAccesstothememorylocationisblockedforanyotherinstructionsTestandSetInstructionExchangeInstruction17TestandSetInstructionbooleantestset(inti){if(i==0){i=1;returntrue;}else{returnfalse;}}18voidexchange(intregister,intmemory){inttemp;temp=memory;memory=register;register=temp;}ExchangeInstruction19TestandSetforMutualExclusionvoidmain(){bolt=0;parbegin(P(1),P(2),...,P(n));}/*programmutualexclusion*/constintn=/*numberofprocesses*/;intbolt;voidP(inti){while(true){while(!testset(bolt))/*donothing*/;/*criticalsection*/;bolt=0;/*remainder*/}}20ExchangeforMutualExclusionvoidmain(){bolt=0;parbegin(P(1),P(2),...,P(n));}/*programmutualexclusion*/constintn=/*numberofprocesses*/;intbolt;voidP(inti){intkeyi=1;while(true){doexchange(keyi,bolt)while(keyi!=0);/*criticalsection*/;exchange(keyi,bolt);/*remainder*/}}21ExchangeforMutualExclusionvoidmain(){bolt=0;parbegin(P(1),P(2),...,P(n));}/*programmutualexclusion*/constintn=/*numberofprocesses*/;intbolt;voidP(inti){intkeyi=1;while(true){while(keyi!=0)exchange(keyi,bolt);/*criticalsection*/;exchange(keyi,bolt);/*remainder*/}}22AdvantagesofMachine-instructionApproachApplicabletoanynumberofprocessesoneitherasingleprocessorormultipleprocessorssharingmainmemoryItissimpleandthereforeeasytoverifyItcanbeusedtosupportmultiplecriticalsections23DisadvantagesofMachine-instructionApproachBusy-waiting(忙等待)consumesprocessortimeStarvationispossiblewhenaprocessleavesacriticalsectionandmorethanoneprocessiswaiting.DeadlockIfalowpriorityprocesshasthecriticalregionandahigherpriorityprocessneeds,thehigherpriorityprocesswillobtaintheprocessortowaitforthecriticalregionPriorityInversionProblem(优先级翻转问题)24Semaphores(信号量)1965,DijkstraSpecialvariablecalledasemaphoreisusedforsignalingTwoprimitives(原语)ared
本文标题:操作系统并发性互斥和同步
链接地址:https://www.777doc.com/doc-2381253 .html