目錄
process & thread
process(程序/進程)
- CUP中待執行的程式碼,thread的容器(實際執行任務的是thread)
- 一個process
可擁有多個thread
- 有
new、ready、running、waiting、terminated、suspended
6種狀態 - 每個
process間是獨立
的,較難互相溝通The process has its own Process Control Block, Stack, and Address Space.
- 在多功作業系統(Multitasking Operating System)中,可以同時執行多個process,這就是multiprocessing。但一個CPU一次只能執行一個process,所以才有多核心CPU的誕生
- process中的thread可以並行(concurrency)或平行(parallelism)的方式運作
thread(執行緒/線程)
- 組成process的部分
- 多個thread可以
被同個process擁有
- 有
Running、Ready、Blocked
3種狀態 - 每個
thread間並非獨立
的。它們共享定址空間、資源,所以當main thread有任何的變動、或者前面的thread crash會都影響到其他threadthreads of the same process share address space and other resources.
- 執行程式碼時,thread會將變數存在stack(記憶體空間)中。
stack
只有該thread可以使用,並不共享
,但heap
可以被同個process的thread共享
Thread has Parents’ PCB, its own Thread Control Block, and Stack and common Address space.
thread pool
線程池。粗略地說的可以把thread pool視為存放預先建立(實例化)好的thread的地方
,當thread執行完了任務,就去queue依序拿一個來執行。先實例化thread,是因為這樣就不用重複建立,可以提高效能
In computer programming, a thread pool is a software design pattern for achieving concurrency of execution in a computer program. Often also called a replicated workers or worker-crew model, a thread pool maintains multiple threads waiting for tasks to be allocated for concurrent execution by the supervising program.
By maintaining a pool of threads, the model increases performance and avoids latency in execution due to frequent creation and destruction of threads for short-lived tasks.The number of available threads is tuned to the computing resources available to the program, such as a parallel task queue after completion of execution.
以下是上文翻譯
在程式的領域,thread pool是一種用於達到並行執行程式的設計模式。它也常被稱作replicated workers 或 worker-crew model。
thread pool會有多個thread在等被發配到任務。thread pool以不重複建立又拆掉thread,來提升效能、避免運行時的延遲。
可以同時被維持的thread的數量取決於程式可以取得的計算資源(CPU之意)。例如平行執行時可取得的資源會比一個完成後再執行下一個少
🖊名詞解釋
名詞 | 解釋 | 其他 |
---|---|---|
process control block | process control block是一種資料結構,裡面存了關於process的資料(ex: state, number, program counter , cpu register , scheduling information, memory management...) |
|
program counter | program counter會指出process下一個要執行的指令存放的路徑 | |
cpu register | register是一種暫存器,可存放指令、儲存路徑、資料...。 | Along with the program counter this state information must be saved when an interrupt occurs and to allow the process to be continued correctly afterward. |
context switching | CPU執行一個process時,另一個事件發生了。它去處理新事件之前會把執行到一半的任務的進度存到PCB,之後再讀進度繼續執行 | a context switch is the process of storing the state of a process or thread, so that it can be restored and resume execution at a later point. |
multiprocessing | 系統同時用多個CUP(processors)同時執行多個process | useful for CPU-bound processes, such as computationally heavy tasks |
multithreading | 單個CUP(processors)同時執行多個thread | useful for IO-bound processes, such as reading files from a network or database |
concurrency | 快速context switching產生的假象 | interleaved operation |
parallelism | 同時執行多個process,不context switching | using multiple CUP |
📖參考資料
Difference between Process and Thread
Program/Process/Thread 差異
[CS] 進程與線程的概念整理
進程-線程-協程-傻傻分得清楚
Thread Pool
What is context Switching?
difference between multiprocessing and multithreading
difference between concurrency and parallelism