欢迎来到天天文库
浏览记录
ID:38367554
大小:1.11 MB
页数:69页
时间:2019-06-11
《操作系统概念(第七版_英文版)ch6》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Chapter6:ProcessSynchronizationModule6:ProcessSynchronizationBackgroundTheCritical-SectionProblemPeterson’sSolutionSynchronizationHardwareSemaphoresClassicProblemsofSynchronizationMonitorsSynchronizationExamplesAtomicTransactionsBackgroundConcurrentaccesstoshareddatamayresultindatain
2、consistencyMaintainingdataconsistencyrequiresmechanismstoensuretheorderlyexecutionofcooperatingprocessesSupposethatwewantedtoprovideasolutiontotheconsumer-producerproblemthatfillsallthebuffers.Wecandosobyhavinganintegercountthatkeepstrackofthenumberoffullbuffers.Initially,countissett
3、o0.Itisincrementedbytheproducerafteritproducesanewbufferandisdecrementedbytheconsumerafteritconsumesabuffer.SharedMemoryProducer:/*produceanitemandputinnextProduced*/while(true){while(count==BUFFER_SIZE);//donothingbuffer[in]=nextProduced;in=(in+1)%BUFFER_SIZE;count++;}Consumer:/*con
4、sumetheiteminnextConsumed*/while(true){while(count==0);//donothingnextConsumed=buffer[out];out=(out+1)%BUFFER_SIZE;count--;}RaceConditioncount++couldbeimplementedasregister1=countregister1=register1+1count=register1count--couldbeimplementedasregister2=countregister2=register2-1co
5、unt=register2Considerthisexecutioninterleavingwith“count=5”initially:S0:producerexecuteregister1=count{register1=5}S1:producerexecuteregister1=register1+1{register1=6}S2:consumerexecuteregister2=count{register2=5}S3:consumerexecuteregister2=register2-1{register2=4}S4:producerexec
6、utecount=register1{count=6}S5:consumerexecutecount=register2{count=4}WhataboutthecaseifwereversedtheorderofthestatementsatS4andS5?RaceCondition:theoutcomeoftheexecutiondependsontheparticularorderinwhichtheaccesstakesplace.register1andregister2maybethesamephysicalregister,whatwillhap
7、pen?Critical-SectionProblemCritical-Section:inwhichtheprocessesmaybechangingcommonvariables,updatingatable,writingafile,andsoonWemustdesignaprotocolthattheprocessescanusetocooperate,whenoneprocessisexecutinginitscriticalsection,nootherprocessistobeallowedtoexecuteinthiscriticalsectio
8、n.Entrysecti
此文档下载收益归作者所有