
84d8eb5aee6506de1b210819ae5d006a.ppt
- Количество слайдов: 42
Operating Systems Certificate Program in Software Development CSE-TC and CSIM, AIT September -- November, 2003 7. CPU Scheduling (Ch. 5, S&G) ch 6 in the 6 th ed. v Objectives – examine and compare some of the common CPU scheduling algorithms OSes: 7. CPU Scheduling 1
Contents 1. CPU Scheduling – what it is, burst cycles, criteria 2. Scheduling Algorithms – FCFS, SJF, Priority, RR, multilevel 3. Algorithm Evaluation – deterministic, queueing, simulation OSes: 7. CPU Scheduling 2
1. CPU Scheduling v One of the main aims of an OS is to maximise the utilization of the CPU by switching it between processes – when should the switching occur? – which processes should be executed next? OSes: 7. CPU Scheduling 3
1. 1. What is a Process? dispatch new ready event completed OSes: 7. CPU Scheduling Fig. 4. 1, p. 90 running exit interrupt terminated waiting I/O event or wait 4
1. 2. Scheduling Opportunities v When the running process yields the CPU: – the process enters a wait state – the process terminates v When an interrupt occurs: – the current process is still ready – process switches from wait state to ready v Preemptive vs. non-preemptive scheduling OSes: 7. CPU Scheduling 5
1. 3. CPU and I/O Burst Cycle Fig. 5. 1, p. 124 : load store add store read from file wait for I/O store increment index write to file wait for I/O load store : OSes: 7. CPU Scheduling CPU burst I/O burst CPU burst 6
CPU Burst Duration (ms) frequency Fig. 5. 2, p. 125; VUW CS 305 160 140 120 100 80 60 40 20 0 0 OSes: 7. CPU Scheduling 2 4 8 16 24 32 40 7
1. 4. Scheduling Criteria v CPU utilization – keep the CPU as busy as possible v Throughput – no. of processes completed per time unit v Turnaround time – how long it takes to complete a process OSes: 7. CPU Scheduling continued 8
v Waiting time – the total time a process is in the ready queue – the measure used in chapter 5 v Response time – time a process takes to start responding OSes: 7. CPU Scheduling 9
2. Scheduling Algorithms 2. 1. First-Come, First-Served (FCFS) 2. 2. Shortest Job First (SJF) 2. 3. Priority Scheduling 2. 4. Round Robin (RR) 2. 5. Multilevel Queue Scheduling 2. 6. Multilevel Feedback Queue Scheduling OSes: 7. CPU Scheduling 10
2. 1. FCFS Scheduling v When the CPU is available, assign it to the process at the start of the ready queue. v Simple to implement – use a FIFO queue v Non-preemptive OSes: 7. CPU Scheduling 11
Example (v. 1) p. 129 v All processes arrive at time 0. v Process Burst Time P 1 P 2 P 3 v Gantt Chart: 24 3 3 P 1 0 P 2 24 27 P 3 30 v Average waiting time: (0 + 24 + 27)/3 = 17 ms OSes: 7. CPU Scheduling 12
Example (v. 2) v Process Burst Time P 2 P 3 P 1 3 3 24 v Gantt Chart: P 2 0 P 3 3 P 1 6 30 v Average waiting time: (6 + 0 + 3)/3 = 3 ms OSes: 7. CPU Scheduling 13
FCFS Features v May not give the best average waiting time. v Average times can vary a lot depending on the order of the processes. v Convoy effect – small processes can get stuck behind a big process OSes: 7. CPU Scheduling 14
2. 2. Shortest Job First Scheduling (SJF) v When the CPU is available, assign it to the process with the smallest next CPU burst duration – better name is “shortest next CPU burst” v Can be preemptive or non-preemptive OSes: 7. CPU Scheduling 15
Non-preemptive Example v Process p. 131 Burst Time P 1 P 2 P 3 P 4 6 8 7 3 v Gantt Chart: P 4 0 P 1 3 P 3 9 P 2 16 24 v Average waiting time: (3 + 16 + 9 + 0)/4 = 7 ms – FCFS gives 10. 25 ms OSes: 7. CPU Scheduling 16
SJF Features v Provably optimal – gives the minimum average waiting time v Problem: it is usually impossible to know the next CPU burst duration for a process – solution: guess (predict) OSes: 7. CPU Scheduling 17
Predicting the next CPU burst time v Use the formula: Tn+1 = w tn + (1 - w) Tn v Meaning: – Tn+1 = prediction for the next (n+1 th) CPU burst duration – tn = known duration of current (nth) CPU burst – Tn = previous prediction (based on the sequence of old ti times) – w = a weight (0 <= w <= 1); usually w = 1/2 OSes: 7. CPU Scheduling 18
Preemptive SJF v When a new process arrives, if it has a shorter next CPU burst duration than what is left of the currently executing process then preempt the current process. OSes: 7. CPU Scheduling 19
Example Process v p. 133 Arrival Time Burst Time 0 1 2 3 8 4 9 5 P 1 P 2 P 3 P 4 v Gantt Chart: P 1 P 2 0 OSes: 7. CPU Scheduling 1 P 4 5 P 1 10 P 3 17 26 continued 20
v Average waiting time: ( (10 -1) + (17 -2) + (5 -3) )/4 = 6. 5 ms start time arrival time v Non-preemptive SJF gives 7. 75 ms OSes: 7. CPU Scheduling 21
2. 3. Priority Scheduling v Associate a priority with each process and the CPU is allocated to the process with the highest priority. v FCFS, SJF are special cases. v Low numbers = high priority. OSes: 7. CPU Scheduling 22
Example p. 134 v Process Burst Time P 1 P 2 P 3 P 4 P 5 10 1 2 1 5 v Gantt Chart: P 2 P 5 0 Priority 1 3 4 2 P 1 6 P 3 16 P 4 18 19 v Average waiting time: 8. 2 ms OSes: 7. CPU Scheduling 23
Features v Internal/external priorities. v Preemptive or non-preemptive. v How to avoid starvation? – aging OSes: 7. CPU Scheduling 24
2. 4. Round Robin Scheduling (RR) v A small unit of time (a time quantum, a time slice) is defined – typically 10 - 100 ms v The ready queue is treated as a circular queue. v The CPU scheduler goes around the queue giving each process one time quantum – preemptive OSes: 7. CPU Scheduling 25
Example p. 135 v Time quantum = 4 ms. At time 0. v Process Burst Time P 1 P 2 P 3 24 3 3 v Gantt Chart: P 1 P 2 0 4 P 3 7 P 1 10 P 1 14 P 1 18 P 1 22 P 1 26 30 v Average waiting time: 17/3 = 5. 67 ms OSes: 7. CPU Scheduling 26
RR Features v Average waiting time can be quite long. v Context switching is an important overhead when the time quantum is small. OSes: 7. CPU Scheduling continued 27
Fig. 5. 5, p. 137 v Average turnaround time of a set of processes does not necessarily improve as the time quantum size increase: Average Turnaround Time 12. 5 12 Process Time P 1 6 P 2 3 P 3 1 P 4 7 11. 5 11 10. 5 10 9. 5 1 2 3 4 5 6 7 Time Quantum OSes: 7. CPU Scheduling 28
2. 5. Multilevel Queue Scheduling Fig. 5. 6, p. 138 highest priority system processes interactive editing processes batch processes lowest priority OSes: 7. CPU Scheduling student processes continued 29
v Scheduling between queues: – fixed priority preemptive scheduling – varying time slices between the queues OSes: 7. CPU Scheduling 30
2. 6. Multilevel Feedback Queue Scheduling v Allow a process to move between queues – priority sinks when quantum exceeded – greater discrimination against longer jobs – better response for shorter jobs OSes: 7. CPU Scheduling Fig. 5. 7, p. 140; VUW CS 305 Q 0 Q 1 > Q 0 Q 2 > Q 1 FCFS 31
3. Algorithm Evaluation v 3. 1. Deterministic Modelling v 3. 2. Queueing Models v 3. 3. Simulation OSes: 7. CPU Scheduling 32
Common Issues v Model/simulation is based on some pattern of use of a real machine – e. g. student’s use of bazooka: lots of interactive, small jobs, and a few large ones v How to represent the changes in usage: – changes in problems, programs, users OSes: 7. CPU Scheduling 33
3. 1. Deterministic Modelling v Take a given workload and calculate the performance of each scheduling algorithm: – FCFS, SJF, and RR (quantum = 10 ms) OSes: 7. CPU Scheduling 34
Example p. 145 v At time 0. v Process P 1 P 2 P 3 P 4 P 5 OSes: 7. CPU Scheduling Burst Time 10 29 3 7 12 continued 35
Gantt Charts and Times v 1. FCFS: P 1 0 P 2 10 P 3 39 P 4 42 P 5 49 61 v Average waiting time: (0 + 10 + 39 + 42 + 49)/5 = 28 ms OSes: 7. CPU Scheduling continued 36
v 2. Non-preemptive SJF: P 3 0 P 4 3 P 1 10 P 5 20 P 2 32 61 v Average waiting time: (10 + 32 + 0 + 3 + 20)/5 = 13 ms OSes: 7. CPU Scheduling continued 37
v 3. RR: P 1 0 P 2 10 P 3 P 4 20 23 P 5 30 P 2 40 P 5 P 2 50 52 61 v Average waiting time: (0 + 32 + 20 + 23 + 40)/5 = 23 ms OSes: 7. CPU Scheduling 38
Results v For this mix of processes and CPU burst durations: – SJF 13 ms – RR 23 ms – FCFS 28 ms OSes: 7. CPU Scheduling 39
Deterministic Modelling Features v Simple and fast to calculate. v Requires exact numbers. v Limited generality, but with enough cases it may reveal some trends. OSes: 7. CPU Scheduling 40
3. 2. Queueing Models v Usually the process mix varies greatly in an OS, but it may still be possible to determine statistical distributions for the CPU and I/O bursts. v The mathematics is difficult, and only applies to limited cases. OSes: 7. CPU Scheduling 41
3. 3. Simulations v Often driven by random number generators to model CPU burst durations, process arrival, departure, etc. v Trace tapes – records of actual events in a real system, which can be used to drive simulations OSes: 7. CPU Scheduling 42