# OS Chapter4 ###### tags: `作業系統` ## *Single and Multithreaded processes* ![](https://i.imgur.com/l8jUan5.png =40%x) ![](https://i.imgur.com/Ee5B5DN.png =50%x) :warning: 兩張圖各代表一個process - multithread中thread之間是共用記憶體,所以在存取資料上會更方便,並且因為在同一個process所以溝通上更方便 ## *Multithreaded Server Architecture* ![](https://i.imgur.com/RSD3QW0.png) - 在產生新的thread的時候不用像產生新的process一樣需要複製parent process,節省空間 ## *Multithreaded Server benefits* 1. **Responsiveness** : 如果部分process被鎖住仍然可以繼續執行 2. **Resource Sharing** : threads享有共同空間 3. **Economy** : 比產生process還便宜 4. **Scalability(可擴展性)** : process可以利用multicore architectures ## *Multicore Programming* Multicore or multiprocessor systems putting pressure on programmers : - Dividing activites - Balance - Data splitting - Data dependency - Testing and debugging :arrow_right: therefore exploring the parallelism ## *Concurrency v.s. Parallelism* :::success ### Concurrent execution on single-core system cnocurrent : 分時共享,使之有種在同步執行的感覺 ![](https://i.imgur.com/z0gPgPl.png) ::: :::info ### Parallelism on multi-core system parallelism : 同時間真的有2個以上task在進行 ![](https://i.imgur.com/VYFzFBi.png) ::: ## *Data and Task Parallelism* :::success ### Data parallelism 把同一份資料分配在不同的core裡面,每一個core做一樣的事情 ![](https://i.imgur.com/tqRnrAa.png) ::: :::info ### Task parallelism 分配不同的threads在一個core裡面,每個thread做不一樣的事情 ![](https://i.imgur.com/YaTdTiW.png) ::: ## *Multithreading Models* | | 由誰管理thread | OS是否可以察覺thread | System call | | -------------- | ------------------ | -------------------- | --- | | User threads | User-level library | No | 不會使用到system call | | Kernel threads | OS、kernel | Yes | 專門處理 System Call | :::success ### Many-to-One 多個user-level threads :arrow_right: 單一kernel thread **問題** : 同時間還是只有一個thread執行,因為kernel不支援multi thread,所以當一個thread鎖住的時候(比如waiting state)會導致全部的thread都鎖住 **解決方法** : 在要進行I/O的時候進行攔截 ![](https://i.imgur.com/BcqyyWI.png) ::: :::info ### One-to-One 每一個user-level thread :arrow_right: 單一kernel thread **問題** : 多個threads可能導致overhead ![](https://i.imgur.com/dWjNxbP.png) ::: :::warning ### Many-to-Many 多個user-level threads :arrow_right: 多個kernel thread ![](https://i.imgur.com/JMoOYjx.png) ::: :::danger ### Two-level 類似M:M,但它允許一個user thread可以綁定到kernel thread **優點** : 能夠處理即時反應,使一些thread有專屬 ![](https://i.imgur.com/f6S8rTR.png) ::: ## *Thread Libraries* thread library 提供可以創造和管理thread的API給開發者 **Pthreads** - 可以提供給user-level與kernel-leve - 新增及同步執行緒的標準是POSIX。 - Pthreads是定義不是實作。 - thread library的行為由API決定,而執行方面則取決於thread library的發展。 **JAVA thread** : - JVM管理 - 依照OS給予什麼thread,便實行什麼樣的thread - 兩種新增方式: 1. 繼承Thread類別 2. 用Runnable interface產生的間接thread。 ## *Implicit Threading* 由編譯器和run-time libraries(執行時函式庫)管理和製造threads,而不是開發者負責 :::success ### 方法一 : Thread Pools - 是一個存放等待被叫去工作的threads的地方。 - 優點: 1. 節省去產生一個thread的成本。 2. 可允許thread的大小跟Pool一樣大。 3. 可以同步處理要求,不需要中斷。 4. Usually slightly faster to service a request with an existing thread than create a new thread ::: :::info ### 方法二 : Fork-Join Parallelism 多個thread(task)被forked(生成)然後joined(合併) ![](https://i.imgur.com/ZxEB8Ag.png) ::: ## *fork() and exec()* **fork()** : 生成threads時,看系統是單一新增呼叫的,還是把所有的threads都複製出來。 **exec()** : 執行process中所有的threads ## *Signal Handling* 在Unix中signal用來通知process一個特別的事件發生 :::success **signal handler** : 管理signal 1. 特別事件產生signal 2. siganl會被傳送到一個process 3. signal會被其中一種signal handlers處理 * default * user-defined 每一個signal 都有default handler,而user-defined signal handler 可以覆寫default ::: :::warning :question: 當一個signal要被送到multi-thread的時候,它該送到哪個地方? 1. 把訊號傳到有應用的thread 2. 把訊號傳送到process裡面全部的thread 3. 把訊號傳送到process裡面特定的thread ::: ## *Thread Cancellation* - 在thread結束之前將其終止 - target thread就是要被消除的thread :::success ### 方法一 : Asynchronous cancellation 方法 : 立即終止target thread 問題 : 但因為thread是共用的,所以其他的thread可能會受到影響 ::: :::info ### 方法二 : Deferred cancellation 方法 : 在確保target thread不會影響到其他thread的時候終止 cancellation depends on thread state 如果thread現在的狀態是disabled,那cancellation就會等到狀態變成enable的時候才消除 ![](https://i.imgur.com/WQQTFg7.png) ::: ## *Thread-Local Storage* thread-local storage(TLS)允許每個thread有它自己的資料,並且只有這個thread可以使用 ## *Scheduler Activations* M:M和Two-level models通訊要求間,維持適當的kernel threads的數量,再分配給應用程式 **lightwegiht process(LWP)**:在user thread跟kernel thread中間的資料結構,提供虛擬行程的排程 **upcall** : 一種由schedular activation提供的通訊機制(kernel和thread library裡的upcall handler之間),用於控制kernel thread的數量 ![](https://i.imgur.com/VroQaqn.png) **** 參考 : https://ithelp.ithome.com.tw/articles/10204372