# [作業系統] Ch4 The Process ## The Concept of Process - Process 就是**正在進行中的 program** - Process v.s. program - **Program**:**靜態**存放在 Disk 中 - **Process**:**作業系統**從 disk 中把 program load 下來,並執行 ## CPU Virtualization 透過 **Time Sharing** 技術讓 CPU 在多個 process 中快速切換 - **Context Switch**:切換不同 process - **CPU Scheduling**:決定process 運行的順序 ## Machine state 一個 process 在任意時刻所能讀取或更新的所有系統資源與資訊 ### Memory - **code**:存放 instructions 的地方 - **static data**:存放已初始化的 global variable - **heap**:用於**動態記憶體**分配 - `malloc`, `free`... - **stack**:function 呼叫、參數、回傳 address 與 local variable ![image](https://hackmd.io/_uploads/SkeXPADh1x.png) ### Registers - **General Registers**:用來儲存暫存值 - **Program counter(PC)/Instruction Pointer(IP)**:指向**下一個將被執行 instruction** 的 address - **Stack pointer(SP): - 指向 **stack** 的頂端 - 用於函數調用管理 ### I/O state - 指現在開啟的 **I/O 文件** ## Process API :::info 實際的 system call 操作會在[第五章](https://hackmd.io/@Toast1001/SJJpa-Shye) ::: - **Create**: - 創建 process - e.g. `fork()`, `exec()`... - **Destroy**: - 終止 process - e.g. `kill()`, `exit()`... - **Wait**: - Delay process - e.g. `wait()`, `sleep()`... - **暫停與恢復**: - e.g. `pause()`, `resume()`... - **查詢狀態**: - e.g. `ps`, `top`... ## Process States - **Ready**:可以執行,但 CPU 忙碌中 - **Running**:正在使用 CPU - **Blocked**:使用完成 CPU,離開 CPU ![image](https://hackmd.io/_uploads/r1TONyOhkx.png) ## Process Data Structures ### Process Control Block(PCB) - **定義**:作業系統為每個 Process 維護的一個 Data Structure - 內部結構(重要欄位): - **PID**:Process ID - **Process state**:紀錄 process 的狀態 - **Priority**:優先序,與 CPU Sceduling 有關 - **Parent Process/Child Process**:追蹤 parent process 與 child process ### Process List - 每一個 Queue 存放著每個 process 的 **PCB** - **Ready Queue**:存放所有 Ready process - **Blocked Queue**:存放所有 Blocked process - **Running Process**:存放所有 Running process ## Context switch :::warning 第六章有更多的講解 ::: - 當作業系統決定讓另一個 process 運行時,它需要**先 interrupt 當前 process** 並切換到新的 process - 作業系統的操作: - 當前 process 的 process state 存入 PCB - 載入新 process 的 PCB ![image](https://hackmd.io/_uploads/rJr3EJ_hkl.png)