Please enable JavaScript.
Coggle requires JavaScript to display documents.
Chapter 4: Threads & Concurrency (Threading Issues (Scheduler…
Chapter 4: Threads &
Concurrency
Multicore Programming
兩種平行化
Data parallelism
:star: : 把資料分成好幾份,每個core做相同的事
Task parallelism
:star: : 每個thread做不一樣的事
Amdahl's Law
:star:
部分效能提升,對於總體而言可以提升多少?
Multithreading Models
兩種thread
User threads
:star: : POSIX
Pthreads
:star:, Windows threads, Java threads
Kernel threads
:star: : 一般作業系統
Multithreading Models (User - Kernel)
Many-to-One
缺點
一個卡住全部卡住
在多核系統中不能平行運作,因為一次只能有一個user thread在kernel中
例子 :
Solaris Green Threads
:star:、
GNU Portable Threads
:star:
One-to-One
每個process的thread樹有時會受到限制
例子 : Windows、Linux
Many-to-Many
允許OS建立夠多的kernel thread
Two-level
有點類似M:M,但允許user thread綁訂到kernel thread
Thread Libraries
Thread library
:star:
定義 : 專門為programmer提供創建、管理thread的API
兩種實作方法
整個library都在user space
由OS支援的Kernel-level library
Pthreads
:star::star::star:
僅是
規格
,而非
實作
Implicit Threading
隨著thread數↑,程式要正確的難度↑
thread的建立、管理是由compiler、run-time library在做,而非programmer
五種方法
Thread Pools
:star:
說明 : 先把thread建起來預備、等待
優點
速度較快
thread數目受到Pool大小限制
task可定期排程
Fork-Join
:star:
Fork-Join Parallelism
:star:
OpenMP
Grand Central Dispatch
Intel Threading Building Blocks
Threading Issues
Signal Handling
Signals
:star::star::star: : are used in UNIX to notify a process that a particular event has occurred
signal handler
:star::star::star: : 用來處理signal
default handler
:star:
每個signal都有
可被
User-defined signal handler
:star:override
Thread Cancellation
在thread完成前就把它停掉
target thread
:star: : 要被取消的thread,稱之
兩種方法
Asynchronous cancellation
: 它要你結束就得馬上結束
Deferred cancellation
: 完成某個執行點才結束
cancellation只發生在thread到達
cancellation point
:star時
Then呼叫
cleanup handler
:star:
Thread-Local Storage
(TLS):star:
讓不同thread有自己的stogare
=> 避免不同thread之間存取到不該存取的資料,造成錯誤
Scheduler Activations
需要溝通,以分配給各應用程式適當的thread數
lightweight process (LWP)
:star: : 用於user、kernel thread之間的資料結構
upcall handler
:star: : 當CPU輪到某process,而那個process剛好在睡覺(做IO),OS會提醒它一下,才不會直接把CPU白白讓給別的process
This communication allows an application to maintain the correct number kernel threads
Operating System Examples
Summary