Zero871015
Operating System #3
Try
HackMD
Zero871015
·
Follow
Last edited by
Zero871015
on
Nov 12, 2019
Linked with GitHub
Contributed by
Operating System #3
Program VS Process
Program和Process中文都翻譯成程式或程序,因此兩者的區別是基礎卻常見的考題。
Process是指執行中的Program,當一個Program被load到記憶體之後就成為了Process。
超級重要!面試常考題目!
一個Process在記憶體中被分為幾個部分:
program Code,也稱為text section
program counter,紀錄現在process活動到哪裡
stack,存放臨時資料(動態資料),包含function的參數、回傳指標、區域變數等
data section,包含全域變數等靜態資料
heap,包含動態產生的記憶體空間資料(malloc或是new等指令產生的資料)
Program是passive,而process則是active;也就是執行中的程式才叫process。
Image Not Showing
Possible Reasons
The image file may be corrupted
The server hosting the image is unavailable
The image path is incorrect
The image format is not supported
Learn More →
Process State
一個process的執行會不停切換狀態:
new:當該process被創造時
running:process的指令正在被執行時
waiting:process因為某些事件而暫停時
ready:process在等候被OS選到執行時(前面說過一次只有一個process執行)
terminated:當process結束時
Image Not Showing
Possible Reasons
The image file may be corrupted
The server hosting the image is unavailable
The image path is incorrect
The image format is not supported
Learn More →
雖然現今有些作業系統已經將new和ready合併,但考試時還是得寫完整以求保險。
Process Control Block (PCB)
在OS中,每個Process都有一個對應的PCB。
PCB存放Process的相關資訊,也稱作task control block。
Process state:目前的狀態
Program counter:下一個要執行的指令位置
CPU registers:暫存器的內容
CPU scheduling information:排程時的相關屬性(後面提到排程時會更詳細)
Memory-management information:分配給該Process的記憶體空間
Accounting information:CPU使用量、時間用量、時限等等
I/O status information:配置給該Process的I/O裝置、開啟的檔案等
Switch From Process to Process
之前提過,為了看起來每個程式同時運作,OS會不同切換Process讓CPU不同運作。
在切換Process的同時,也必須將對應的PCB讀進來和存回去。而記憶體的速度比起CPU來說非常緩慢,因此PCB的讀入讀出其實很耗時。
Image Not Showing
Possible Reasons
The image file may be corrupted
The server hosting the image is unavailable
The image path is incorrect
The image format is not supported
Learn More →
這樣的Context Switch成本,受到了硬體速度的影響。有時候硬體會使用多個等級的暫存器來輔助這件事。
Thread
到目前為止我們都只考慮一個Process只有一個Thread(執行緒)。
想想看,如果一個Process有多個Program counter
…
多個執行點在同一個程式中,每個執行點就是一個Thread。
這樣的程式我們叫Multiple threads(多執行緒),會在後面的章節更詳細說明。
Process Scheduling
為了最大化CPU的使用率,快速的切換到對的Process到CPU核心中是重要的。
Process scheduler就是在決定下一個要執行哪個可用的Process。
我們會專注在兩個排程序列中:
Ready queue:已經準備好要執行的Process會在這個queue中,等候OS選擇到它執行。
Wait queue:在這裡的Process可能在等待某些事件發生(例如I/O裝置等)。
Process會在這些queue中移來移去。
Image Not Showing
Possible Reasons
The image file may be corrupted
The server hosting the image is unavailable
The image path is incorrect
The image format is not supported
Learn More →
queue的實作可能是linked list(上圖),會根據不同的OS有不同的實作方法。
Data的部分就是PCB,排程時可能也會參考這些資訊來決定執行順序。
Type of schedulers
Long-term/ job scheduler
選擇哪些Processes要進來ready queue準備執行。
執行頻率不高,通常幾秒、幾分鐘再執行就好。
速度不快。
Short-term/ CPU scheduler
從ready queue選擇process執行。
有些系統只有Short-term,省去Long-term的步驟。
執行頻率快速,通常單位是毫秒。
必須非常快速。
Medium-term scheduler
當記憶體不夠用,必須將部分Process從記憶體中移開,在真正需要時再移回來。
Processes中可能分為兩類:
I/O-bound process:如果Process需要等後I/O輸入,那它很有可能馬上就要進入wait狀態,也就是說它的CPU bursts很低。
CPU-bound process:工作為大量的計算,CPU可以全力運算,CPU bursts很高。
要如何在排程階段分配好兩者的比例是Long-term scheduler的重點。
Operations on Processes
Process Creation
要創造出一個新的Process,我們使用**fork()**的方式。
原來的Process複製出一份一樣的程式架構,並將該複製體設定為自己的小孩(Tree架構)。
子程式再根據需求改變內容,變為不同程式。
管理上使用pid(process identifier)來辨識不同程式。
Process Termination
使用
exit
來結束一個Process。
Process的資源deallocated是OS的重要工作。
如果擁有子Process,父Process必須wait所有小孩結束。
也可以用
abort
強制結束父Process。
小孩的資源可能過多了
小孩的工作已經不需要執行
當父Process強制結束,所有小孩也會跟著中止。
如果沒有正常結束,該process會變成一個
zombie
。
Interprocess Communication
系統中的Process可以是independent或是cooperating。
Cooperating Process可以影響其他Process或被其他Process影響,也包含共享資料這件事。
Cooperating Process的好處:
資料共享
計算速度提升
模組化
方便化
Cooperating Process必須做到interprocess communication(IPC)的架構,而IPC分為兩種:
Shared memory
Image Not Showing
Possible Reasons
The image file may be corrupted
The server hosting the image is unavailable
The image path is incorrect
The image format is not supported
Learn More →
Message passing
Image Not Showing
Possible Reasons
The image file may be corrupted
The server hosting the image is unavailable
The image path is incorrect
The image format is not supported
Learn More →
在共享記憶體時可能會有同步化的問題,我們在後面Synchronization會詳細討論。
tags:
note
Operating System #3
Program VS Process
Process State
Process Control Block (PCB)
Switch From Process to Process
Thread
Process Scheduling
Type of schedulers
Operations on Processes
Process Creation
Process Termination
Interprocess Communication
Expand all
Back to top
Go to bottom
Operating System #3
Program VS Process
Process State
Process Control Block (PCB)
Switch From Process to Process
Thread
Process Scheduling
Type of schedulers
Operations on Processes
Process Creation
Process Termination
Interprocess Communication
Expand all
Back to top
Go to bottom
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up
Comment