现代操作系统 Chapter 2 Part2.ppt
《现代操作系统 Chapter 2 Part2.ppt》由会员分享,可在线阅读,更多相关《现代操作系统 Chapter 2 Part2.ppt(60页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter 2 Processes and threadsContents2.1 Processes2.2 Interprocess communication(IPC)2.3 Classical IPC problems2.4 Threads2.5 Scheduling22.4 Threads2.4.1 Threads usage2.4.2 The classical thread model2.4.3 Implementing threads in user space2.4.4 Implementing threads in the kernel32.4.1 Threads usag
2、eWhy would anyone want to have a kind of process within a process?It turns out there are several reasons for having these miniprocesses,called threads(线程线程).In many applications,multiple activities are going on at once.By decomposing such an application into multiple sequential threads that run in q
3、uasi-parallel(准并行准并行),the programming model becomes simpler.Threads are lighter weight than processes,they are easier to create and destroy than processes.Performance.When there is substantial computing and also substantial I/O,having threads allows these activities to overlap,thus speeding up the a
4、pplication.4An example of multithreadA word processor with 3 threads52.4.2 The classical thread modelThe process model is based on two independent concepts:resource grouping and execution.Sometimes it is useful to separate them;this is where threads come in.A process has an address space containing
5、program text and data,as well as other resources.A thread has a program counter,registers,a stack.Process are used to group resources together;threads are the entities scheduled for execution on the CPU.6线程的引入线程的引入v进程的两个基本属性:进程的两个基本属性:进程是一个可拥有资源的独立单位;进程是一个可拥有资源的独立单位;进程同时又是一个可以独立调度和分配的基本进程同时又是一个可以独立调
6、度和分配的基本单位。单位。v由于进程是一个资源拥有者,因而在进程的由于进程是一个资源拥有者,因而在进程的创建、撤消和切换中,系统必须为之付出较创建、撤消和切换中,系统必须为之付出较大的时空开销。也正因为如此,在系统中所大的时空开销。也正因为如此,在系统中所设置的进程数目不宜过多,进程切换的频率设置的进程数目不宜过多,进程切换的频率也不宜太高,但这也就限制了并发程度的进也不宜太高,但这也就限制了并发程度的进一步提高。一步提高。7线程的引入线程的引入v如何能使多个程序更好地并发执行,同时又尽量如何能使多个程序更好地并发执行,同时又尽量减少系统的开销,已成为近年来设计操作系统时减少系统的开销,已成为
7、近年来设计操作系统时所追求的重要目标。所追求的重要目标。v能否能否将进程的两个基本属性分开将进程的两个基本属性分开,由操作系统分,由操作系统分别进行处理。即把处理机调度和其他资源的分配别进行处理。即把处理机调度和其他资源的分配针对不同的活动实体进行,以使之轻装运行;而针对不同的活动实体进行,以使之轻装运行;而对拥有资源的基本单位,又不频繁地对之进行切对拥有资源的基本单位,又不频繁地对之进行切换。换。v在这种思想的指导下,产生了在这种思想的指导下,产生了线程线程的概念。的概念。v线程是进程中的一个实体,是被系统独立调度和线程是进程中的一个实体,是被系统独立调度和分配的基本单位。分配的基本单位。8
8、进程进程线程线程1线程线程2线程线程3进程进程1 分配处理机分配处理机资源分配资源分配线程示意图线程示意图9线程与进程的比较线程与进程的比较v下面将从四个方面比较线程和进程:下面将从四个方面比较线程和进程:调度调度并发性并发性拥有资源拥有资源系统开销系统开销10(1)调度)调度v在引入线程的操作系统中:在引入线程的操作系统中:线程是调度和分配的基本单位线程是调度和分配的基本单位进程是资源拥有的基本单位进程是资源拥有的基本单位v把传统进程的两个属性分开,线程便能轻装把传统进程的两个属性分开,线程便能轻装运行,从而可显著地提高系统的并发程度。运行,从而可显著地提高系统的并发程度。v在同一进程中,线
9、程的切换不会引起进程的在同一进程中,线程的切换不会引起进程的切换;在由一个进程中的线程切换到另一个切换;在由一个进程中的线程切换到另一个进程中的线程时,才会引起进程的切换。进程中的线程时,才会引起进程的切换。11(2)并发性)并发性v在引入线程的操作系统中,不仅进程之间可在引入线程的操作系统中,不仅进程之间可以并发执行,而且在以并发执行,而且在一个进程中的多个线程一个进程中的多个线程之间亦可并发执行之间亦可并发执行,因而使操作系统具有更,因而使操作系统具有更好的并发性,从而能更有效地使用系统资源好的并发性,从而能更有效地使用系统资源和提高系统吞吐量。和提高系统吞吐量。12(3)拥有资源)拥有资
10、源v不论是传统的操作系统,还是设有线程的操不论是传统的操作系统,还是设有线程的操作系统,作系统,进程都是拥有资源的一个独立单位进程都是拥有资源的一个独立单位,它可以拥有自己的资源。它可以拥有自己的资源。v一般地说,一般地说,线程自己不拥有系统资源线程自己不拥有系统资源(只有(只有一些必不可少的资源),但它可以访问其隶一些必不可少的资源),但它可以访问其隶属进程的资源。属进程的资源。13(4)系统开销)系统开销v由于在创建或撤消进程时,系统都要为之分由于在创建或撤消进程时,系统都要为之分配或回收资源,因此,操作系统所付出的开配或回收资源,因此,操作系统所付出的开销将显著地大于在创建或撤消线程时的
11、开销。销将显著地大于在创建或撤消线程时的开销。v进程切换的开销也远大于线程切换的开销。进程切换的开销也远大于线程切换的开销。14(a)Three processes each with one thread.Each thread operates in a different address space.The Classical Thread Model(b)One process with three threads.All three threads share the same address space.15The Classical Thread Modelsome items
12、shared by all threads in a processsome items private to each thread16Thread statevLike a traditional process,a thread can be in any one of several states:running,blocked,ready,or terminated.vThe transitions between thread states are the same as the transitions between process states.17Thread creatio
13、n and terminationvWhen multithreading is present,processes normally start with a single thread present.This thread has the ability to create new threads by calling a library procedure,for example,thread_create.vWhen a thread has finished its work,it can exit by calling a library procedure,say,thread
14、_exit.18vAnother common thread call is thread_yield,which allows a thread to voluntarily give up the CPU to let another thread run.vWhy do we need this call?192.4.3 Implementing threads in user spacevThere are two main ways to implement a threads package:In user space:to put the threads package enti
15、rely in user space.The kernel knows nothing about them.In the kernel20A user-level threads package.A threads package managed by the kernel.21Advantages of user-level threads packagevA user-level threads package can be implemented on an OS that does not support threads.vThey allow each process to hav
16、e its own customized scheduling algorithm.22The problems of user-level threads packagevIf a thread is blocked,the kernel,not even knowing about the existence of threads,naturally blocks the entire process until the disk I/O is complete,even though other threads might be runnable.vIf a thread starts
17、running,no other thread in that process will ever run unless the first thread voluntarily gives up the CPU.232.4.4 Implementing threads in the kernelvThe kernel has a thread table that keeps track of all the threads in the system.When a thread wants to create a new thread or destroy an existing thre
18、ad,it makes a kernel call.vAll calls that might block a thread are implemented as system calls,at considerably greater cost than a call to a run-time system procedure.vWhen a thread blocks,the kernel can run either another thread from the same process or a thread from a different process.242.5 Sched
19、uling When a computer is multiprogrammed,it frequently has multiple processes or threads competing for the CPU at the same time.If only one CPU is available,a choice has to be made which process to run next.The part of the OS that makes the choice is called the scheduler,and the algorithm it uses is
20、 called the scheduling algorithm.252.5.1 Introduction to scheduling1.Process behaviorNearly all processes alternate bursts of computing with I/O requests.Figure 2-38261.Process behaviorvIn Fig 2-38(a),these processes spend most of their time computing,which are called compute-bound(计算密集型计算密集型).vIn F
21、ig 2-38(b),these processes spend most of their time waiting for I/O,which are called I/O-bound(I/O密集型密集型).vAs CPU get faster,processes tend to get more I/O-bound.272.When to scheduleWhen a new process is created,a decision needs to be made whether to run the parent process or the child process.A sch
22、eduling decision must be made when a process exits.When a process blocks on I/O,on a semaphore,or for some other reason,another process has to be selected to run.When an I/O interrupt occurs,a scheduling decision may be made.28vA nonpreemptive scheduling algorithm(非抢占式调度算法非抢占式调度算法)picks a process to
23、 run and then just lets it run until it blocks or until it voluntarily releases the CPU.vA preemptive scheduling algorithm picks a process and lets it run for a maximum of some fixed time.293.Categories of scheduling algorithmsvIn different environments different scheduling algorithms are needed.vTh
24、ree environments:BatchNonpreemptive algorithms,or preemptive algorithms with long time periods for each process,are often acceptable.InteractiveIn an environment with interactive users,preemption is essential to keep one process from hogging the CPU and denying service to the others.Real timeIn syst
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 现代操作系统 Chapter Part2 现代 操作系统
限制150内