# Chapter 6: CPU Scheduling

• Always want to have CPU working • Usually many processes in ready queue
– Ready to run on CPU – Consider a single CPU here

Preemptive vs. Non-preemptive Scheduling
• Non-preemptive scheduling
– A new process is selected to run either
• when a process terminates or • when an explicit system request causes a wait state (e.g., I/O or wait for child)

• Need strategies
– Selecting next process to run – For allocating CPU time – What happens after a process does a system call?

• Preemptive scheduling
– New process selected to run also when
• An interrupt occurs • When new processes become ready

• Short-term scheduling
– Must not take much CPU time

Performance Criteria
• CPU utilization
– Percentage of time that CPU is busy (and not idle), over some period of time

Scheduling Algorithms
• First-come, First-Served (FCFS)
– Complete the jobs in order of arrival

• Throughput
– Number of jobs completed per unit time

• Shortest Job First (SJF)
– Complete the job with shortest next CPU requirement (e.g., burst) – Provably optimal w.r.t. average waiting time

• Turnaround time
– Time interval from submission of a process until completion of the process

• Priority
– Processes have a priority number – Allocate CPU to process with highest priority

• Waiting time
– Sum of the time periods spent in the ready queue

• Response time
– Time from submission until first output/input – May approximate by time from submission until first access to CPU

• Round-Robin (RR)
– Each process gets a small unit of time on CPU (time quantum or time slice) – For now, assume a FIFO queue of processes

FCFS: First-Come First-Served
• Implement with a FIFO ready queue • Major disadvantage can be long wait times • Example
– Draw Gantt chart – Compute the average wait time for processes with the following burst times and queue order:
• P1: 20, P2: 12, P3: 8, P4: 16, P5: 4

Solution: Gantt Chart Method
P1 20 P2 P3 32 40 P4 P5 56 60

• • • • • • •

Waiting times: P1: 0 P2: 20 P3: 32 P4: 40 P5: 56 Average wait time: 29.6

1

P4: 16. P2: 12.. requires future knowledge • May have an estimate. P3: 8.g. t = 6 What is estimate of next CPU burst? τ1 = 0..g.SJF: Shortest Job First • The job with the shortest next CPU burst time is selected • Example (from before): – CPU job burst times: • P1: 20. and τn is the last estimate Example Estimate • • • • Say. to predict next CPU burst – E. First-Served) – Non-preemptive • FCFS as priority: all have equal priorities • SJF as priority: priority is reciprocal of predicted CPU burst • Priorities can be – Internal • according to O/S factors (e. α is a constant indicating how much to base estimate on last CPU burst.g. α = 0. base on last CPU burst and a number summarizing history of CPU bursts τn+1 = α * t + (1 .g. P5: 4 P5 P3 4 12 SJF Solution P2 24 P4 40 P1 60 – Draw Gantt chart and compute the average waiting time given SJF CPU scheduling • • • • • • • Waiting times: P1: 40 P2: 12 P3: 4 P4: 24 P5: 0 Average wait time: 16 SJF • Provably shortest average wait time • However. as a function of amount of CPU usage.α) * τn – Where t is the last CPU burst value. User importance – Static: fixed for the duration of the process – Dynamic • Changing during processing • E..5 * 10 = 8 Priority Scheduling • Have to decide on a numbering scheme – 0 can be highest or lowest Which Scheduling Algorithms Can be Preemptive? • FCFS (First-come.. or length of time waiting (a solution to indefinite blocking or starvation) • Priority – Can be either – Choice when a processes priority changes or when a higher priority process arrives 2 .5 τ0 = 10 CPU burst. memory requirements) • SJF (Shortest Job First) – Can be either – Choice when a new job arrives – Can preempt or not – External: e.5 * 6 + 0.

6 Assume processes submitted at same time 3 . P3: 8. and compute average wait time • • • • • • • Waiting times: P1: 16 + 12 + 8 + 4 = 40 P2: 4 + 16 + 12 = 32 P3: 8 + 16 = 24 P4: 12 + 16 + 8 = 36 P5: 16 Average wait time: 29.t. P2: 12.6 Calculate Other Measurements • Response time – Estimate by time from job submission to time to first CPU dispatch – Assume all jobs submitted at same time.8 Performance Characterstics of Scheduling Algorithms • Different algorithms will have different performance characteristics • RR (Round Robin) – Good average response time • Important for interactive or timesharing systems • SJF – Best average waiting time – Some overhead w. quantum) of execution on CPU • Then move to next process • Continue until all processes completed • Example – CPU job burst times & order in queue • P1: 20. estimates of CPU burst length Average 41.6 Turnaround Time Calculations Job P1 P2 P3 P4 P5 FCFS 20 32 40 56 60 SJF 60 24 12 40 4 28 RR 60 44 32 48 20 40. in order given Response Time Calculations Job P1 P2 P3 P4 P5 FCFS 0 20 32 40 56 SJF 40 12 4 24 0 16 RR 0 4 8 12 16 8 • Turnaround time – Time interval from submission of a process until completion of the process FCFS P1 20 SJF P5 P3 4 RR 4 8 P2 P3 32 40 P4 P4 56 P1 P5 60 P2 12 12 16 20 24 24 28 32 36 40 40 44 48 52 56 60 60 P1 P2 P3 P4 P5 P1 P2 P3 P4 P1 P2 P4 P1 P4 P1 Average 29. P5: 4 P1 P2 P3 P4 P5 P1 P2 P3 P4 P1 P2 P4 P1 P4 P1 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 – Draw Gantt chart.RR (Round Robin) Scheduling completes Solution completes completes completes completes • Give each process a unit of time (time slice. P4: 16.r.