用户和内核线程_第1页
用户和内核线程_第2页
用户和内核线程_第3页
用户和内核线程_第4页
用户和内核线程_第5页
已阅读5页,还剩30页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、5.1 Operating System ConceptsModule 5: Threads线程线程Overview 综述综述Benefits 益处益处User and Kernel Threads 用户和内核线程用户和内核线程Multithreading Models 多线程模型多线程模型Solaris 2 Threads Solaris 2线程线程Java Threads Java线程线程5.2 Operating System ConceptsResponsiveness 响应响应Resource Sharing 资源共享资源共享Economy经济性经济性Utilization of M

2、P Architectures MP体系结构的运用体系结构的运用Benefits益处益处5.3 Operating System ConceptsSingle and Multithreaded Processes单个和多线程进程单个和多线程进程5.4 Operating System ConceptsMultithreads exampleA www server5.5 Operating System ConceptsMultithreads exampleA www server5.6 Operating System ConceptsMulti-ThreadingWhy limit o

3、urselves to a single thread? Think of a web server that must service a large stream of requests If only have one thread, can only process one request at a time What to do when reading a file from disk?Multi-threading model Each process can have multiple threads Each thread has a private stackTRegist

4、ers are also private All threads of a process share the code and heapTObjects to be shared across multiple threads should be allocated on the heap5.7 Operating System ConceptsMulti-Threading (cont)Implementation Each thread is described by a thread-control block (TCB) A TCB typically containsTThread

5、 IDTSpace for saving registersTPointer to thread-specific data not on stackObservation Although the model is that each thread has a private stack, threads actually share the process address space Theres no memory protection! Threads could potentially write into each others stack5.8 Operating System

6、ConceptsProcess Address Space RevisitedOSCodeGlobalsStackHeapOSCodeGlobalsStackHeapStack(a) Single-threaded address space(b) Multi-threaded address space5.9 Operating System ConceptsUser Threads用户线程用户线程Thread Management Done by User-Level Threads Library由用户级线程库进行管理的线程由用户级线程库进行管理的线程Examples例子例子- POSI

7、X Pthreads- Mach C-threads- Solaris threads5.10 Operating System ConceptsKernel Threads内核线程内核线程Supported by the Kernel由内核支持由内核支持Examples例子例子- Windows 95/98/NT - Solaris- Digital UNIX5.11 Operating System ConceptsUser vs. Kernel ThreadsAdvantages of user threads Performance: low-cost thread operation

8、s (do not require crossing protection domains) Flexibility: scheduling can be application specific Portability: user thread library easy to portDisadvantages of user threads If a user-level thread is blocked in the kernel, the entire process (all threads of that process) are blocked Cannot take adva

9、ntage of multiprocessing (the kernel assigns one process to only one processor)5.12 Operating System ConceptsUser vs. Kernel Threadsprocessprocessoruserthreadsthreadschedulingprocessschedulingkernelthreadsthreadschedulingkerneluserprocessorthreadsthreadsprocessscheduling5.13 Operating System Concept

10、sUser vs. Kernel ThreadsNo reason why we shouldnt have bothMost systems now support kernel threadsUser-level threads are available as linkable librarieskernelthreadsprocessoruserthreadsthreadschedulingthreadschedulingkerneluserprocessscheduling5.14 Operating System ConceptsMultithreading Models多线程模型

11、多线程模型Many-to-One多对一多对一One-to-One一对一一对一Many-to-Many 多对多多对多5.15 Operating System ConceptsMany-to-One多对一多对一Many User-Level Threads Mapped to Single Kernel Thread.多个用户级线程映像进单个内核线程多个用户级线程映像进单个内核线程Used on Systems That Do Not Support Kernel Threads. 用于不支持内核线程的系统中用于不支持内核线程的系统中5.16 Operating System ConceptsM

12、any-to-one Model多对一模型多对一模型5.17 Operating System ConceptsOne-to-One一对一一对一Each User-Level Thread Maps to Kernel Thread.每个用户级线程映像进内核线程每个用户级线程映像进内核线程Examples- Windows 95/98/NT- OS/25.18 Operating System ConceptsOne-to-one Model一对一模型一对一模型5.19 Operating System ConceptsMany-to-many Model多对多模型多对多模型5.20 Oper

13、ating System ConceptsPthreadsa 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.5.21 Operating System ConceptsSolaris 2 Threads Solaris 2线程线程5.22

14、 Operating System ConceptssolarisUnix 的发展分支的发展分支4.XBSD是由加州大学贝克莱分校计算机系统研究组开发的。该研是由加州大学贝克莱分校计算机系统研究组开发的。该研究组也发布究组也发布BSD NET1和和BSD NET2版,它们包含了版,它们包含了4.XBSD系统系统公众可用源代码。公众可用源代码。SVRX是是AT& T的系统的系统V的简称。的简称。XPG3是是X/Open可移植性指南的第三次发行本的简称。可移植性指南的第三次发行本的简称。ANSI C是是C程序设程序设计语言的计语言的ANSI标准。标准。POSIX.1是是Unix类系统界面的

15、类系统界面的IEEE和和ISD标标准。准。 5.23 Operating System ConceptsSolaris Process Solaris 线程线程5.24 Operating System ConceptsLinux ThreadsLinux 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 (

16、process)5.25 Operating System ConceptsJava Threads Java线程线程Java Threads May be Created by: Java线程可如下创建:线程可如下创建: Extending Thread class 扩充线程类扩充线程类 Implementing the Runnable interface 实现可运行接口实现可运行接口5.26 Operating System ConceptsExtending the Thread Class线程类型的扩展线程类型的扩展class Worker1 extends Threadpublic

17、 void run() System.out.println(“I am a Worker Thread”);5.27 Operating System ConceptsCreating the Thread创建线程创建线程public class Firstpublic static void main(String args) Worker runner = new Worker1();runner.start();System.out.println(“I am the main thread”);5.28 Operating System ConceptsThe Runnable In

18、terface可运行接口可运行接口public interface Runnablepublic abstract void run();5.29 Operating System ConceptsImplementing the Runnable Interface可运行接口的实现可运行接口的实现class Worker2 implements Runnablepublic void run() System.out.println(“I am a Worker Thread”);5.30 Operating System ConceptsCreating the Thread创建线程创建线

19、程public class Secondpublic static void main(String args) Runnable runner = new Worker2();Thread thrd = new Thread(runner);thrd.start();System.out.println(“I am the main thread”);5.31 Operating System ConceptsJava Thread ManagementJava线程的管理线程的管理suspend() suspends execution of the currently running th

20、read.挂起挂起 - 暂停当前线程的运行暂停当前线程的运行sleep() puts the currently running thread to sleep for a specified amount of time. 睡眠睡眠 - 让当前线程入睡一段指定的时间让当前线程入睡一段指定的时间resume() resumes execution of a suspended thread. 恢复恢复 - 再执行被挂起的线程再执行被挂起的线程stop() stops execution of a thread. 停止停止 - 停止一个线程的执行停止一个线程的执行5.32 Operating System ConceptsJava Thread States Java线程状态线程状态 5.33 Operating System ConceptsProducer Consumer Problem生产着消费者问题生产着消费者问题public class Server public Server() MessageQueue mailBox = new MessageQueue(); Producer producerThread = new Producer(mailBox); Consumer con

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论