Outline Process Control Block PCB Process State Scheduling
Outline Process Control Block (PCB) Process State Scheduling Policy Completely Fair Scheduler (CFS) Multi-thread Linux Kernel
Process Control Block (1/2) Ø每個Process在OS中都有一個對應的PCB ØPCB記載Process的相關資訊包含: • • Process ID : 唯一的(Unique) Process State : Ready, Running, Wait Programming Counter : 放Process下一個要執行的指令位置 CPU Registers : Process所需的暫存器 CPU Scheduling Info : Process的優先權 Memory Management Info : Base, limit registers Accounting Info : 使用了多少CPU的時間、時間限制 I/O State : 未完成的I/O請求、分配給Process的Devices編號
Process Control Block (2/2)
Outline Process Control Block (PCB) Process State Scheduling Policy Completely Fair Scheduler (CFS) Multi-thread Linux Kernel
Process State ØProcess : 執行中的程式
Outline Process Control Block (PCB) Process State Scheduling Policy Completely Fair Scheduler (CFS) Multi-thread Linux Kernel
Scheduling Policy (1/2) Ø在Ready Queue中的process會互相爭奪CPU, 所以利用Scheduling Policy 來決定下一個被CPU執行的Process Ø在Linux中的scheduling policy又可分為兩大類 1. Real-time Policy 2. Normal Policy
Scheduling Policy (2/2) ØReal-time Policy • Real-time Policy的Process會優先被排程 • 優先權範圍為 1到 99 • Real-time Policy中分為兩種Policy 1. SCHED_FIFO 2. SCHED_RR ØNormal Policy • 優先權範圍為 100到 139 • Normal Policy中分為三種Policy 1. SCHED_OTHER 2. SCHED_BATCH 3. SCHED_IDLE
Real-time Policy(1/2) ØSCHED_FIFO (First In First Out ) • 擁有最高優先權的Process優先執行 • 持續執行此Process直到它自行釋出或遇到更高優先權的Process Priority Burst time P 1 2 3 P 2 1 15 P 3 3 2
Real-time Policy(2/2) ØSCHED_RR (Round Robin) • 擁有最高優先權的先執行 • 若有兩個優先權相同的Process,Kernel 會分配一個 time slice 去限制執行 時間 Process Priority Burst time P 1 1 15 P 2 1 10 Time slice = 5 ms
Normal Policy ØSCHED_OTHER • 每個Process都有time slice • 使用Completely Fair Scheduler (CFS)機制運行 ØSCHED_BATCH • 比 SCHED_OTHER 的優先權低 • 適合Batch Process,非急迫且與User互動性低的process ØSCHED_IDLE • 最低優先權 • 在System Idle時此process才會被執行
Scheduling Policy ØShort Job First (SJF) • 最短作業優先排班法 • 優先選擇具有最小CPU Burst Time的Process • 若遇到兩個Burst Time一樣的Process,則在使用FIFO Process Burst time P 1 6 P 2 8 P 3 7 P 4 3
Outline Process Control Block (PCB) Process State Scheduling Policy Completely Fair Scheduler (CFS) Multi-thread Linux Kernel
Completely Fair Scheduler (1/3) Ø由Ingo Molnar提出 Ø在Linux 2. 6. 23 (2007. 10) 之後,Linux Kernel採用了CFS機制 Ø解決某些Process因為優先權較低而始終沒被CPU處理到的問題 Ø導入了“Virtual Runtime”
Completely Fair Scheduler (2/2) ØVirtual Runtime (vruntime) • 每個Process都有自己的vruntime與Nice值 • 假設一process的執行時間為 t ns, 則每一輪排程時 Vruntime += t * nice • vrntime越小表示此process距離上一次被執行時間間隔最久 • 選擇vrntime最小的process為下一個CPU執行的Process ØNice • 用來調整Process的執行優先順序 • Nice範圍為 -20到 19,越小優先權越高
Completely Fair Scheduler (3/3) Ø例子 Round 1: Process Nice t P 1 -2 5 P 1: Vruntime = 5*(-2)=-10 P 2 -1 5 P 2: Vruntime = 5*(-1)=-5 P 3 -2 10 P 3: Vruntime = 10*(-2)=-20 Process Nice t P 1 -2 5 P 1: Vruntime = -10+5*(-2)=-20 P 2 -1 5 P 2: Vruntime = -5+5*(-1)=-10 Process Nice t P 2 -1 5 P 2: Vruntime = -10+5*(-1)=-15 P 4 -1 10 P 4: Vruntime = 10*(-1)=-10 Round 2: Round 3:
Outline Process Control Block (PCB) Process State Scheduling Policy Completely Fair Scheduler (CFS) Multi-thread Linux Kernel
Thread ØThread • • • 又稱Light Weight Process (LWP) CPU執行時的一個基本單位 由一個Thread ID、程式計數器、一組暫存器及一個Stack組成 一個Process可能包含多個Thread 同一個Process中的Thread彼此共享 1. Code section 2. Data section 3. OS Resources (Open Files, signals)
Multi-thread (1/4) ØThread又可分為User Level的User Thread與Kernel的Kernel Thread ØUser Thread • 由 User Level 的 Thread Library 來產生、排班、管理Thread ØKernel Thread • 由 OS來產生、排班、管理Thread
Multi-thread (2/4) ØMany to One • • 多個User Thread對應到一個Kernel Thread 由Thread Library管理 如果有一個Thread呼叫系統暫停, 則整個Process就會被暫停 一次只有一個Thread可以存取Kernel
Outline Process Control Block (PCB) Process State Scheduling Policy Completely Fair Scheduler (CFS) Multi-thread Linux Kernel
Linux Kernel kernel_init( ) start_kernel( ) rest_init( ) kthreadd init_post( )
Linux Kernel - kthreadd 負責所有Kernel Thread的創建、調度及管理 去查看kthread_create_list中是否有kernel thread 需要創建, 若有則使用create_kthread()創建, kthreadd( ) 接著用wake_up_process()喚醒這個task讓他運行 使用kthread_stop()來刪除kernel thread Check kthread_create_list create_kthread( ) Linux/kernel/kthred. c wake_up_proce ss( )
Linux Kernel - khelper 透過khelper來使Kernel Thread帶起User Mode的應用程式執行 首先去查看Work. Queue裡是否有user thread需要執行, 若有則透過call_usermodehelper_setup()設定這個user thread相關路徑及參數, 最後使用call_usermodehelper_exec()來達到執行user thread的目的 __init usermodehelper_init() Check khelper_wq call_usermodehelper_setup( ) Linux/kernel/kmod. c call_usermodehelper_exec( )
Linux Kernel - Code(1/2) Øcall_usermodehelper_setup( ) 設定預被執行的user thread其相關路徑及參數 Linux/kernel/kmod. c
Linux Kernel - Code(2/2) Øcall_usermodehelper_exec( ) 達到執行user thread的目的 Linux/kernel/kmod. c
- Slides: 29