CPU Scheduling
Tanenbaum Ch 2.4
Silberchatz and Galvin Ch 5
Basic Concepts
• CPU - I/O Burst Cycle
• CPU Burst Distribution
160
140
120
100
80
60
40
20
0
cs431-cotter
0
8
16
24
32
2
CPU Scheduling
• Decision points:
–
–
–
–
Process switch from running to waiting state
Process switch from running to ready state
Process switch from waiting to ready state
Process terminates
• Scheduling types
– non-preemptive
– preemptive
cs431-cotter
3
Task Dispatcher
• Functions:
– Switching context
– Switching to user mode
– Jumping to PC location
• The time needed to stop one process and start
up another is known as dispatch latency.
cs431-cotter
4
Categories of Scheduling Algorithms



Batch
Interactive
Real time
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
5
Scheduling Algorithm Goals
Figure 2-39. Some goals of the scheduling algorithm under
different circumstances.
cs431-cotter
6
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Scheduling in Batch Systems



First-come first-served
Shortest job first
Shortest remaining Time next
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
7
Scheduling Algorithms
(First-come, First-served)
• Determine avg waiting time if:
– (P1, P2, P3) or (P3, P2, P1)
Process
Burst Time
P1
P2
P3
24
3
3
0
cs431-cotter
10
20
30
8
Scheduling Algorithms
(First-come, First-served)
• Determine avg waiting time if:
– (P1, P2, P3)
Process
Burst Time
P1
P2
P3
24
3
3
0
10
Waiting time:
P1: 0
P2: 24
P3: 27
avg: 17
20
30
P1 ……………………………………………..P2…..P3…….
cs431-cotter
9
Scheduling Algorithms
(First-come, First-served)
• Determine avg waiting time if:
–
(P3, P2, P1)
Process
Burst Time
P1
P2
P3
24
3
3
0
10
Waiting time:
P1: 6
P2: 3
P3: 0
avg: 3
20
30
P3….P2….P1………………………………………………...
cs431-cotter
10
Scheduling Algorithms
(Shortest Job First)
• Schedule jobs based on their length.
• May be preemptive
– If a job is queued that is shorter than the remaining
time on the current job, there is a switch.
• May be non-preemptive.
– Once a job has started, it works to its normal switch.
cs431-cotter
11
Scheduling Algorithms
(Shortest Job First)
• Non-Preemptive / preemptive SJF
Process
P1
P2
P3
P4
0
cs431-cotter
Arr. Time Burst Time
0.0
7
2.0
4
4.0
1
5.0
4
10
20
30
12
Scheduling Algorithms
(Shortest Job First)
• Preemptive SJF
Process
P1
P2
P3
P4
Arr. Time Burst Time
0.0
7
2.0
4
4.0
1
5.0
4
P1
|
0
10
20
30
P1..
cs431-cotter
13
Scheduling Algorithms
(Shortest Job First)
• Preemptive SJF
Process
P1
P2
P3
P4
P1
|
Arr. Time Burst Time
0.0
7
2.0
4
4.0
1
5.0
4
P2
|
0
10
20
30
P1..P2..
cs431-cotter
14
Scheduling Algorithms
(Shortest Job First)
• Preemptive SJF
Process
P1
P2
P3
P4
P1
|
P2
|
Arr. Time Burst Time
0.0
7
2.0
4
4.0
1
5.0
4
P3
|
0
10
20
30
P1..P2..P3
cs431-cotter
15
Scheduling Algorithms
(Shortest Job First)
• Preemptive SJF
Process
P1
P2
P3
P4
P1
|
P2
|
Arr. Time Burst Time
0.0
7
2.0
4
4.0
1
5.0
4
P3P4
| |
0
10
20
30
P1..P2..P3P2..
cs431-cotter
16
Scheduling Algorithms
(Shortest Job First)
• Preemptive SJF
Process
P1
P2
P3
P4
P1
|
P2
|
0
Arr. Time Burst Time
0.0
7
2.0
4
4.0
1
5.0
4
P3P4
| |
10
20
30
P1..P2..P3P2..P4…….
cs431-cotter
17
Scheduling Algorithms
(Shortest Job First)
• Preemptive SJF
Process
P1
P2
P3
P4
P1
|
P2
|
0
Arr. Time Burst Time
0.0
7
2.0
4
4.0
1
5.0
4
P3P4
| |
10
20
30
P1..P2..P3P2..P4…….P1……….
cs431-cotter
18
Scheduling Algorithms
(Shortest Job First)
•
Non-preemptive SJF
Process
P1
P2
P3
P4
P1
|
P2
|
0
Arr. Time Burst Time
0.0
7
2.0
4
4.0
1
5.0
4
P3P4
| |
10
20
30
P1…………..P3P2…….P4…….
cs431-cotter
19
Scheduling Algorithms
(Shortest Job First)
• Determining the length of the jobs.
• Use weighted average burst lengths
 n 1   * tn  (1   ) * n
0  1
cs431-cotter
20
Scheduling Algorithms
(Shortest Job First)
1

2
cs431-cotter
21
Scheduling in Interactive Systems







cs431-cotter
Round-robin scheduling
Priority scheduling
Multiple queues
Shortest process next
Guaranteed scheduling
Lottery scheduling
Fair-share scheduling
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
22
Round Robin Scheduling
• Developed in response to time-sharing
systems.
• Each job is given control of the CPU for a
short period - a time quantum. (In the range
of 10-100 milliseconds.)
• Control then passes to the next job (FIFO?)
• Average waiting time dependent on job size,
quantum size.
cs431-cotter
23
Round-Robin Scheduling
Figure 2-41. Round-robin scheduling.
(a) The list of runnable processes. (b) The list of runnable
processes after B uses up its quantum.
cs431-cotter
24
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Round Robin Scheduling
Process
Burst Time
P1
P2
P3
6
3
1
P4
7
0
cs431-cotter
10
All processes arrive at T=0
Vary Quantum time from
1 to 7. Calculate average
turnaround time (to job
completion).
20
30
25
Round Robin Scheduling
Process
Burst Time
P1
P2
P3
6
3
1
P4
7
0
All processes arrive at T=0
Quantum Average
Size
Turnaround
1
11
10
20
30
P1P2P3P4P1P2P4P1P2P4P1P4P1P4P1P4P4
cs431-cotter
26
Round Robin Scheduling
Process
Burst Time
P1
P2
P3
6
3
1
P4
7
0
All processes arrive at T=0
Quantum Average
Size
Turnaround
1
11
2
11.5
10
20
30
P1..P2..P3P4..P1..P2P4..P1..P4..P4
cs431-cotter
27
Round Robin Scheduling
Process
Burst Time
P1
P2
P3
6
3
1
P4
7
0
All processes arrive at T=0
Quantum
Average
Size
Turnaround
1
11
2
11.5
3
10.75
10
20
30
P1. . P2. . P3P4. . P1. . P4. . .P4
cs431-cotter
28
Round Robin Scheduling
avg. turnaround
13
12
11
10
9
8
1
cs431-cotter
2
3
4
5
6
7
Quantum
29
Priority Scheduling
• Each process (or class of processes) is
given a priority.
• Jobs are executed in priority order
• Issue: If there are sufficient high priority
jobs, lower priority jobs may never get
scheduled.
• One solution: Aging of processes. After a
job has been in queue for a given period of
time, raise its priority.
cs431-cotter
30
Priority Scheduling
Figure 2-42. A scheduling algorithm with four priority classes.
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
31
Multi-level queue Scheduling
• Partition the job space into distinct classes or
queues
– foreground / background
– system / interactive / editing / batch / student
– etc.
• Independently assign queueing service
disciplines
• Assign queue priorities
– Highest to lowest (empty one queue before starting next)
– Divide time between queues
• 80 / 20
• 30 / 20 / 20 / 20 / 10
cs431-cotter
32
Multilevel Feedback Queue
Scheduling
• Similar to Multilevel queue scheduling, but
jobs are allowed to move between queues.
• Avoids process starvation by allowing
neglected jobs to move up to a higher
queue.
• Differentiate queues by (for example)
quantum size
– queue 1 = 8 ms
– queue 2 = 16 ms
– queue 3 = FCFS
cs431-cotter
33
Thread Scheduling (1)
Figure 2-43. (a) Possible scheduling of user-level threads with a
50-msec process quantum and threads that run 5 msec per
cs431-cotter
34
CPU burst.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Thread Scheduling (2)
Figure 2-43. (b) Possible scheduling of kernel-level threads with
the same characteristics as (a).
cs431-cotter
35
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Multiple Processor Scheduling
• CPU scheduling more complex when
multiple CPUs are available
• Homogeneous processors within a
multiprocessor are usual (heterogeneous
processors found in distributed systems).
• Load sharing used with SMP.
– Single queue, multiple servers.
• Asymmetric Multiprocessing (AMP)
– Assign roles (system control, application processing)
– Avoids system data sharing
cs431-cotter
36
Benefits of Multiprocessors
Process Scheduling
– Process inter-arrival time variation can be characterized
by coefficient of variation = s / s
• standard deviation of interarrival time / mean service time
• ratio of 1 = exponential distribution. (your mileage may vary…)
– Differences between scheduling algorithms become
much less important in multiprocessor systems
RR to FCFS
thruput ratio
Single processor
cs431-cotter
Dual processor
Coefficient of Variation
37
Benefits of Multiprocessors
Thread Scheduling
• Allows true parallel processing within an
application
• However, if there is significant interaction
among threads, small differences in thread
management & scheduling can have big
results.
• General Approaches used
– Load Sharing - (not necessarily load balancing)
– Gang Scheduling - Schedule related threads together
– Dynamic Scheduling - allow # of threads to vary
cs431-cotter
38
Real-Time Scheduling
• System Response Classifications
– Hard real-time System - requires response
guarantees
– Soft real-time System – response not guaranteed
• Dispatch Latency
Interrupt
processing
Response interval
Dispatch latency
conflicts
cs431-cotter
Real-time
process
execution
dispatch
39
Summary
• Primary job of an OS is to schedule
processes
– Batch
– Interactive
– Real Time
• Selection of scheduling algorithm
depends on optimization criteria
cs431-cotter
40
Questions
•
•
•
•
•
On a system with multilevel queue scheduling, would you expect to
see the same scheduling algorithms used on all levels, or would you
expect them to be different? Justify your answer.
Consider the following set of processes, with the length of the CPU
burst time given in milliseconds. The processes are assumed to have
arrived in the order P1, P2, P3, P4.
Process
Job Size
P1
10
P2
1
P3
2
P4
5
Of the scheduling algorithms FCFS, SJF, and RR(qt=2) which
algorithm offers the best waiting time?
In many multiprocessing systems, although there is a fixed limit to the
amount of time that a job can keep control of the CPU, in practice, the
jobs are released earlier. Why?
The book talked about the multiple queueing system used by CTSS. If
a process needed 30 quanta to complete, how many times would it be
swapped in to complete?
How does Lottery Scheduling work? What is its principle advantage
over, say, Guaranteed Scheduling?
cs431-cotter
41