Program Thread 和 Process的不同點
- Thread is for execution
- Kernel level thread, physical parallelism
- Cores Divide work amount of physical cores / CPU
- Load balancing
- Data Splitting
- Which will lead to data dependency coodination and message passing
- Bad thing for multiple cores is this is hard for Testing and debugging
- User level thread only have logical parallelism
- Example, Read from user input is blocking; we have to work on it to make it logical parallelism
- Kernel level thread, physical parallelism
- Process is for resource
What is Program
Program是一個存在disk中且斷電或重啟不會消失可執行檔案,儲存在儲存媒介中,以實體檔案的形態存在
A program is an executable file residing on the disk (secondary storage) in a directory. It is also termed as a set of instructions stored in the secondary storage device that are intended to carry out a specific job. It is read into the primary memory and executed by the kernel.
本文來自 Abbymz 的CSDN 部落格 ,全文地址請點選: https://blog.csdn.net/Abby210/article/details/51225034?utm_source=copy
或者可以稱之為persistently 的檔案,斷電也不會消失。
What is Process ?
當program被執行之後,就變成了程序。執行者的許可權和程式所需的資料都會載入到記憶體中,OS會給予這些記憶體單元一個pid。
Unit of resource ownership (allocation) and unit of protection
text, Code of Programmer |
---|
Data |
Heap |
Stack |
一個program可以創建出很多個程序,關機或停電會死掉
Process的組成
- Permission user or kernel
- Priority
- Files
- ID
- PID
- PPID
- Folk
- UserID
- Protected access to
- Processors
- other process
- Files
- I/O
- A virtual space that holds the process image
What is Thread ?
執行緒稱之為Lightweight Process,一個程序可以有多個執行緒,他們共享一片記憶體
- has access to the same data
- When one thread alters a data, other threads see the results
- When one thread open a file, other threads can also access that file.
Unit of dispatch or unit of execution
多執行緒的好處
- 開啟執行緒要快於開啟程序
- 關閉執行緒要快於關閉程序
- 切換執行緒要快於切換程序
- 執行緒可以相互通訊
什麼叫Multithreading
The ability of an OS to support multiple, concurrent paths of execution within a single process.
Process and thread states
- Ready
- new process usually set as read state
- At this time scheduler is not pick it up yet.
- If it get picked by scheduler and running in CPU, then goes to Running State
- If it get external event, something like lock, then goes to block
- Running,
- Scheduler picked it up, and CPU is run this instruction
- If it get finished, goes to Exit states
- If it get IO interrupts, goes to ready, blocked status
- If it can not get the resource which is conflict with other process, goes to deadlock
- Scheduler picked it up, and CPU is run this instruction
- Block
- If it lock (wait able ) is release, or IO is completed, then goes to Ready
- Deadlock
- Exit (Zombie State)
- When process is finished, waiting for cleaning up
How Deadlock happen ?
- When process A has resource a, and it need resource b from process B.
- At the same time, process B need resource a from A
- Then those process gonna have deadlock due to both of them can get the resource they need.
Reference
https://slideplayer.com/slide/5219996/
想要看到更多瑋哥的學習筆記、考試複習資料、面試準備資料?想要看到IBM工作時期的技術積累和國外初創公司的經驗總結?
image
敬請關注: