欢迎来到天天文库
浏览记录
ID:40414216
大小:207.34 KB
页数:29页
时间:2019-08-02
《川丹甫制冷压缩机股份有限公司招(I)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、OutlineforTodayObjective:Tocontinuetalkingaboutthecriticalsectionproblemandgetmorepracticethinkingaboutpossibleinterleavings.Starttalkingaboutsynchronizationprimitives.Introduceother“classic”concurrencyproblemsAdministrativedetails:LookonthewebforTAs’officehoursorc
2、hecknewsgroupforUTAs’officehoursforassignment1.Oncollectingproblemsets…SemaphoresWell-knownsynchronizationabstractionDefinedasanon-negativeintegerwithtwoatomicoperationsP(s)-[waituntils>0;s--]V(s)-[s++]Theatomicityandthewaitingcanbeimplementedbyeitherbusywaitingorb
3、lockingsolutions.Reminder:notation[]=atomicSemaphoreUsageBinarysemaphorescanprovidemutualexclusion(solutionofcriticalsectionproblem)Countingsemaphorescanrepresentaresourcewithmultipleinstances(e.g.solvingproducer/consumerproblem)Signalingevents(persistenteventsthat
4、stayrelevantevenifnobodylisteningrightnow)while(1){...otherstuff...criticalsection}TheCriticalSectionProblemP(mutex)V(mutex)Semaphore:mutexinitially1FillintheboxesProducer/ConsumerProducer:while(whatever){locallygenerateitemfillemptybufferwithitem}Consumer:while(wh
5、atever){getitemfromfullbufferuseitem}Producer/ConsumerProducer:while(whatever){locallygenerateitemfillemptybufferwithitem}Consumer:while(whatever){getitemfromfullbufferuseitem}P(emptybuf);V(fullbuf);P(fullbuf);V(emptybuf);Semaphores:emptybufinitiallyN;fullbufinitia
6、lly0;TweedledumandTweedledeeSeparatethreadsexecutingtheirrespectiveprocedures.TheideatocausethemtoforevertaketurnsexchanginginsultsthroughthesharedvariableXinstrictalternation.TheSleep()andWakeup()routinesoperateasfollows:Sleepblocksthecallingthread,Wakeupunblocksa
7、specificthreadifthatthreadisblocked,otherwiseitsbehaviorisunpredictableThecodeshownaboveexhibitsawell-knownsynchronizationflaw.Outlineascenarioinwhichthiscodewouldfail,andtheoutcomeofthatscenariovoidTweedledum(){while(1){Sleep();x=Quarrel(x);Wakeup(Tweedledee);}}vo
8、idTweedledee(){while(1){x=Quarrel(x);Wakeup(Tweedledum);Sleep();}}Ifdeegoesfirsttosleep,thewakeupislost(sincedumisn’tsleepingyet).Bothsleepforeve
此文档下载收益归作者所有