4.1Providetwoprogrammingexamplesinwhichmultithreadingdoesnotprovidebetterperformancethanasingle-threadedsolutionAnswer:(1)Anykindofsequentialprogramisnotagoodcandidatetobethreaded.Anexampleofthisisaprogramthatcalculatesanindividualtaxreturn.(2)AnotherexampleisashellprogramsuchastheC-shellorKornshell.Suchaprogrammustcloselymonitoritsownworkingspacesuchasopenfiles,environmentvariables,andcurrentworkingdirectory.4.2Describetheactionstakenbyathreadlibrarytocontextswitchbetweenuser-levelthreads.Answer:Contextswitchingbetweenuserthreadsisquitesimilartoswitchingbetweenkernelthreads,althoughitisdependentonthethreadslibraryandhowitmapsuserthreadstokernelthreads.Ingeneral,contextswitchingbetweenuserthreadsinvolvestakingauserthreadofitsLWPandreplacingitwithanotherthread.Thisacttypicallyinvolvessavingandrestoringthestateoftheregisters.4.3Underwhatcircumstancesdoesamultithreadedsolutionusingmultiplekernelthreadsprovidebetterperformancethanasingle-threadedsolutiononasingle-processorsystem?Answer:Whenakernelthreadsuffersapagefault,anotherkernelthreadcanbeswitchedintousetheinterleavingtimeinausefulmanner.Asingle-threadedprocess,ontheotherhand,willnotbecapableofperformingusefulworkwhenapagefaulttakesplace.Therefore,inscenarioswhereaprogrammightsufferfromfrequentpagefaultsorhastowaitforothersystemevents,amulti-threadedsolutionwouldperformbetterevenonasingle-processorsystem.4.4Whichofthefollowingcomponentsofprogramstatearesharedacrossthreadsinamultithreadedprocess?a.Registervaluesb.Heapmemoryc.Globalvariablesd.StackmemoryAnswer:Thethreadsofamultithreadedprocessshareheapmemoryandglobalvariables.Eachthreadhasitsseparatesetofregistervaluesandaseparatestack.4.5Canamultithreadedsolutionusingmultipleuser-levelthreadsachievebetterperformanceonamultiprocessorsystemthanonasingle-processorsystem?Answer:Amultithreadedsystemcomprisingofmultipleuser-levelthreadscannotmakeuseofthedifferentprocessorsinamultiprocessorsystemsimultaneously.Theoperatingsystemseesonlyasingleprocessandwillnotschedulethedifferentthreadsoftheprocessonseparateprocessors.Consequently,thereisnoperformancebenefitassociatedwithexecutingmultipleuser-levelthreadsonamultiprocessorsystem.4.6AsdescribedinSection4.5.2,Linuxdoesnotdistinguishbetweenprocessesandthreads.Instead,Linuxtreatsbothinthesameway,allowingatasktobemoreakintoaprocessorathreaddependingonthesetofflagspassedtotheclone()systemcall.However,manyoperatingsystems—suchasWindowsXPandSolaris—treatprocessesandthreadsdifferently.Typically,suchsystemsuseanotationwhereinthedatastructureforaprocesscontainspointerstotheseparatethreadsbelongingtotheprocess.Contrastthesetwoapproachesformodelingprocessesandthreadswithinthekernel.Answer:Ononehand,insystemswhereprocessesandthreadsareconsideredassimilarentities,someoftheoperatingsystemcodecouldbesimplified.Ascheduler,forinstance,canconsiderthedifferentprocessesandthreadsinequalfootingwithoutrequiringspecialcodetoexaminethethreadsassociatedwithaprocessduringeveryschedulingstep.Ontheotherhand,thisuniformitycouldmakeithardertoimposeprocess-wideresourceconstraintsinadirectmanner.Instead,someextracomplexityisrequiredtoidentifywhichthreadscorrespondtowhichprocessandperformtherelevantaccountingtasks.4.7TheprogramshowninFigure4.11usesthePthreadsAPI.WhatwouldbeoutputfromtheprogramatLINECandLINEP?Answer:OutputatLINECis5.OutputatLINEPis0.4.8Consideramultiprocessorsystemandamultithreadedprogramwrittenusingthemany-to-manythreadingmodel.Letthenumberofuser-levelthreadsintheprogrambemorethanthenumberofprocessorsinthesystem.Discusstheperformanceimplicationsofthefollowingscenarios.a.Thenumberofkernelthreadsallocatedtotheprogramislessthanthenumberofprocessors.b.Thenumberofkernelthreadsallocatedtotheprogramisequaltothenumberofprocessors.c.Thenumberofkernelthreadsallocatedtotheprogramisgreaterthanthenumberofprocessorsbutlessthanthenumberofuserlevelthreads.Answer:Whenthenumberofkernelthreadsislessthanthenumberofprocessors,thensomeoftheprocessorswouldremainidlesincetheschedulermapsonlykernelthreadstoprocessorsandnotuser-levelthreadstoprocessors.Whenthenumberofkernelthreadsisexactlyequaltothenumberofprocessors,thenitispossiblethatalloftheprocessorsmightbeutilizedsimultaneously.However,whenakernelthreadblocksinsidethekernel(duetoapagefaultorwhileinvokingsystemcalls),thecorrespondingprocessorwouldremainidle.Whentherearemorekernelthreadsthanprocessors,ablockedkernelthreadcouldbeswappedoutinfavorofanotherkernelthreadthatisreadytoexecute,therebyincreasingtheutilizationofthemultiprocessorsystem.4.9WriteamultithreadedJava,Pthreads,orWin32programthatoutputsprimenumbers.Thisprogramshouldworkasfollows:Theuserwillruntheprogramandwillenteranumberonthecommandline.Theprogramwillthencreateaseparatethreadthatoutputsalltheprimenumberslessthanorequaltothenumberenteredbytheuser.Answer:PleaserefertothesupportingWebsiteforsourcecodesolution.4.10Modifythesocket-baseddateserver(Figure3.19)inChapter3sothattheserverserviceseachclientrequestinaseparatethread.Answer:PleaserefertothesupportingWebsiteforsourcecodesolution.4.11The