




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- DB14-T 1585-2025 葡萄綠枝嫁接技術規程
- 高中語文人教版必修二《詩經-氓》課件
- 餐飲店股權轉讓與供應鏈優化協議
- 水利樞紐采石場租賃合同示范文本
- 2025年公共關系與傳播學考試試題及答案
- 2025年國際貿易與經濟合作試卷及答案
- 2025年城市規劃與設計專業考試真題及答案
- 湖北省武漢市武昌區2025屆高三下學期5月質量檢測(三模) 數學試題【含答案】
- 郴州環保科技公司勞動合同管理
- 農村地區柴油發電設備采購與安裝服務合同
- 項目延期申請表
- 體系文件編號規則
- 患者突發昏迷應急預案演練腳本-
- 計算機應用基礎-終結性考試試題國開要求
- 2023年全國統一高考真題物理試卷(新課標ⅰ)(含答案及解析)
- 2023年05月四川省廣安市司法局公開招考2名勞務派遣制司法行政輔助人員筆試題庫含答案解析
- 公司“三基”工作檢查評比細則(搶維修管理)
- 《安裝條》浙江省建筑設備安裝工程提高質量的若干意見
- 分布式系統復習題與答案
- 甘肅水資源概況
- 運動員簡歷模板
評論
0/150
提交評論