420 likes | 621 Views
Operating Systems. Certificate Program in Software Development CSE-TC and CSIM, AIT September -- November, 2003. Objectives examine and compare some of the common CPU scheduling algorithms. 7. CPU Scheduling (Ch. 5, S&G). ch 6 in the 6th ed. Contents. 1. CPU Scheduling
E N D
Operating Systems Certificate Program in Software DevelopmentCSE-TC and CSIM, AITSeptember -- November, 2003 • Objectives • examine and compare some of the common CPU scheduling algorithms 7. CPU Scheduling(Ch. 5, S&G) ch 6 in the 6th ed.
Contents 1. CPU Scheduling • what it is, burst cycles, criteria 2. Scheduling Algorithms • FCFS, SJF, Priority, RR, multilevel 3. Algorithm Evaluation • deterministic, queueing, simulation
1. CPU Scheduling • 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?
new terminated 1.1. What is a Process? Fig. 4.1, p.90 dispatch running exit ready interrupt eventcompleted waiting I/O eventor wait
1.2. Scheduling Opportunities • When the running process yields the CPU: • the process enters a wait state • the process terminates • When an interrupt occurs: • the current process is still ready • process switches from wait state to ready • Preemptive vs. non-preemptive scheduling
1.3. CPU and I/O Burst Cycle Fig. 5.1, p.124 :loadstoreaddstoreread from file wait for I/Ostoreincrement indexwrite to file wait for I/Oloadstore : CPU burst I/O burst CPU burst I/O burst CPU burst
160 140 120 100 80 60 frequency 40 20 0 0 2 4 8 16 24 32 40 CPU Burst Duration (ms) Fig. 5.2, p.125;VUW CS 305
1.4. Scheduling Criteria • CPU utilization • keep the CPU as busy as possible • Throughput • no. of processes completed per time unit • Turnaround time • how long it takes to complete a process continued
Waiting time • the total time a process is in the ready queue • the measure used in chapter 5 • Response time • time a process takes to start responding
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
2.1. FCFS Scheduling • When the CPU is available, assign it to the process at the start of the ready queue. • Simple to implement • use a FIFO queue • Non-preemptive
Example (v.1) p.129 • All processes arrive at time 0. • Process Burst Time P1 24 P2 3 P3 3 • Gantt Chart: • Average waiting time: (0 + 24 + 27)/3 = 17 ms P1 P2 P3 0 24 27 30
Example (v.2) • Process Burst Time P2 3 P3 3 P1 24 • Gantt Chart: • Average waiting time: (6 + 0 + 3)/3 = 3 ms P2 P3 P1 0 3 6 30
FCFS Features • May not give the best average waiting time. • Average times can vary a lot depending on the order of the processes. • Convoy effect • small processes can get stuck behind a big process
2.2. Shortest Job First Scheduling (SJF) • When the CPU is available, assign it to the process with the smallest next CPU burst duration • better name is “shortest next CPU burst” • Can be preemptive or non-preemptive
Non-preemptive Example p.131 • Process Burst Time P1 6 P2 8 P3 7 P4 3 • Gantt Chart: • Average waiting time: (3 + 16 + 9 + 0)/4 = 7 ms • FCFS gives 10.25 ms P4 P1 P3 P2 0 3 9 16 24
SJF Features • Provably optimal • gives the minimum average waiting time • Problem: it is usually impossible to know the next CPU burst duration for a process • solution: guess (predict)
Predicting the next CPU burst time • Use the formula: Tn+1 = w tn + (1- w) Tn • Meaning: • Tn+1 = prediction for the next (n+1th) 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
Preemptive SJF • 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.
Example p.133 • Process Arrival Time Burst Time P1 0 8 P2 1 4 P3 2 9 P4 3 5 • Gantt Chart: P1 P2 P4 P1 P3 0 1 5 10 17 26 continued
Average waiting time: ( (10-1) + (1-1) + (17-2) + (5-3) )/4 = 6.5 ms • Non-preemptive SJF gives 7.75 ms start time arrival time
2.3. Priority Scheduling • Associate a priority with each process and the CPU is allocated to the process with the highest priority. • FCFS, SJF are special cases. • Low numbers = high priority.
Example p.134 • Process Burst Time Priority P1 10 3 P2 1 1 P3 2 3 P4 1 4 P5 5 2 • Gantt Chart: • Average waiting time: 8.2 ms P2 P5 P1 P3 P4 0 1 6 16 18 19
Features • Internal/external priorities. • Preemptive or non-preemptive. • How to avoid starvation? • aging
2.4. Round Robin Scheduling (RR) • A small unit of time (a time quantum, a time slice) is defined • typically 10 - 100 ms • The ready queue is treated as a circular queue. • The CPU scheduler goes around the queue giving each process one time quantum • preemptive
Example p.135 • Time quantum = 4 ms. At time 0. • Process Burst Time P1 24 P2 3 P3 3 • Gantt Chart: • Average waiting time: 17/3 = 5.67 ms P1 P2 P3 P1 P1 P1 P1 P1 0 4 7 10 14 18 22 26 30
RR Features • Average waiting time can be quite long. • Context switching is an important overhead when the time quantum is small. continued
Fig. 5.5, p.137 • Average turnaround time of a set of processes does not necessarily improve as the time quantum size increase: 12.5 12 Process Time P1 6 P2 3 P3 1 P4 7 11.5 AverageTurnaround Time 11 10.5 10 9.5 1 2 3 4 5 6 7 Time Quantum
2.5. Multilevel Queue Scheduling Fig. 5.6, p.138 highest priority system processes interactive processes interactive editing processes batch processes student processes lowest priority continued
Scheduling between queues: • fixed priority preemptive scheduling • varying time slices between the queues
2.6. Multilevel Feedback Queue Scheduling Fig. 5.7, p.140;VUW CS 305 • Allow a process to move between queues • priority sinks when quantum exceeded • greater discrimination against longer jobs • better response for shorter jobs Q0 Q1 > Q0 Q2 > Q1 FCFS
3. Algorithm Evaluation • 3.1. Deterministic Modelling • 3.2. Queueing Models • 3.3. Simulation
Common Issues • 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 • How to represent the changes in usage: • changes in problems, programs, users
3.1. Deterministic Modelling • Take a given workload and calculate the performance of each scheduling algorithm: • FCFS, SJF, and RR (quantum = 10 ms)
Example p.145 • At time 0. • Process Burst Time P1 10 P2 29 P3 3 P4 7 P5 12 continued
Gantt Charts and Times • 1. FCFS: • Average waiting time: (0 + 10 + 39 + 42 + 49)/5 = 28 ms P1 P2 P3 P4 P5 0 10 39 42 49 61 continued
2. Non-preemptive SJF: • Average waiting time: (10 + 32 + 0 + 3 + 20)/5 = 13 ms P3 P4 P1 P5 P2 0 3 10 20 32 61 continued
3. RR: • Average waiting time: (0 + 32 + 20 + 23 + 40)/5 = 23 ms P1 P2 P3 P4 P5 P2 P5 P2 0 10 20 23 30 40 50 52 61
Results • For this mix of processes and CPU burst durations: • SJF 13 ms • RR 23 ms • FCFS 28 ms
Deterministic Modelling Features • Simple and fast to calculate. • Requires exact numbers. • Limited generality, but with enough cases it may reveal some trends.
3.2. Queueing Models • 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. • The mathematics is difficult, and only applies to limited cases.
3.3. Simulations • Often driven by random number generators to model CPU burst durations, process arrival, departure, etc. • Trace tapes • records of actual events in a real system, which can be used to drive simulations