欢迎来到天天文库
浏览记录
ID:56373045
大小:773.00 KB
页数:69页
时间:2020-06-14
《操作系统概念(第七版英文版).ppt》由会员上传分享,免费在线阅读,更多相关内容在PPT专区-天天文库。
1、Chapter6:ProcessSynchronizationModule6:ProcessSynchronizationBackgroundTheCritical-SectionProblemPeterson’sSolutionSynchronizationHardwareSemaphoresClassicProblemsofSynchronizationMonitorsSynchronizationExamplesAtomicTransactionsBackgroundConcurrentaccesstos
2、hareddatamayresultindatainconsistencyMaintainingdataconsistencyrequiresmechanismstoensuretheorderlyexecutionofcooperatingprocessesSupposethatwewantedtoprovideasolutiontotheconsumer-producerproblemthatfillsallthebuffers.Wecandosobyhavinganintegercountthatkeep
3、strackofthenumberoffullbuffers.Initially,countissetto0.Itisincrementedbytheproducerafteritproducesanewbufferandisdecrementedbytheconsumerafteritconsumesabuffer.SharedMemoryProducer:/*produceanitemandputinnextProduced*/while(true){while(count==BUFFER_SIZE);//
4、donothingbuffer[in]=nextProduced;in=(in+1)%BUFFER_SIZE;count++;}Consumer:/*consumetheiteminnextConsumed*/while(true){while(count==0);//donothingnextConsumed=buffer[out];out=(out+1)%BUFFER_SIZE;count--;}RaceConditioncount++couldbeimplementedasregister1=count
5、register1=register1+1count=register1count--couldbeimplementedasregister2=countregister2=register2-1count=register2Considerthisexecutioninterleavingwith“count=5”initially:S0:producerexecuteregister1=count{register1=5}S1:producerexecuteregister1=register1+
6、1{register1=6}S2:consumerexecuteregister2=count{register2=5}S3:consumerexecuteregister2=register2-1{register2=4}S4:producerexecutecount=register1{count=6}S5:consumerexecutecount=register2{count=4}WhataboutthecaseifwereversedtheorderofthestatementsatS4and
7、S5?RaceCondition:theoutcomeoftheexecutiondependsontheparticularorderinwhichtheaccesstakesplace.register1andregister2maybethesamephysicalregister,whatwillhappen?Critical-SectionProblemCritical-Section:inwhichtheprocessesmaybechangingcommonvariables,updatingat
8、able,writingafile,andsoonWemustdesignaprotocolthattheprocessescanusetocooperate,whenoneprocessisexecutinginitscriticalsection,nootherprocessistobeallowedtoexecuteinthiscriticalsection.Entrysecti
此文档下载收益归作者所有