您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > operating system《操作系统》ch04-threads-31
Chapter4:Threads4.2ChapterObjectivesTointroducethenotionofathread-afundamentalunitofCPUutilizationthatformsthebasisofmultithreadedcomputersystems.TodiscusstheAPlsforPhtreads,Win32,andJavathreadlibraries.4.3ContentOverviewOverviewMultithreadingModelsThreadingIssuesPthreadsWindowsXPThreadsLinuxThreadsJavaThreads4.44.1OverviewAthreadisabasicunitofCPUutilization;ItcomprisesathreadID,aprogramcounter,aregisterset,andastack.Itshareswithotherthreadsbelongingtothesameprocessitscodesection,datasection,andotheroperating-systemresources,suchasopenfilesandsignals.4.5SingleandMultithreadedProcesses4.6BenefitsResponsivenessResourceSharingEconomyUtilizationofMPArchitectures4.7UserThreads&KernelThreadsUserthreadsaresupportedabovethekernelandaremanagedwithoutkernelsupport,Threadmanagementdonebyuser-levelthreadslibrarykernelthreadsaresupportedandmanageddirectlybytheoperatingsystem.,SupportedbytheKernel4.84.2MultithreadingModelsUltimately,theremustexistarelationshipbetweenuserthreadsandkernelthreads.Many-to-OneOne-to-OneMany-to-Many4.9Many-to-OneManyuser-levelthreadsmappedtosinglekernelthreadExamples:SolarisGreenThreadsGNUPortableThreadsThreadmanagementisdonebythethreadlibraryinuserspace,soitisefficient;buttheentireprocesswillblockifathreadmakesablockingsystemcall.becauseonlyonethreadcanaccessthekernelatatime,multiplethreadsareunabletoruninparallelonmultiprocessors.4.10Many-to-OneModel4.11One-to-OneEachuser-levelthreadmapstoakernelthreadItprovidesmoreconcurrencythanthemany-to-onemodelbyallowinganotherthreadtorunwhenathreadmakesablockingsystemcall;italsoallowsmultiplethreadstoruninparallelonmultiprocessors.Theonlydrawbackisthatcreatingauserthreadrequirescreatingthecorrespondingkernelthread,sorestrictiontothenumberofthreadssupportedbythesystemisneccesaryExamplesWindowsNT/XP/2000LinuxSolaris9andlater4.12One-to-oneModel4.13Many-to-ManyModelAllowsmanyuserlevelthreadstobemappedtomanykernelthreadsAllowstheoperatingsystemtocreateasufficientnumberofkernelthreadsSolarispriortoversion9WindowsNT/2000withtheThreadFiberpackage4.14Many-to-ManyModel4.15Two-levelModelSimilartoM:M,exceptthatitallowsauserthreadtobeboundtokernelthreadExamplesIRIXHP-UXTru64UNIXSolaris8andearlier4.16Two-levelModel4.17***4.3ThreadLibrariesprovidestheprogrammeranAPIforcreatingandmanagingthreads.twoprimarywaysofimplementingathreadlibrary.providealibraryentirelyinuserspacewithnokernelsupport.Allcodeanddatastructuresforthelibraryexistinuserspace.Thismeansthatinvokingafunctioninthelibraryresultsinalocalfunctioncallinuserspaceandnotasystemcall.implementakernel-levellibrarysupporteddirectlybytheoperatingsystem.Inthiscase,codeanddatastructuresforthelibraryexistinkernelspace.InvokingafunctionintheAPIforthelibrarytypicallyresultsinasystemcalltothekernel.Threemainthreadlibrariesareinuse(1)POSIXPthreads,(2)Win32,and(3)Java.4.18PthreadsAPOSIXstandard(IEEE1003.1c)APIforthreadcreationandsynchronizationAPIspecifiesbehaviorofthethreadlibrary,implementationisuptodevelopmentofthelibraryCommoninUNIXoperatingsystems(Solaris,Linux,MacOSX)4.19WindowsXPThreadsImplementstheone-to-onemappingEachthreadcontainsAthreadidRegistersetSeparateuserandkernelstacksPrivatedatastorageareaTheregisterset,stacks,andprivatestorageareaareknownasthecontextofthethreadsTheprimarydatastructuresofathreadinclude:ETHREAD(executivethreadblock)KTHREAD(kernelthreadblock)TEB(threadenvironmentblock)4.20JavaThreadsJavathreadsaremanagedbytheJVMJavathreadsmaybecreatedby:ExtendingThreadclassImplementingtheRunnableinterface4.21JavaThreadStates4.224.4ThreadingIssuesSemanticsoffork()andexec()systemcallsThreadcancellationSignalhandlingThreadpoolsThreadspecificdataScheduleractivations4.23Semanticsoffork()andexec()Doesfork()duplicateonlythecallingthreadorallthreads?4.24ThreadCancellationTerminatingathreadbeforeithasfinishedTwogeneralapproaches:AsynchronouscancellationterminatesthetargetthreadimmediatelyDeferredcancellationallowsthetargetthreadtoperiodicallycheckifitshouldbecancelled4.25SignalHandlingSignalsareusedinUNIXsystemstonotifyaprocessthataparticulareventhasoccurredAsignalhandlerisusedtoprocesssignals1.Signalisgeneratedbyparticularevent2.Signalisdeliveredtoaprocess3.SignalishandledOptions:DeliverthesignaltothethreadtowhichthesignalappliesDeliverthesignaltoeverythreadintheprocessDeliverthesignaltocertainthreadsintheprocessAssignaspecificthreatoreceiveallsignalsfortheprocess4.26ThreadPoolsCreateanumberofthreadsinapoolwheretheyawaitworkAdvantages:UsuallyslightlyfastertoservicearequestwithanexistingthreadthancreateanewthreadAllowsthenumberofthreadsintheapplication(s)tobeboundtothesizeofthepool4.27ThreadSpecificDataAllowseachthreadtohaveitsowncopyofdataUsefulwhenyoudonothavecontroloverthethreadcreationprocess(i.e.,whenusingathreadpool)4.28SchedulerActivationsBothM:MandTwo-levelmodelsrequirecommunicationtomaintaintheappropriatenumberofkernelthreadsallocatedtotheapplicationScheduleractivationsprovideupcalls-acommunicationmechanismfromt
本文标题:operating system《操作系统》ch04-threads-31
链接地址:https://www.777doc.com/doc-3373964 .html