1 / 35

Chap 5 Threads 线程

Chap 5 Threads 线程. Contents 内容. Benefits 益处 Thread 线程综述 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2 Threads Solaris 2 线程 W indows2000 Threads Windows2000 线程 Java Threads Java 线程 Summary( 总结 ) H omework 作业. Benefits 益处. Responsiveness 响应

kaida
Download Presentation

Chap 5 Threads 线程

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chap 5 Threads线程 Applied Operating System Concepts

  2. Contents内容 • Benefits 益处 • Thread 线程综述 • User and Kernel Threads 用户和内核线程 • Multithreading Models 多线程模型 • Solaris 2 Threads Solaris 2线程 • Windows2000 Threads Windows2000线程 • Java ThreadsJava线程 • Summary(总结) • Homework作业 Applied Operating System Concepts

  3. Benefits 益处 • Responsiveness 响应 • Resource Sharing 资源共享 • Economy经济性 • Utilization of MP Architectures MP体系结构的运用 Applied Operating System Concepts

  4. Threads线程 • A thread (or lightweight process) 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 传统的或重型进程等价于只有一个线程的任务 Applied Operating System Concepts

  5. Threads线程(Cont.) • In a multiple threaded task, while one server thread is blocked and waiting, a second thread in the same task can run. 在一个多线程任务中,当一个服务器线程被阻塞后,在同一个任务中的第2个线程可运行 • Cooperation of multiple threads in same job confers higher hroughput and improved performance. 同一作业中多线程协同导致高吞吐量并改善性能 • Applications that require sharing a common buffer (i.e., producer-consumer) benefit from thread utilization. 应用程序需要共享一个公用的缓冲(即生产者-消费者),从线程运用中得利 Applied Operating System Concepts

  6. Threads线程(Cont.) • Threads provide a mechanism that allows sequential processes to make blocking system calls while also achieving parallelism. 线程提供了一种机制,允许序列进程阻塞系统调用同时还能实现并行 • Kernel-supported threads 内核支持线程(Mach and OS/2). • User-level threads; supported above the kernel, via a set of library calls at the user level (Project Andrew from CMU). 用户级线程;在内核之上,通过用户级的库调用 • Hybrid approach implements both user-level and kernel-supported threads (Solaris 2). 混合处理实现用户级和内核支持线程 Applied Operating System Concepts

  7. Thread and Process线程和进程 • Scheduling调度 线程是调度的基本单位,同一进程中的线程切换不会引起进程切换。 • Concurrence并发 线程可以提高系统的并发性。 • Resource资源 进程拥有资源,是资源分配的基本单位,而线程则不拥有资源,但它可以访问创建它的进程所拥有的资源。 • Context Switch上下文切换 线程的上下文切换的代价比进程小。 Applied Operating System Concepts

  8. Windows 2000 Process and Thread Windows 2000进程和线程 Applied Operating System Concepts

  9. Single and Multithreaded Processes单个和多线程进程 Applied Operating System Concepts

  10. Thread and Child Process线程和子进程 Process Thread Child Process Thread Applied Operating System Concepts

  11. User-Level Threads (ULT)用户线程 • Thread Management Done by User-Level Threads Library,such as thread creation, scheduling and management.由用户级线程库进行管理的线程,如线程创建、调度等。 • The kernel is unaware of user-level thread, all thread creation and scheduling are done in user space without the need for kernel intervention 。内核看不到ULT,线程的创建和调度在用户空间,不需要内核的干预。 • Examples例子 - POSIX Pthreads - Mach C-threads - Solaris threads Applied Operating System Concepts

  12. User-Level Threads用户线程(Cont.) Process Scheduling Process Process BLOCK Thread cause RUN Thread BLOCK Thread BLOCK single-threaded multi-threaded 单线程进程 多线程进程 运行线程的阻塞导致整个进程阻塞 运行进程的阻塞不会导致进程的阻塞 Applied Operating System Concepts

  13. Kernel-Level Threads内核线程 • Supported by the Kernel由内核支持 • KLT creation ,scheduling, and management are done by the kernel in kernel space.KLT创建、调度和管理由内核在核心空间运行。 • KLT are generally slower to create and manage than ULT. KLT创建和管理通常比ULT慢。 • Examples例子 - Windows 95/98/NT - Solaris - Digital UNIX Applied Operating System Concepts

  14. Multithreading Models多线程模型 • Many-to-One多对一 • One-to-One一对一 • Many-to-Many 多对多 Applied Operating System Concepts

  15. Many-to-One多对一 • Many User-Level Threads Mapped to Single Kernel-Level Thread.多个用户级线程映像进单个内核线程 • Multiple threads are unable to run in parallel on multiprocessor.多个线程不能并行运行在多个处理器上。 • Thread management is done in user space ,so it’s efficient, but the entire process will block if a thread makes a blocking system call. 线程管理在用户态执行,因此是高效的,但一个线程的阻塞系统调用会导致整个进程的阻塞。 • Used on Systems That Do Not Support Kernel Threads. 用于不支持内核线程的系统中 Applied Operating System Concepts

  16. Many-to-one Model多对一模型 Applied Operating System Concepts

  17. One-to-One一对一 • Each User-Level Thread Maps to Kernel-Level Thread.每个用户级线程映像进内核线程 • More concurrency than the many-to-one model 比多对一模型有更好的并发性 • Allow multiple threads to run in parallel on multiprocessor 允许多个线程并行运行在多个处理器上 • Creating a ULT requires creating the corresponding KLT 创建一个ULT需要创建一个KLT,效率较差 • Examples - Windows 95/98/NT - OS/2 Applied Operating System Concepts

  18. One-to-one Model一对一模型 Applied Operating System Concepts

  19. Many-to-many 多对多 • Many ULTs maps to a smaller or equal number of KLTs. • 多个ULT映射为相等或小于数目的KLT. • More concurrency 更好的并发性 • Examples • Solaris • Irix • Digital UNIX Applied Operating System Concepts

  20. Many-to-many Model多对多模型 Applied Operating System Concepts

  21. Solaris 2 Threads Solaris 2线程 Applied Operating System Concepts

  22. Solaris Process Solaris 线程 Applied Operating System Concepts

  23. Windows 2000 ThreadW2K线程 • 线程状态 • Ready就绪:可以被调度执行 • Standbly备用:下一次可以运行 • Running运行:执行线程 • Waiting等待:由于某个事件阻塞 • Transition转换:准备好运行但资源不可用时,由等待进入转换 • Terminated终止:运行结束。 Applied Operating System Concepts

  24. Windows 2000 ThreadW2K线程(cont.) Applied Operating System Concepts

  25. NT线程的有关API • CreateThread()函数在调用进程的地址空间上创建一个线程,以执行指定的函数;返回值为所创建线程的句柄。 • ExitThread()函数用于结束本线程。 • SuspendThread()函数用于挂起指定的线程。 • ResumeThread()函数递减指定线程的挂起计数,挂起计数为0时,线程恢复执行。 Applied Operating System Concepts

  26. Java Threads Java线程 • Java Threads May be Created by:Java线程可如下创建: • Extending Thread class 扩充线程类 • Implementing the Runnable interface 实现可运行接口 Applied Operating System Concepts

  27. Extending the Thread Class线程类型的扩展 class Worker1 extends Thread { public void run() { System.out.println(“I am a Worker Thread”); } } Applied Operating System Concepts

  28. Creating the Thread创建线程 public class First { public static void main(String args[]) { Worker runner = new Worker1(); runner.start(); System.out.println(“I am the main thread”); } } Applied Operating System Concepts

  29. The Runnable Interface可运行接口 public interface Runnable { public abstract void run(); } Applied Operating System Concepts

  30. Implementing the Runnable Interface可运行接口的实现 class Worker2 implements Runnable { public void run() { System.out.println(“I am a Worker Thread”); } } Applied Operating System Concepts

  31. Creating the Thread创建线程 public class Second { public static void main(String args[]) { Runnable runner = new Worker2(); Thread thrd = new Thread(runner); thrd.start(); System.out.println(“I am the main thread”); } } Applied Operating System Concepts

  32. Java Thread ManagementJava线程的管理 • suspend()– suspends execution of the currently running thread.挂起 - 暂停当前线程的运行 • 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. 停止 - 停止一个线程的执行 Applied Operating System Concepts

  33. Java Thread States Java线程状态 Applied Operating System Concepts

  34. Summary(总结) The process model introduced in Chapter 4 assumed that a process was an executing program with a single thread of control. Many modern operating systems now provide features for a process to contain multiple threads of control. This chapter introduces many concepts associated with multithreaded computer systems and covers how to use Java to create and manipulate threads. We have found it especially useful to discuss how a Java thread maps to the thread model of the host operating system. Applied Operating System Concepts

  35. 作业 • P132 5.1 5.2 5.4 Applied Operating System Concepts

More Related