350 likes | 363 Views
Explore user and kernel threads, multithreading models, and examples like Solaris and Java. Learn the advantages and disadvantages of user versus kernel threads and various multithreading models.
E N D
Module 5: Threads线程 • Overview 综述 • Benefits 益处 • User and Kernel Threads 用户和内核线程 • Multithreading Models 多线程模型 • Solaris 2 Threads Solaris 2线程 • Java Threads Java线程 Operating System Concepts
Benefits益处 • Responsiveness 响应 • Resource Sharing 资源共享 • Economy经济性 • Utilization of MP Architectures MP体系结构的运用 Operating System Concepts
Single and Multithreaded Processes单个和多线程进程 Operating System Concepts
Multithreads example • A www server Operating System Concepts
Multithreads example • A www server Operating System Concepts
Multi-Threading • Why limit ourselves 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 stack • Registers are also private • All threads of a process share the code and heap • Objects to be shared across multiple threads should be allocated on the heap Operating System Concepts
Multi-Threading (cont) • Implementation • Each thread is described by a thread-control block (TCB) • A TCB typically contains • Thread ID • Space for saving registers • Pointer to thread-specific data not on stack • Observation • Although the model is that each thread has a private stack, threads actually share the process address space • There’s no memory protection! • Threads could potentially write into each other’s stack Operating System Concepts
OS OS Code Code Globals Globals Stack Stack Stack Heap Heap Process Address Space Revisited (a) Single-threaded address space (b) Multi-threaded address space Operating System Concepts
User Threads用户线程 • Thread Management Done by User-Level Threads Library由用户级线程库进行管理的线程 • Examples例子 - POSIX Pthreads - Mach C-threads - Solaris threads Operating System Concepts
Kernel Threads内核线程 • Supported by the Kernel由内核支持 • Examples例子 - Windows 95/98/NT - Solaris - Digital UNIX Operating System Concepts
User vs. Kernel Threads • Advantages of user threads • Performance: low-cost thread operations (do not require crossing protection domains) • Flexibility: scheduling can be application specific • Portability: user thread library easy to port • Disadvantages 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 advantage of multiprocessing (the kernel assigns one process to only one processor) Operating System Concepts
User vs. Kernel Threads user threads kernel threads threads thread scheduling user kernel threads process thread scheduling process scheduling process scheduling processor processor Operating System Concepts
User vs. Kernel Threads • No reason why we shouldn’t have both • Most systems now support kernel threads • User-level threads are available as linkable libraries user threads thread scheduling user kernel kernel threads thread scheduling process scheduling processor Operating System Concepts
Multithreading Models多线程模型 • Many-to-One多对一 • One-to-One一对一 • Many-to-Many 多对多 Operating System Concepts
Many-to-One多对一 • Many User-Level Threads Mapped to Single Kernel Thread.多个用户级线程映像进单个内核线程 • Used on Systems That Do Not Support Kernel Threads. 用于不支持内核线程的系统中 Operating System Concepts
Many-to-one Model多对一模型 Operating System Concepts
One-to-One一对一 • Each User-Level Thread Maps to Kernel Thread.每个用户级线程映像进内核线程 • Examples - Windows 95/98/NT - OS/2 Operating System Concepts
One-to-one Model一对一模型 Operating System Concepts
Many-to-many Model多对多模型 Operating System Concepts
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. Operating System Concepts
Solaris 2 Threads Solaris 2线程 Operating System Concepts
solaris • Unix 的发展分支 • 4.XBSD是由加州大学贝克莱分校计算机系统研究组开发的。该研究组也发布BSD NET1和BSD NET2版,它们包含了4.XBSD系统公众可用源代码。SVRX是AT& T的系统V的简称。XPG3是X/Open可移植性指南的第三次发行本的简称。ANSI C是C程序设计语言的ANSI标准。POSIX.1是Unix类系统界面的IEEE和ISD标准。 Operating System Concepts
Solaris Process Solaris 线程 Operating System Concepts
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) Operating System Concepts
Java Threads Java线程 • Java Threads May be Created by:Java线程可如下创建: • Extending Thread class 扩充线程类 • Implementing the Runnable interface 实现可运行接口 Operating System Concepts
Extending the Thread Class线程类型的扩展 class Worker1 extends Thread { public void run() { System.out.println(“I am a Worker Thread”); } } Operating System Concepts
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”); } } Operating System Concepts
The Runnable Interface可运行接口 public interface Runnable { public abstract void run(); } Operating System Concepts
Implementing the Runnable Interface可运行接口的实现 class Worker2 implements Runnable { public void run() { System.out.println(“I am a Worker Thread”); } } Operating System Concepts
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”); } } Operating System Concepts
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. 停止 - 停止一个线程的执行 Operating System Concepts
Java Thread States Java线程状态 Operating System Concepts
Producer Consumer Problem生产着消费者问题 public class Server { public Server() { MessageQueue mailBox = new MessageQueue(); Producer producerThread = new Producer(mailBox); Consumer consumerThread = new Consumer(mailBox); producerThread.start(); consumerThread.start(); } public static void main(String args[]) { Server server = new Server(); } } Operating System Concepts
Producer Thread生产者线程 class Producer extends Thread { public Producer(MessageQueue m) { mbox = m; } public void run() { while (true) { // produce an item & enter it into the buffer Date message = new Date(); mbox.send(message); } } private MessageQueue mbox; } Operating System Concepts
Consumer Thread消费者线程 class Consumer extends Thread { public Consumer(MessageQueue m) { mbox = m; } public void run() { while (true) { Date message = (Date)mbox.receive(); if (message != null) // consume the message } } private MessageQueue mbox; } Operating System Concepts