Operating System Concepts Chapter 5 Process Scheduling 1

  • Slides: 26
Download presentation
Operating System Concepts 作業系統原理 Chapter 5 行程排班 (Process Scheduling) 1

Operating System Concepts 作業系統原理 Chapter 5 行程排班 (Process Scheduling) 1

5. 3 排班演算法(Scheduling Algorithms) n 5. 3. 1 先來先做之排班方法(FCFS: First-Come, First-Served ) q 把CPU分配給第一個要求CPU的行程。FCFS策略的製作很容易用

5. 3 排班演算法(Scheduling Algorithms) n 5. 3. 1 先來先做之排班方法(FCFS: First-Come, First-Served ) q 把CPU分配給第一個要求CPU的行程。FCFS策略的製作很容易用 Fl. FO佇列管理。當一個行程進入就緒佇列之後,它的行程控制區段 就鏈接到串列的尾端。 FCFS: Nonpreemptive What’s the average waiting time? Convoy effect (護送現象) short process behind long process 9

n SJF is optimal – gives minimum average waiting time for a given set

n SJF is optimal – gives minimum average waiting time for a given set of processes q The difficulty is knowing the length of the next CPU request Determining Length of Next CPU Burst n n Can only estimate the length Can be done by using the length of previous CPU bursts, using exponential averaging

Examples of Exponential Averaging n =0 q q n =1 q q n n+1

Examples of Exponential Averaging n =0 q q n =1 q q n n+1 = n Recent history does not count n+1 = tn Only the actual last CPU burst counts If we expand the formula, we get: n+1 = tn+(1 - ) tn-1 + …+(1 - )j tn-j + …+(1 - )n +1 0 n Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor

Prediction of the Length of the Next CPU Burst 0=10 =1/2

Prediction of the Length of the Next CPU Burst 0=10 =1/2

14

14

n 5. 3. 3 優先權排班方式 (Priority Scheduling) q 每一個行程都有它的優先順序。CPU 將分配給具有最高優先權的行 程,具有相同優先順序的行程則按照 FCFS 來排班。優先權一般是 一些固定範圍的數字,譬如

n 5. 3. 3 優先權排班方式 (Priority Scheduling) q 每一個行程都有它的優先順序。CPU 將分配給具有最高優先權的行 程,具有相同優先順序的行程則按照 FCFS 來排班。優先權一般是 一些固定範圍的數字,譬如 0 到 7 或 0 至 4095。 Process Burst Time P 1 10 P 2 1 P 3 2 P 4 1 P 5 5 P 2 0 P 5 1 Priority 3 1 4 5 2 P 3 P 4 16 18 19 6 Average waiting time = (6+0+16+18+1)/5 = 8. 2 Problem Indefinite Blocking (無限期阻塞) or Starvation (饑餓) – low priority processes may never execute Solution Aging (老化) – as time progresses increase the priority of the process 15

Round Robin (RR) n Each process gets a small unit of CPU time (time

Round Robin (RR) n Each process gets a small unit of CPU time (time quantum), usually 10 -100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. n If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units. n Performance q q q large FIFO q small q must be large with respect to context switch, otherwise overhead is too high

(time quantum) (switching context) 18

(time quantum) (switching context) 18

time quantum = 5 P 1(5) – p 2(3) – p 3(1) – p

time quantum = 5 P 1(5) – p 2(3) – p 3(1) – p 4(5) – p 1(1) –p 4(2) Turnaround Time = 15 + 8 + 9 + 7 = 49 time quantum = 6 P 1(6) – p 2(3) – p 3(1) – p 4(6) – p 4(1) Turnaround T = 6 + 9 + 10 + 17 = 42 19

Example of Multilevel Feedback Queue n Three queues: q q q n Q 0

Example of Multilevel Feedback Queue n Three queues: q q q n Q 0 – RR with time quantum 8 milliseconds Q 1 – RR time quantum 16 milliseconds Q 2 – FCFS Scheduling q q A new job enters queue Q 0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q 1. At Q 1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q 2.

24

24