欢迎来到天天文库
浏览记录
ID:43515645
大小:374.42 KB
页数:25页
时间:2019-10-09
《lecture_6_Cooperating Threads》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、CooperatingThreadsZhaoFangOperatingSystems-forthecollegestudentsGoalsforTodayWhyallowcooperatingthreads?WhatareAtomicOperations?BackgroundProcessescanexecuteconcurrentlyMaybeinterruptedatanytime,partiallycompletingexecutionConcurrentaccesstoshareddatamayresultindatainconsi
2、stencyMaintainingdataconsistencyrequiresmechanismstoensuretheorderlyexecutionofcooperatingprocessesIndependentThreadsNostatessharedwithotherthreadsDeterministiccomputationOutputdependsoninputReproducibleOutputdoesnotdependontheorderandtimingofotherthreadsSchedulingorderd
3、oesnotmattere.g.,compilersCooperatingThreadsSharedstatesNondeterministicNon-reproduciblee.g.,elevatorcontrollersExample:2threadssharingthesamedisplayThreadAThreadBcout<<“ABC”;cout<<“123”;Youmayget“A12BC3”So,WhyAllowCooperatingThreads?Sharedresourcese.g.,asingleprocesso
4、rSpeedupOccurswhenthreadsusedifferentresourcesatdifferenttimesModularityAnapplicationcanbedecomposedintothreadsHigh-levelExample:WebServerServermusthandlemanyrequestsNon-cooperatingversion:serverLoop(){con=AcceptCon();ProcessFork(ServiceWebPage(),con);}Whataresomedisadva
5、ntagesofthistechnique?ThreadedWebServerNow,useasingleprocessMultithreaded(cooperating)version:serverLoop(){connection=AcceptCon();ThreadFork(ServiceWebPage(),connection);}ThreadPoolsProblemwithpreviousversion:UnboundedThreadsWhenweb-sitebecomestoopopular–throughputsinksIns
6、tead,allocateabounded“pool”ofworkerthreads,representingthemaximumlevelofmultiprogrammingqueueMasterThreadThreadPoolContinue……worker(queue){while(TRUE){con=Dequeue(queue);if(con==null)sleepOn(queue);elseServiceWebPage(con);}master(){}allocThreads(worker,queue);while(TRUE){con=Ac
7、ceptCon();Enqueue(queue,con);wakeUp(queue);}}SomeConcurrentProgramsIfthreadsworkonseparatedata,schedulingdoesnotmatterThreadAThreadBx=1;y=2;SomeConcurrentProgramsIfthreadssharedata,thefinalvaluesarenotasobviousThreadAThreadBx=1;y=2;x=y+1;y=y*2;Whataretheindivisibleoperations
8、?AtomicOperationsAnatomicoperationalwaysrunstocomplet
此文档下载收益归作者所有