版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Chapter 5 Thread,Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads,Why we need threads?,Thinking:why the web browser can download page, display and interact with user concurrently? solution:create child processes to deal with d
2、ifferent tasks? How about performance? A process has its own resource and executes independently. There exists resource reallocating and status switching-cost greatly,Why we need threads?(cont.),Why we need threads ?(cont.),Multi-Threading,Why limit ourselves to a single thread? Think of a web serve
3、r 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? Context swtich Multi-threading model Each process can have multiple threads(多线程) Each thread has a private stack Registers are also private All thr
4、eads of a process share the code and heap Objects to be shared across multiple threads should be allocated on the heap,Single and Multithreaded Processes,Benefits,Responsiveness响应时间 Resource Sharing资源共享 Economy经济 Utilization of MP Architectures 利用多处理机结构,Thread,Thread is an attribute of a process and
5、 is the independently scheduling unit线程是进程的一个属性,是被系统独立调度的基本单位,是CPU的抽象 A process can create many threads, which share the processs resource Multiple threads run concurrently(并发执行), switch among them is simple 线程的特征:动态性、并发性、(运行)独立性、异步性,Thread (cont.),Eg. In Windows, each process use its own 4GB memory
6、, and multiple threads of the process share the same 4GB Suppose:process 1 needs to access 1000 in memory, process 2 needs to access 1000 in memory, they will use different address in the memory。But two threads, accessing 1000, of a process, use the same address,Thread (cont.),线程与进程的比较 (1)调度 同一进程的多线
7、程间调度时,不引起进程的切换 不同进程的线程间调度,需要进程切换 (2)并发性 一个进程的多个线程之间可并发执行 (3)资源的拥有 线程不拥有系统资源,不拥有代码段、数据段。,Multi-Threading (cont),Implementation Each thread is described by a thread-control block (TCB线程控制块) A TCB typically contains Thread ID Execution State: CPU registers, program counter, pointer to stack Space for s
8、aving registers Scheduling info: State, priority, CPU time Various Pointers (for implementing scheduling queues) Pointer to enclosing process? (PCB) Etc (add stuff as you find a need),Process Address Space Revisited,OS,Code,Globals,Stack,Heap,OS,Code,Globals,Stack,Heap,Stack,(a) Single-threaded addr
9、ess space,(b) Multi-threaded address space,Multi-Threading (cont.),Observation 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 stack,User Threads,Thread manageme
10、nt done by user-level threads library使用用户级的线程库完成线程的管理 Examples 举例 - POSIX Pthreads - Mach C-threads - Solaris threads,Kernel Threads,Supported by the Kernel直接由内核支持 Examples举例 - Windows 95/98/NT/2000 - Solaris - Tru64 UNIX - BeOS - Linux,User vs. Kernel Threads,Thread Management Done by User-Level Th
11、reads Library 对用户线程的支持通常处于内核之上,通过一个用户级线程库实现。线程库提供了对线程的创建、调度和管理的支持,这无需来自内核的支持。所有的线程创建和调度工作都在用户空间完成,而且整个过程不受内核的干涉。 Thread Management Supported by the Kernel 内核线程由操作系统直接支持:内核在内核空间内实现了线程的创建、调度和管理。,User vs. Kernel Threads,Advantages of user threads Performance(性能): low-cost thread operations (do not requ
12、ire 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 advant
13、age of multiprocessing (the kernel assigns one process to only one processor),User vs. Kernel Threads,process,processor,user threads,thread scheduling,process scheduling,kernel threads,thread scheduling,kernel,user,processor,threads,threads,process scheduling,User vs. Kernel Threads,No reason why we
14、 shouldnt have both Most systems now support kernel threads User-level threads are available as linkable libraries,kernel threads,processor,user threads,thread scheduling,thread scheduling,kernel,user,process scheduling,Multithreading Models,Many-to-One多对一 One-to-One 一对一 Many-to-Many 多对多,Many-to-One
15、,Many user-level threads mapped to single kernel thread. Used on systems that do not support kernel threads.,Many-to-One Model,One-to-One,Each user-level thread maps to kernel thread.每一个用户级的线程都映射到一个内核的线程上。 Examples - Windows 95/98/NT/2000 - OS/2,One-to-one Model,Many-to-Many Model,Allows many user l
16、evel threads to be mapped to many kernel threads. Allows the operating system to create a sufficient number of kernel threads. Solaris 2 Windows NT/2000 with the ThreadFiber package,Many-to-Many Model,Threading Issues,Semantics of fork() and exec() system calls. fork() 和 exec() 系统调用的语义 Thread cancel
17、lation. Signal handling Thread pools 线程池 Thread specific data,Pthreads,a POSIX standard (IEEE 1003.1c) API for thread creation and synchronization. IEEE为UNIX标准化定义的一个标准。线程创建和同步的应用编程的界面。 API specifies behavior of the thread library, implementation is up to development of the library. 该标准中的API定义了线程库的行为
18、,实现取决于线程库的开发。 Common in UNIX operating systems. UNIX操作系统中大都实现了该标准。,Solaris 2 Threads,Solaris Process,Windows 2000 Threads,Implements the one-to-one mapping. Each thread contains - a thread id - register set - separate user and kernel stacks - private data storage area,Linux Threads,Linux refers to t
19、hem as tasks rather than threads. Linux中通常不叫线程,而是叫任务。 Thread creation is done through clone() system call. 线程的创建通过clone()系统调用实现。 Clone() allows a child task to share the address space of the parent task (process) Clone() 容许子任务共享父进程的地址空间。,Java Threads,Java threads may be created by:JAVA的线程可以按以下方法来创建:
20、 Extending Thread class 扩展线程类 Implementing the Runnable interface 实现可以运行的界面 Java threads are managed by the JVM.JAVA的线程由JVM管理。,Java Threads,public class TestThread1 public static void main(String args) Runner1 r = new Runner1(); r.start(); for(int i=0; i100; i+) System.out.println(Main Thread:- + i)
21、; class Runner1 extends Thread public void run() for(int i=0; i100; i+) System.out.println(Runner1 : + i); ,Java Threads,public class TestThread2 public static void main(String args) Runner2 r = new Runner2(); Thread t1 = new Thread(r); Thread t2 = new Thread(r); t1.start(); t2.start(); class Runner2 implements Runnable public void run() for(int i=0; i30; i+) System.out.println(Thread.currentThread().getName+ i); ,Java Thread States,作业,1. Provide two programming examples of multithreading giving improved performance over a single-threaded solution. 2. Provide two programming exa
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026湖南宁远政务新媒体运营劳务派遣人员招聘2人农业考试模拟试题及答案解析
- 2026西藏林芝波密县招聘大学生乡村医生5人农业考试参考题库及答案解析
- 2026广东广州市荔湾中心医院招聘编外工作人员68人(第一批)农业考试备考试题及答案解析
- 2026年宁波北仑区霞浦街道招聘编外工作人员1人农业考试参考题库及答案解析
- 2026四川宜宾发展创投有限公司第一批员工招聘7人农业考试备考题库及答案解析
- 家国情怀高中作文演讲稿
- 2026甘肃人力资源服务股份有限公司招聘农业笔试备考试题及答案解析
- 2026国家民委直属事业单位招聘12人(北京)农业考试模拟试题及答案解析
- 2026重庆港股份有限公司招聘46人农业考试备考试题及答案解析
- 2026浙江宁波大学附属人民医院招聘编外人员2人农业笔试模拟试题及答案解析
- 2026年北京市海淀区高三一模语文试卷(含答案)
- 建筑垃圾减量化监理监督实施细则
- GB/T 18711-2025选煤用磁铁矿粉试验方法
- 2024年同等学力申硕英语考试真题
- 最优切割模型
- 内耗的分类、特点及其与金属结构的关系
- GA∕T 1776-2021 警用机器人系统通用技术要求
- 质量成本CoPQ(课堂PPT)
- 第二章投标人须知
- 上海市监理通用表(共45页)
- 云管理系统订单管理操作手册
评论
0/150
提交评论