Cpu Scheduling: CIS 505: Software Systems
Cpu Scheduling: CIS 505: Software Systems
Cpu Scheduling: CIS 505: Software Systems
Overview of topics
o Issues in scheduling
o Basic scheduling algorithms
First-come First-served
Insup Lee Round Robin
Shortest Job First
Department of Computer and Information Science
Priority based
University of Pennsylvania o Scheduling in Unix
o Real-time scheduling (Priority Inheritance)
CIS 505, Spring 2007
CIS 505, Spring 2007 CPU Scheduling 3 CIS 505, Spring 2007 CPU Scheduling 4
1
Example of FCFS SJF - Shortest Job First
Non-preemptive
Workload (Batch system)
Ready queue treated as a priority queue based on smallest CPU-
Job 1: 24 units, Job 2: 3 units, Job 3: 3 units time requirement
• arriving jobs inserted at proper position in queue
FCFS schedule: • dispatcher selects shortest job (1st in queue) and runs to completion
| Job 1 | Job 2 | Job 3 |
0 24 27 30 Advantages: provably optimal w.r.t. average turnaround time
Disadvantages: in general, cannot be implemented. Also,
starvation possible!
Total waiting time: 0 + 24 + 27 = 51 Can do it approximately: use exponential averaging to predict
Average waiting time: 51/3 = 17 length of next CPU burst
==> pick shortest predicted burst next!
Total turnaround time: 24 + 27 + 30 = 81
Average turnaround time: 81/3 = 27
CIS 505, Spring 2007 CPU Scheduling 7 CIS 505, Spring 2007 CPU Scheduling 8
CIS 505, Spring 2007 CPU Scheduling 11 CIS 505, Spring 2007 CPU Scheduling 12
2
Properties of RR HPF - Highest Priority First
CIS 505, Spring 2007 CPU Scheduling 13 CIS 505, Spring 2007 CPU Scheduling 14
CIS 505, Spring 2007 CPU Scheduling 15 CIS 505, Spring 2007 CPU Scheduling 16
CIS 505, Spring 2007 CPU Scheduling 17 CIS 505, Spring 2007 CPU Scheduling 18
3
Unix CPU Scheduler Example (exercise)
Suppose p_nice is 0, clock ticks every 10msec, time quantum is 100msec, and
Two values in the PCB p_cpu adjustment every sec
o p_cpu: an estimate of the recent CPU use Suppose initial base value is 4. Initially, p_cpu is 0
o p_nice: a user/OS settable weighting factor (-20..20) for flexibility; Initial priority is 4.
default = 0; negative increases priority; positive decreases priority Suppose scheduler selects this process at some point, and it uses all of its
A process' priority calculated periodically quantum without blocking. Then, p_cpu will be 10, priority recalculated to 10, as
new base is 0.
priority = base + p_cpu + p_nice At the end of a second, p_cpu, as well as priority, becomes 5 (more likely to
and the process is moved to appropriate ready queue scheduled)
CPU utilization, p_cpu, is incremented each time the system clock Suppose again scheduler picks this process, and it blocks (say, for disk read)
after 30 msec. p_cpu is 8
ticks and the process is found to be executing.
Process is now in waiting queue for disk transfer
p_cpu is adjusted once every second (time decay) At the end of next second, p_cpu is updated to 4
o Possible adjustment: divide by 2 (that is, shift right) When disk transfer is complete, disk interrupt handler computes priority using a
negative base value, say, -10. New priority is -6
o Motivation: Recent usage penalizes more than past usage
Process again gets scheduled, and runs for its entire time quantum. p_cpu will
o Precise details differ in different versions (e.g. 4.3 BSD uses current be updated to 14
load (number of ready processes) also in the adjustment formula)
CIS 505, Spring 2007 CPU Scheduling 19 CIS 505, Spring 2007 CPU Scheduling 20