收藏 分销(赏)

操作系统概念ch4-quanPPT课件.ppt

上传人:丰**** 文档编号:10458190 上传时间:2025-05-28 格式:PPT 页数:36 大小:474.50KB 下载积分:12 金币
下载 相关 举报
操作系统概念ch4-quanPPT课件.ppt_第1页
第1页 / 共36页
操作系统概念ch4-quanPPT课件.ppt_第2页
第2页 / 共36页


点击查看更多>>
资源描述
Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,4.,*,Click to edit Master title style,Operating System Concepts 7,th,edition,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,Click to edit Master title style,.,Chapter 4:Threads,.,Chapter 4:Threads,Overview,概述,Multithreading Models,多线程模型,Threading Issues,多线程问题,Pthreads,Windows XP Threads,Linux Threads,Java Threads,Threads,操作系统中引入进程的目的,是为了描述和实现多个程序的并发执行,以改善资源利用率以及提高系统吞吐量。,那为什么还需要引入线程呢,?,线程的引入,进程的两个基本属性:,资源的拥有者,给每个进程分配一虚拟地址空间,保存进程影像,控制一些资源(文件、,I/O,设备),有状态、优先级、调度,调度单位,进程是一个执行轨迹,这两个属性构成了进程并发执行的基础,线程的引入,系统必须完成的操作:,创建进程、撤销进程、进程切换,当进程被创建时,系统要为它分配,PCB,表及其它必要的资源,如内存等;当进程被撤消时,系统要收回这些资源及,PCB,表等,因此系统必须付出一定的开销。,当进程运行时,进程的切换现象更会大量存在,由于要保留当前执行进程的,CPU,现场和为选中执行的进程重布现场,更需较大的开销。,缺点:时间空间开销大,限制并发程度的提高,线程的引入,线程的引入目的:,减少进程切换和创建开销,提高执行效率和节省资源,实现:将进程的资源申请和调度属性分开。即进程作为资源的申请和拥有者,但不作为调度的基本单位,这样,就产生了线程的概念。,线程:是进程中的一个实体,是独立调度和分派的基本单位。,Single and Multithreaded Processes,单线程进程以及多线程进程,A,thread,is a basic unit of CPU utilization;it consists of:,线程是,CPU,运行的一个基本单元,包括,program counter,程序计数器,register set,寄存器集,stack space,栈空间,A thread shares with its peer threads its:,一个线程与它的对等线程共享:,code section,代码段,data section,数据段,operating-system resources,操作系统资源,collectively know as a,task,.,总体作为一个任务,A traditional or,heavyweight,process is equal to a task with one thread,传统的或重型进程等价于只有一个线程的任务,Application Scenarios of Multithreading,多线程应用场景,1.,网页浏览器,可能分为多个线程,分别完成不同的功能,一个线程接收文本,另一个线程接收数据(图片、音频等),2.Word Processor,文本编辑器,一个线程用于显示图形,一个线程读入用户键盘输入,第三个线程在后台进行拼写和语法检查,Application Scenarios of Multithreading,多线程应用场景,3.,网页服务器是可能应用多线程机制最有效的情况,要应对很多客户请求,执行多个相似任务:每个客户请求的处理过程是非常相似的,处理每一个客户请求的代码都是一致的,因此线程的代码可以共享,Benefits,好处,Responsiveness,响应度高,Resource Sharing,资源共享,内存和资源共享,代码共享(运行一个进程内多个线程执行一个相同的代码),Economy,经济,进程创建比较耗费资源,线程允许资源共享,在多个线程所需要的数据相似程度比较高的时候,可以充分利用这一优势,Utilization of MP Architectures,多处理器体系结构的利用,User Threads,用户线程,Thread management done by user-level threads library,利用用户级的线程库进行管理,线程库提供对线程的创建、调度和管理,无需用户干预,内核不知道用户线程的存在,缺点:如果内核是单线程的,那么任何一个用户级线程执行阻塞原语的时候就会引起整个进程阻塞,即使当时还有该进程内还有其他的线程可以被调度执行,Three primary thread libraries:,POSIX Pthreads,Win32 threads,Java threads,Kernel Threads,内核线程,Supported by the Kernel,内核支持,操作系统内核负责其创建、调度和管理,内核直接管理线程,因此,当一个线程调用阻塞原语,进入阻塞态时,内核能调度当前进程内的其他线程执行,在多处理器环境下,内核能够在多个处理器上调度不同的线程执行,Examples,Windows XP/2000,Solaris,Linux,Tru64 UNIX,Mac OS X,Multithreading Models,多线程模型,Many-to-One,One-to-One,Many-to-Many,Many-to-One,Many user-level threads mapped to single kernel thread,多个用户级线程映射到一个内核线程,Examples:,Solaris Green Threads,GNU Portable Threads,缺点,:一旦线程阻塞,进程就阻塞,Many-to-One Model,One-to-One,Each user-level thread maps to kernel thread,每个用户级线程映射到一个内核线程,Examples,Windows NT/XP/2000,Linux,Solaris 9 and later,缺点:内核负担大,切换频繁,One-to-one Model,One,-to-One Model,Many-to-Many Model,Allows many user level threads to be mapped to many kernel threads,允许多个用户级线程映射到多个内核线程上,Allows the operating system to create a sufficient number of kernel threads,允许操作系统创建足够多的内核线程,Solaris prior to version 9,Windows NT/2000 with the,ThreadFiber,package,Many-to-Many Model,Two-level Model,二级模型,Similar to M:M,except that it allows a user thread to be,bound,to kernel thread,跟多对多类似,也允许一个用户线程绑定到一个内核线程上,Examples,IRIX,HP-UX,Tru64 UNIX,Solaris 8 and earlier,Two-level Model,Threading Issues,多线程问题,Semantics of,fork(),and,exec(),system calls,Thread cancellation,Signal handling,Thread pools,Thread specific data,Scheduler activations,Semantics of fork()and exec(),Does,fork(),duplicate only the calling thread or all threads?,如果程序中的一个线程调用,fork(),,新进程会复制所有线程,还是单个线程?,Thread Cancellation,线程取消,Terminating a thread before it has finished,在线程结束前终止线程的任务,Two general approaches:,两种方法,Asynchronous cancellation,terminates the target thread immediately,异步取消:立即终止目标线程,Deferred cancellation,allows the target thread to periodically check if it should be cancelled,延迟取消:允许目标线程不断地检查它是否应终止,允许目标线程有机会以有序的方式终止自己,Signal Handling,信号处理,Signals are used in UNIX systems to notify a process that a particular event has occurred,信号在,UNIX,系统中用来通知进程某个特定的事件发生了,A,signal handler,is used to process signals,Signal is generated by particular event,信号是由特定事件的发生所产生的,Signal is delivered to a process,产生的信号要发送到进程,Signal is handled,信号必须加以处理,Options:,Deliver the signal to the thread to which the signal applies,发送信号到信号所应用的线程,Deliver the signal to every thread in the process,发送到进程的每一个线程,Deliver the signal to certain threads in the process,发送到进程内的某些固定线程,Assign a specific thread to receive all signals for the process,规定一个特定线程以接收进程的所有信号,Thread Pools,线程池,Create a number of threads in a pool where they await work,创建一定数量的线程,放到池中等待工作,Advantages:,Usually slightly faster to service a request with an existing thread than create a new thread,通常用现有线程处理请求比等待创建新的线程要快,Allows the number of threads in the application(s)to be bound to the size of the pool,限制了在任何时候可用线程的数量,Thread Specific Data,线程特定数据,Allows each thread to have its own copy of data,允许每一个线程有自己的数据副本,Useful when you do not have control over the thread creation process(i.e.,when using a thread pool),Scheduler Activations,调度程序激活,Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application,多对多和二级模型都需要通过通信维持一个合适的内核线程的数量以保证最好的性能,Scheduler activations provide,upcalls,-a communication mechanism from the kernel to the thread library,This communication allows an application to maintain the correct number kernel threads,Pthreads,A POSIX standard(IEEE 1003.1c)API for thread creation and synchronization,API specifies behavior of the thread library,implementation is up to development of the library,Common in UNIX operating systems(Solaris,Linux,Mac OS X),Windows XP Threads,Implements the one-to-one mapping,Each thread contains,A thread id,Register set,Separate user and kernel stacks,Private data storage area,The register set,stacks,and private storage area are known as the,context,of the threads,The primary data structures of a thread include:,ETHREAD(executive thread block),KTHREAD(kernel thread block),TEB(thread environment block),Linux Threads,Linux refers to them as,tasks,rather than,threads,Thread creation is done through,clone(),system call,clone(),allows a child task to share the address space of the parent task(process),Java Threads,Java threads are managed by the JVM,Java threads may be created by:,Extending Thread class,Implementing the Runnable interface,Java Thread States,End of Chapter 4,.,
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服