版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Applied Operating System Concepts,Chap 5 Threads線程,Applied Operating System Concepts,Contents內(nèi)容,Benefits 益處Thread 線程綜述User and Kernel Threads 用戶和內(nèi)核線程Multithreading Models 多線程模型Solaris 2 Threads Solari
2、s 2線程Windows2000 Threads Windows2000線程Java Threads Java線程Summary(總結(jié))Homework作業(yè),Applied Operating System Concepts,,Responsiveness 響應(yīng)Resource Sharing 資源共享Economy經(jīng)濟(jì)性Utilization of MP Architectures MP體
3、系結(jié)構(gòu)的運(yùn)用,Benefits 益處,Applied Operating System Concepts,Threads線程,A thread (or lightweight process) is a basic unit of CPU utilization; it consists of: 線程(輕型進(jìn)程)是CPU運(yùn)用的一個(gè)基本單元,包括program counter 程序計(jì)數(shù)器register set 寄存器
4、集stack space ??臻gA thread shares with its peer threads its: 一個(gè)線程與它的對(duì)等線程共享:code section 代碼段data section 數(shù)據(jù)段operating-system resources 操作系統(tǒng)資源collectively know as a task. 總體作為一個(gè)任務(wù)A traditional or heavyweight proces
5、s is equal to a task with one thread 傳統(tǒng)的或重型進(jìn)程等價(jià)于只有一個(gè)線程的任務(wù),Applied Operating System Concepts,Threads線程(Cont.),In a multiple threaded task, while one server thread is blocked and waiting, a second thread in the same tas
6、k can run. 在一個(gè)多線程任務(wù)中,當(dāng)一個(gè)服務(wù)器線程被阻塞后,在同一個(gè)任務(wù)中的第2個(gè)線程可運(yùn)行Cooperation of multiple threads in same job confers higher hroughput and improved performance. 同一作業(yè)中多線程協(xié)同導(dǎo)致高吞吐量并改善性能Applications that require sharing a common buffer
7、(i.e., producer-consumer) benefit from thread utilization. 應(yīng)用程序需要共享一個(gè)公用的緩沖(即生產(chǎn)者-消費(fèi)者),從線程運(yùn)用中得利,Applied Operating System Concepts,Threads provide a mechanism that allows sequential processes to make blocking system calls
8、while also achieving parallelism. 線程提供了一種機(jī)制,允許序列進(jìn)程阻塞系統(tǒng)調(diào)用同時(shí)還能實(shí)現(xiàn)并行Kernel-supported threads 內(nèi)核支持線程(Mach and OS/2). User-level threads; supported above the kernel, via a set of library calls at the user level (Project A
9、ndrew from CMU). 用戶級(jí)線程;在內(nèi)核之上,通過用戶級(jí)的庫(kù)調(diào)用Hybrid approach implements both user-level and kernel-supported threads (Solaris 2). 混合處理實(shí)現(xiàn)用戶級(jí)和內(nèi)核支持線程,Threads線程(Cont.),Applied Operating System Concepts,Thread and Process線程和進(jìn)程,Sche
10、duling調(diào)度線程是調(diào)度的基本單位,同一進(jìn)程中的線程切換不會(huì)引起進(jìn)程切換。Concurrence并發(fā)線程可以提高系統(tǒng)的并發(fā)性。Resource資源進(jìn)程擁有資源,是資源分配的基本單位,而線程則不擁有資源,但它可以訪問創(chuàng)建它的進(jìn)程所擁有的資源。Context Switch上下文切換線程的上下文切換的代價(jià)比進(jìn)程小。,Applied Operating System Concepts,Windows 2000 Process a
11、nd Thread Windows 2000進(jìn)程和線程,Applied Operating System Concepts,Single and Multithreaded Processes單個(gè)和多線程進(jìn)程,Applied Operating System Concepts,Thread and Child Process線程和子進(jìn)程,,,,,Thread,Thread,Process,,,Child Process,,Appli
12、ed Operating System Concepts,User-Level Threads (ULT)用戶線程,Thread Management Done by User-Level Threads Library,such as thread creation, scheduling and management.由用戶級(jí)線程庫(kù)進(jìn)行管理的線程,如線程創(chuàng)建、調(diào)度等。The kernel is unaware of user-
13、level thread, all thread creation and scheduling are done in user space without the need for kernel intervention 。內(nèi)核看不到ULT,線程的創(chuàng)建和調(diào)度在用戶空間,不需要內(nèi)核的干預(yù)。 Examples例子- POSIX Pthreads- Mach C-threads- Solaris threads,Applie
14、d Operating System Concepts,User-Level Threads用戶線程(Cont.),,,,Thread,,Thread,,Thread,Process,Process,single-threaded multi-threaded 單線程進(jìn)程 多線
15、程進(jìn)程運(yùn)行線程的阻塞導(dǎo)致整個(gè)進(jìn)程阻塞 運(yùn)行進(jìn)程的阻塞不會(huì)導(dǎo)致進(jìn)程的阻塞,,BLOCK,,BLOCK,,cause,BLOCK,,,RUN,Process Scheduling,,,Applied Operating System Concepts,Kernel-Level Threads內(nèi)核線程,Supported by the Kernel由內(nèi)核支持KLT creation ,scheduling, and
16、management are done by the kernel in kernel space.KLT創(chuàng)建、調(diào)度和管理由內(nèi)核在核心空間運(yùn)行。KLT are generally slower to create and manage than ULT. KLT創(chuàng)建和管理通常比ULT慢。Examples例子- Windows 95/98/NT - Solaris- Digital UNIX,Applied Operati
17、ng System Concepts,Multithreading Models多線程模型,Many-to-One多對(duì)一One-to-One一對(duì)一Many-to-Many 多對(duì)多,Applied Operating System Concepts,Many-to-One多對(duì)一,Many User-Level Threads Mapped to Single Kernel-Level Thread.多個(gè)用戶級(jí)線程映像進(jìn)單
18、個(gè)內(nèi)核線程Multiple threads are unable to run in parallel on multiprocessor.多個(gè)線程不能并行運(yùn)行在多個(gè)處理器上。Thread management is done in user space ,so it’s efficient, but the entire process will block if a thread makes a blocking system
19、call. 線程管理在用戶態(tài)執(zhí)行,因此是高效的,但一個(gè)線程的阻塞系統(tǒng)調(diào)用會(huì)導(dǎo)致整個(gè)進(jìn)程的阻塞。 Used on Systems That Do Not Support Kernel Threads. 用于不支持內(nèi)核線程的系統(tǒng)中,Applied Operating System Concepts,Many-to-one Model多對(duì)一模型,Applied Operating System Concepts,One-to-One一
20、對(duì)一,Each User-Level Thread Maps to Kernel-Level Thread.每個(gè)用戶級(jí)線程映像進(jìn)內(nèi)核線程More concurrency than the many-to-one model 比多對(duì)一模型有更好的并發(fā)性Allow multiple threads to run in parallel on multiprocessor允許多個(gè)線程并行運(yùn)行在多個(gè)處理器上Creating a U
21、LT requires creating the corresponding KLT 創(chuàng)建一個(gè)ULT需要?jiǎng)?chuàng)建一個(gè)KLT,效率較差Examples- Windows 95/98/NT- OS/2,Applied Operating System Concepts,One-to-one Model一對(duì)一模型,Applied Operating System Concepts,Many-to-many 多對(duì)多,Many ULTs
22、maps to a smaller or equal number of KLTs.多個(gè)ULT映射為相等或小于數(shù)目的KLT.More concurrency 更好的并發(fā)性ExamplesSolarisIrixDigital UNIX,Applied Operating System Concepts,Many-to-many Model多對(duì)多模型,Applied Operating System Concepts,Sol
23、aris 2 Threads Solaris 2線程,Applied Operating System Concepts,Solaris Process Solaris 線程,Applied Operating System Concepts,Windows 2000 ThreadW2K線程,線程狀態(tài)Ready就緒:可以被調(diào)度執(zhí)行Standbly備用:下一次可以運(yùn)行Running運(yùn)行:執(zhí)行線程Waiting等待:由于某個(gè)
24、事件阻塞Transition轉(zhuǎn)換:準(zhǔn)備好運(yùn)行但資源不可用時(shí),由等待進(jìn)入轉(zhuǎn)換Terminated終止:運(yùn)行結(jié)束。,Applied Operating System Concepts,Windows 2000 ThreadW2K線程(cont.),Applied Operating System Concepts,NT線程的有關(guān)API,CreateThread()函數(shù)在調(diào)用進(jìn)程的地址空間上創(chuàng)建一個(gè)線程,以執(zhí)行指定的函數(shù);返回值為所創(chuàng)
25、建線程的句柄。ExitThread()函數(shù)用于結(jié)束本線程。SuspendThread()函數(shù)用于掛起指定的線程。ResumeThread()函數(shù)遞減指定線程的掛起計(jì)數(shù),掛起計(jì)數(shù)為0時(shí),線程恢復(fù)執(zhí)行。,Applied Operating System Concepts,Java Threads Java線程,Java Threads May be Created by: Java線程可如下創(chuàng)建:Extending Threa
26、d class 擴(kuò)充線程類Implementing the Runnable interface 實(shí)現(xiàn)可運(yùn)行接口,Applied Operating System Concepts,Extending the Thread Class線程類型的擴(kuò)展,class Worker1 extends Thread{public void run() {System.out.println(“I am a Wor
27、ker Thread”);}},Applied Operating System Concepts,Creating the Thread創(chuàng)建線程,public class First{public static void main(String args[]) {Worker runner = new Worker1();runner.start();System.out.println(“I am
28、 the main thread”);}},Applied Operating System Concepts,The Runnable Interface可運(yùn)行接口,public interface Runnable{public abstract void run();},Applied Operating System Concepts,Implementing the Runnable Interface可運(yùn)行
29、接口的實(shí)現(xiàn),class Worker2 implements Runnable{public void run() {System.out.println(“I am a Worker Thread”);}},Applied Operating System Concepts,Creating the Thread創(chuàng)建線程,public class Second{public static void main
30、(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,Java Thread ManagementJava線程
31、的管理,suspend() – suspends execution of the currently running thread.掛起 - 暫停當(dāng)前線程的運(yùn)行sleep() – puts the currently running thread to sleep for a specified amount of time. 睡眠 - 讓當(dāng)前線程入睡一段指定的時(shí)間resume() – resumes execution
32、of a suspended thread. 恢復(fù) - 再執(zhí)行被掛起的線程stop() – stops execution of a thread. 停止 - 停止一個(gè)線程的執(zhí)行,Applied Operating System Concepts,Java Thread States Java線程狀態(tài),Applied Operating System Concepts,Summary(總結(jié)),The process mo
33、del 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 c
34、hapter 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 thre
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 眾賞文庫(kù)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- user_threads.gif
- 北大離散數(shù)學(xué)chap5
- 基礎(chǔ)化學(xué)第二版習(xí)題答案chap5
- [學(xué)習(xí)]網(wǎng)絡(luò)編程5-多線程服務(wù)器
- chap 認(rèn)證過程
- chap6.doc
- chap6.doc
- r1chap-14
- chapⅰ函數(shù)、極限與連續(xù)
- chap-15-4-7
- 投資學(xué)題庫(kù)chap
- bcb線程池
- chap07-樣條函數(shù)
- chap19security安全
- chap3電信13
- r1chap-03
- chap8-數(shù)論入門
- linux多線程編程
- 信息與編碼chap7
- intermediate accounting answer chap021
評(píng)論
0/150
提交評(píng)論