您好,欢迎访问三七文档
Chapter5ThreadOverviewMultithreadingModelsThreadingIssuesPthreadsSolaris2ThreadsWindows2000ThreadsLinuxThreadsJavaThreadsWhyweneedthreads?Thinking:whythewebbrowsercandownloadpage,displayandinteractwithuserconcurrently?solution:createchildprocessestodealwithdifferenttasks?Howaboutperformance?Aprocesshasitsownresourceandexecutesindependently.Thereexistsresourcereallocatingandstatusswitching---costgreatlyWhyweneedthreads?(cont.)Whyweneedthreads?(cont.)Multi-ThreadingWhylimitourselvestoasinglethread?ThinkofawebserverthatmustservicealargestreamofrequestsIfonlyhaveonethread,canonlyprocessonerequestatatimeWhattodowhenreadingafilefromdisk?ContextswtichMulti-threadingmodelEachprocesscanhavemultiplethreads(多线程)EachthreadhasaprivatestackRegistersarealsoprivateAllthreadsofaprocesssharethecodeandheapObjectstobesharedacrossmultiplethreadsshouldbeallocatedontheheapSingleandMultithreadedProcessesBenefitsResponsiveness响应时间ResourceSharing资源共享Economy经济UtilizationofMPArchitectures利用多处理机结构ThreadThreadisanattributeofaprocessandistheindependentlyschedulingunit线程是进程的一个属性,是被系统独立调度的基本单位,是CPU的抽象Aprocesscancreatemanythreads,whichsharetheprocess’sresourceMultiplethreadsrunconcurrently(并发执行),switchamongthemissimple线程的特征:动态性、并发性、(运行)独立性、异步性Thread(cont.)Eg.InWindows,eachprocessuseitsown4GBmemory,andmultiplethreadsoftheprocesssharethesame4GBSuppose:process1needstoaccess1000inmemory,process2needstoaccess1000inmemory,theywillusedifferentaddressinthememory。Buttwothreads,accessing1000,ofaprocess,usethesameaddressThread(cont.)线程与进程的比较(1)调度同一进程的多线程间调度时,不引起进程的切换不同进程的线程间调度,需要进程切换(2)并发性一个进程的多个线程之间可并发执行(3)资源的拥有线程不拥有系统资源,不拥有代码段、数据段。Multi-Threading(cont)ImplementationEachthreadisdescribedbyathread-controlblock(TCB线程控制块)ATCBtypicallycontainsThreadIDExecutionState:CPUregisters,programcounter,pointertostackSpaceforsavingregistersSchedulinginfo:State,priority,CPUtimeVariousPointers(forimplementingschedulingqueues)Pointertoenclosingprocess?(PCB)Etc(addstuffasyoufindaneed)ProcessAddressSpaceRevisitedOSCodeGlobalsStackHeapOSCodeGlobalsStackHeapStack(a)Single-threadedaddressspace(b)Multi-threadedaddressspaceMulti-Threading(cont.)ObservationAlthoughthemodelisthateachthreadhasaprivatestack,threadsactuallysharetheprocessaddressspaceThere’snomemoryprotection!Threadscouldpotentiallywriteintoeachother’sstackUserThreadsThreadmanagementdonebyuser-levelthreadslibrary使用用户级的线程库完成线程的管理Examples举例-POSIXPthreads-MachC-threads-SolaristhreadsKernelThreadsSupportedbytheKernel直接由内核支持Examples举例-Windows95/98/NT/2000-Solaris-Tru64UNIX-BeOS-LinuxUservs.KernelThreadsThreadManagementDonebyUser-LevelThreadsLibrary对用户线程的支持通常处于内核之上,通过一个用户级线程库实现。线程库提供了对线程的创建、调度和管理的支持,这无需来自内核的支持。所有的线程创建和调度工作都在用户空间完成,而且整个过程不受内核的干涉。ThreadManagementSupportedbytheKernel内核线程由操作系统直接支持:内核在内核空间内实现了线程的创建、调度和管理。Uservs.KernelThreadsAdvantagesofuserthreadsPerformance(性能):low-costthreadoperations(donotrequirecrossingprotectiondomains)Flexibility(灵活性):schedulingcanbeapplicationspecificPortability(可移植性):userthreadlibraryeasytoportDisadvantagesofuserthreadsIfauser-levelthreadisblockedinthekernel,theentireprocess(allthreadsofthatprocess)areblockedCannottakeadvantageofmultiprocessing(thekernelassignsoneprocesstoonlyoneprocessor)Uservs.KernelThreadsprocessprocessoruserthreadsthreadschedulingprocessschedulingkernelthreadsthreadschedulingkerneluserprocessorthreadsthreadsprocessschedulingUservs.KernelThreadsNoreasonwhyweshouldn’thavebothMostsystemsnowsupportkernelthreadsUser-levelthreadsareavailableaslinkablelibrarieskernelthreadsprocessoruserthreadsthreadschedulingthreadschedulingkerneluserprocessschedulingMultithreadingModelsMany-to-One多对一One-to-One一对一Many-to-Many多对多Many-to-OneManyuser-levelthreadsmappedtosinglekernelthread.Usedonsystemsthatdonotsupportkernelthreads.Many-to-OneModelOne-to-OneEachuser-levelthreadmapstokernelthread.每一个用户级的线程都映射到一个内核的线程上。Examples-Windows95/98/NT/2000-OS/2One-to-oneModelMany-to-ManyModelAllowsmanyuserlevelthreadstobemappedtomanykernelthreads.Allowstheoperatingsystemtocreateasufficientnumberofkernelthreads.Solaris2WindowsNT/2000withtheThreadFiberpackageMany-to-ManyModelThreadingIssuesSemanticsoffork()andexec()systemcalls.fork()和exec()系统调用的语义Threadcancellation.SignalhandlingThreadpools线程池ThreadspecificdataPthreadsaPOSIXstandard(IEEE1003.1c)APIforthreadcreationandsynchronization.IEEE为UNIX标准化定义的一个标准。线程创建和同步的应用编程的界面。APIspecifiesbehaviorofthethreadlibrary,implementationisuptodevelopmentofthelibrary.该标准中的API定义了线程库的行为,实现取决于线程库的开发。CommoninUNIXoperatingsystems.UNIX操作系统中大都实现了该标准。Solaris2ThreadsSolarisProcessWindows2000ThreadsImplementstheone-to-onemapping.Eachthreadcontains-athreadid-registerset-separateuserandkernelstacks-privatedatastorageareaLinuxThreadsLinuxreferstothemastasksratherthanthreads.Linux中通常不叫线程,而是叫任务。Threadcreationisdonethroughclone()systemcall.线程的创建通过clone()系统调用实现。Clone()allowsachildtasktosharetheaddressspaceoftheparenttask(process)Clone()容许子任务共享父进程的地址空间。JavaThreadsJavathreadsmaybecreatedby:JAV
本文标题:操作系统第5章
链接地址:https://www.777doc.com/doc-4356483 .html