---
tags: 作業系統
---
# 作業系統 Ch4 Threads
### benefit
- responsiveness
- resource sharing
- economy
- scalability
### multicore
- single core - concurrency: support several tasks
- multi core - parallelism: perform several tasks
- data parallelism
- task parallelism: each thread perform unique operation
### each thread have
- tid
- stack
- program counter
- CPU register
### Amdahl's law
S: serial portion
N: number of core
1-S: parallelism portion
> speed up <= 1/(S+(1-S)/N)
### user-level thread library
- POSIX Pthreads:
- POSIX: stardard of parallilism thread
- Pthread: is lib
- Window threads
- Java threads
### multithreading modal
- many-to-one
- many-to-many
- one-to-one: concurrency、overhead
- two-level: like m:m, bound to kernal thread
### thread library
- API for creation and management threads
- entirely in user-mode
- kernal-thread support by OS
- Pthread
- either in user or kernal mode
- specification, not implementation
### implement thread
- creation and management of threads done by compilers and run-time libary
- method
- thread pool: faster、control thread amount、diff way to running task
- openMP: shared-memory、parallel regions
- grand central dispatch: base on thread pool
### signal
- notify process something happen
- signal handler
- default
- user-defined
### thread cancellation
- cancel thread: target thread
- type
- asynchronous cancellation: immediately
- deferred cancellation(default): periodically check、cleanup handler
### TLS ( thread-local-storage )
- allow each thread have own copy of data
- useful when no control (ex: thread pool)
- visiable when function invocation
### Scheduler Activation
- to maintain number of kernal thread
- LWP(lightweight process): like virtual processor、attach to kernal thread
- upcalls: communication mechanism between kernal and upcall handler