operating system《操作系统》ch04-threads-31

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Chapter4:Threads4.2ChapterObjectivesTointroducethenotionofathread-afundamentalunitofCPUutilizationthatformsthebasisofmultithreadedcomputersystems.TodiscusstheAPlsforPhtreads,Win32,andJavathreadlibraries.4.3ContentOverviewOverviewMultithreadingModelsThreadingIssuesPthreadsWindowsXPThreadsLinuxThreadsJavaThreads4.44.1OverviewAthreadisabasicunitofCPUutilization;ItcomprisesathreadID,aprogramcounter,aregisterset,andastack.Itshareswithotherthreadsbelongingtothesameprocessitscodesection,datasection,andotheroperating-systemresources,suchasopenfilesandsignals.4.5SingleandMultithreadedProcesses4.6BenefitsResponsivenessResourceSharingEconomyUtilizationofMPArchitectures4.7UserThreads&KernelThreadsUserthreadsaresupportedabovethekernelandaremanagedwithoutkernelsupport,Threadmanagementdonebyuser-levelthreadslibrarykernelthreadsaresupportedandmanageddirectlybytheoperatingsystem.,SupportedbytheKernel4.84.2MultithreadingModelsUltimately,theremustexistarelationshipbetweenuserthreadsandkernelthreads.Many-to-OneOne-to-OneMany-to-Many4.9Many-to-OneManyuser-levelthreadsmappedtosinglekernelthreadExamples:SolarisGreenThreadsGNUPortableThreadsThreadmanagementisdonebythethreadlibraryinuserspace,soitisefficient;buttheentireprocesswillblockifathreadmakesablockingsystemcall.becauseonlyonethreadcanaccessthekernelatatime,multiplethreadsareunabletoruninparallelonmultiprocessors.4.10Many-to-OneModel4.11One-to-OneEachuser-levelthreadmapstoakernelthreadItprovidesmoreconcurrencythanthemany-to-onemodelbyallowinganotherthreadtorunwhenathreadmakesablockingsystemcall;italsoallowsmultiplethreadstoruninparallelonmultiprocessors.Theonlydrawbackisthatcreatingauserthreadrequirescreatingthecorrespondingkernelthread,sorestrictiontothenumberofthreadssupportedbythesystemisneccesaryExamplesWindowsNT/XP/2000LinuxSolaris9andlater4.12One-to-oneModel4.13Many-to-ManyModelAllowsmanyuserlevelthreadstobemappedtomanykernelthreadsAllowstheoperatingsystemtocreateasufficientnumberofkernelthreadsSolarispriortoversion9WindowsNT/2000withtheThreadFiberpackage4.14Many-to-ManyModel4.15Two-levelModelSimilartoM:M,exceptthatitallowsauserthreadtobeboundtokernelthreadExamplesIRIXHP-UXTru64UNIXSolaris8andearlier4.16Two-levelModel4.17***4.3ThreadLibrariesprovidestheprogrammeranAPIforcreatingandmanagingthreads.twoprimarywaysofimplementingathreadlibrary.providealibraryentirelyinuserspacewithnokernelsupport.Allcodeanddatastructuresforthelibraryexistinuserspace.Thismeansthatinvokingafunctioninthelibraryresultsinalocalfunctioncallinuserspaceandnotasystemcall.implementakernel-levellibrarysupporteddirectlybytheoperatingsystem.Inthiscase,codeanddatastructuresforthelibraryexistinkernelspace.InvokingafunctionintheAPIforthelibrarytypicallyresultsinasystemcalltothekernel.Threemainthreadlibrariesareinuse(1)POSIXPthreads,(2)Win32,and(3)Java.4.18PthreadsAPOSIXstandard(IEEE1003.1c)APIforthreadcreationandsynchronizationAPIspecifiesbehaviorofthethreadlibrary,implementationisuptodevelopmentofthelibraryCommoninUNIXoperatingsystems(Solaris,Linux,MacOSX)4.19WindowsXPThreadsImplementstheone-to-onemappingEachthreadcontainsAthreadidRegistersetSeparateuserandkernelstacksPrivatedatastorageareaTheregisterset,stacks,andprivatestorageareaareknownasthecontextofthethreadsTheprimarydatastructuresofathreadinclude:ETHREAD(executivethreadblock)KTHREAD(kernelthreadblock)TEB(threadenvironmentblock)4.20JavaThreadsJavathreadsaremanagedbytheJVMJavathreadsmaybecreatedby:ExtendingThreadclassImplementingtheRunnableinterface4.21JavaThreadStates4.224.4ThreadingIssuesSemanticsoffork()andexec()systemcallsThreadcancellationSignalhandlingThreadpoolsThreadspecificdataScheduleractivations4.23Semanticsoffork()andexec()Doesfork()duplicateonlythecallingthreadorallthreads?4.24ThreadCancellationTerminatingathreadbeforeithasfinishedTwogeneralapproaches:AsynchronouscancellationterminatesthetargetthreadimmediatelyDeferredcancellationallowsthetargetthreadtoperiodicallycheckifitshouldbecancelled4.25SignalHandlingSignalsareusedinUNIXsystemstonotifyaprocessthataparticulareventhasoccurredAsignalhandlerisusedtoprocesssignals1.Signalisgeneratedbyparticularevent2.Signalisdeliveredtoaprocess3.SignalishandledOptions:DeliverthesignaltothethreadtowhichthesignalappliesDeliverthesignaltoeverythreadintheprocessDeliverthesignaltocertainthreadsintheprocessAssignaspecificthreatoreceiveallsignalsfortheprocess4.26ThreadPoolsCreateanumberofthreadsinapoolwheretheyawaitworkAdvantages:UsuallyslightlyfastertoservicearequestwithanexistingthreadthancreateanewthreadAllowsthenumberofthreadsintheapplication(s)tobeboundtothesizeofthepool4.27ThreadSpecificDataAllowseachthreadtohaveitsowncopyofdataUsefulwhenyoudonothavecontroloverthethreadcreationprocess(i.e.,whenusingathreadpool)4.28SchedulerActivationsBothM:MandTwo-levelmodelsrequirecommunicationtomaintaintheappropriatenumberofkernelthreadsallocatedtotheapplicationScheduleractivationsprovideupcalls-acommunicationmechanismfromt

1 / 29
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功