Return to Contents
Scheduling
Definition
-
deciding how use the processor's time on the computer
-
Goal - to provide efficient service to all users --
at best, we have a trade-off
Types of schedulers:
1. long-term scheduler:
- selects process and loads it into memory for execution
- decides which process to start based on order and priority
- not used in timesharing systems
2. medium-term scheduler:
- schedule processes based on resources they require (memory, I/O)
- suspend processes for which adequate resources are not currently available
- commonly, main memory is the limiting resource and the memory
manager acts as the medium term scheduler
3. short-term scheduler (CPU scheduler):
- shares the processor among the ready (runnable) processes
- crucial the short-term scheduler be very fast
-- a fast decision is more important than an excellent decision
- if a process requires a resource (or input) that it does not have, it is removed from the ready list (and enters the WAITING state)
- uses a data structure called a ready list to identify ready processes
- started in response to a clock interrupt or when a process is suspended or exits
CPU burst
CPU burst: the amount of time the
process uses the processor before it is no longer ready
Types of CPU bursts:
- long bursts -- process is CPU bound (i.e. array work)
- short bursts -- process I/O bound (i.e. vi)
Scheduling Algorithms
a) First-Come, First-Served (FCFS)
- runs the processes in the order they arrive at the short-term scheduler
- removes a process from the processor only if it blocks (i.e., goes into the Wait state) or terminates
- wonderful for long processes when they finally get on
- terrible for short processes if they are behind a long process
e.g. A,B,C,D,E are processes
time units - discrete amounts of time
status: running => pid
waiting => w
P E | W E E E||
R |
O D | W W W D||
C |
E C | W W W W W W C||
S |
S B | W W W B B B B||
|
A | A A A A A A A||
|_________________________________________________________________
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
T I M E =>
TABLE:
T = elapsed time (includes waiting)
t = processing time required
M = T-t = missed (idle) time
R = t/T = ratio (response) time
P = T/t = penalty rate = 1/R
Arrival Process time (t) elapsed missed ratio Penalty
time required time (T) time
0 A 7 7 0 1 1
4 B 7 7 3 4/7 7/4
5 C 1 7 6 1/7 7
9 D 1 4 3 1/4 4
12 E 3 4 1 3/4 4/3
- large penalty number indicates being heavily penalized for being in a
time-shared system
b) Round Robin (RR)
- processes are given equal time slices called quanta (or quantums)
- takes turns of one quantum each
- if a process finishes early, before its quantum expires, the next process starts immediately and
gets a full quantum
- in some implementations, the next process may get only the rest of the quantum
- assume a new process arrives and goes into the queue before the process is removed from the processor
e.g. let quantum (q) = 1.
P E | W E E E ||
R |
O D | W D||
C |
E C | W C||
S |
S B | B W W B W B W W B||
|
A | A A A A W A W W A W W A||
|___________________________________________________________________
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
T I M E =>
TABLE:
T = elapsed time (includes waiting)
t = processing time required
M = T-t = missed (idle) time
R = t/T = ratio (response) time
P = T/t = penalty rate = 1/R
Arrival Process time (t) elapsed missed ratio penalty
time required time (T) time
0 A 7 12 5 7/12 12/7
4 B 4 9 5 4/9 9/4
5 C 1 2 1 1/2 2
9 D 1 2 1 1/2 2
12 E 3 4 1 3/4 4/3
Compare RR with FCFS
-
RR has a much lower penalty ratio than FCFS for processes with short cpu bursts
-
RR gives the processes with short bursts (interactive work) much better service
-
RR gives processes with long bursts somewhat worse service,
but greatly benefits processes with short bursts and the long
processes do not need to wait that much longer
-
RR is preemptive, that is sometimes the processor is taken away from a process
that can still use it
-
FCFS is not preemptive.
c) Shortest-Job-First (SJF)
For example:
Suppose a process p is given a default expected burst
length of 5 time units. When it is run, the actually burst
lengths are 10,10,10,1,1,1
(although this information is not known in advance to any algorithm).
The prediction of burst times for this process
works as follows.
Let e(1) = 5, as a default value.
When process p runs, its first burst actually runs 10 time
units, so, a(1) = 10.
e(2) = 0.5 * e(1) * 0.5 + a(1) = 0.5 * 5 + 0.5 * 10 = 7.5
This is the prediction for the second cpu burst
The actual second cpu burst is 10. So the prediction for the
third cpu burst is:
e(3) = 0.5 * e(2) * 0.5 + a(2) = 0.5 * 7.5 + 0.5 * 10 = 8.75
e(4) = 0.5 * e(3) * 0.5 + a(3) = 0.5 * 8.75 + 0.5 * 10 = 9.38,
after rounding to two decimal places.
So, we predict that the next burst will be close to 10 (9.38)
because we recent bursts have been of length 10.
At this point, it happens that the process starts having shorter
bursts, with a(4) = 1, so the algorithm gradually adjusts its estimated
cpu burst (prediction)
e(5) = 0.5 * e(4) * 0.5 + a(4) = 0.5 * 9.38 + 0.5 * 1 = 5.19
e(6) = 0.5 * e(5) * 0.5 + a(5) = 0.5 * 5.19 + 0.5 * 1 = 3.10
e(7) = 0.5 * e(6) * 0.5 + a(6) = 0.5 * 3.10 + 0.5 * 1 = 2.05
Once again, the algorithm has gradually adjusted to the process's
recent burst lengths.
If the bursts lengths continued to be 1, the estimates would
continue to adjust until, by rounding, they reached 1.00.
Each separate process will have a separate prediction of
the length of its next burst, i.e., e(b+1).
Now to implement the algorithm...
- choose the process expected to take the least time,
run it first until the end of its CPU burst
Notes:
- the Shortest Job First algorithm is a non preemptive algorithm
- the text says that SJF can be either preemptive or not;
we will always refer
to the preemptive version as Shortest Remaining Time instead.
- shortest job first is optimal at finishing the maximum number
of cpu bursts in the shortest time, if estimates are accurate
Problems:
- SJF cannot handle infinite loops
- poor performance for processes with short burst times arriving after
a process with a long burst time has started
- processes with long burst times may starve
- starvation - when a process is indefinitely postponed from getting on the processor
Example:
- suppose that based on previous information about the processes,
our estimates are exactly correct, i.e., we expect process A
to take 7 units, B to take 4 units, etc.
P E | W E E E||
R |
O D | W W W D||
C |
E C | W W C||
S |
S B | W W W W B B B B||
|
A | A A A A A A A||
|___________________________________________________________________
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
T I M E =>
at time:
0: e(A) = 7 it runs for 7 time units nonpreemptively
7: e(B) = 4 and e(C) = 1 choose C, it runs for 1 time unit
8: e(B) = 4 choose B, it runs for 4 time units
12: e(D) = 1; e(E) = 3 choose D, it runs for 1 time unit
13: e(E) = 3 choose E, it runs for 3 units
very short processes get very good service
a process may mislead the scheduler if it previously had a short bursts, but now may be cpu intensive (this algorithm fails very badly for such a case)
the penalty ratios are small; this algorithm works extremely well in most cases
(d) Shortest Remaining Time (SRT) Algorithm
- when a process arrives at the ready queue with an
expected CPU-burst-time that is less than the expected
remaining time of the running process,
the new one preempts the running process
- long processes can starve
P E | W W W E E E||
R |
O D | W D||
C |
E C | C||
S |
S B | W W W W W B W B B B||
|
A | A A A A A W A A||
|___________________________________________________________________
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
T I M E =>
at time:
0: e(A) = 7 choose A
1: (no decision required)
2:
3:
4: e(A) = 3; e(B) = 4 choose A
5: e(A) = 2; e(B) = 4; e(C) = 1 choose C -> done
6: e(A) = 2; e(B) = 4 choose A
7: no decision; A -> done
8: e(B) = 4 choose B
9: e(B) = 3; e(D) = 1 choose D -> done
10: e(B) = 3 choose B
11:
12: e(B) = 1; e(E) = 3 choose B -> done
13: e(E) = 3 choose E
14:
15: E -> done
very short processes get very good service
a process may mislead the scheduler if it previously ran quickly but now may be cpu intensive (this algorithm fails very badly for such a case)
the penalty ratios are small;
this algorithm works extremely well in most cases
this algorithm provably gives the highest throughput (number of processes completed) of all scheduling algorithms if the estimates are exactly correct.
(e) Nonpreemptive Priority Algorithm
processor is allocated to the ready process with the highest priority
(we will assume that the highest priority is 0, as in UNIX and LINUX)
shortest remaining time (SJF) algorithm is a priority algorithm
with the priority defined as the expected time
choose priority equal to the expected CPU burst time (p=t).
use 0 as highest priority.
e.g. priority p = t
A 7
B 4
C 1
D 1
E 3
if all arrive at time 0 and are to run to completion, the C, D have the highest priority, etc.
if there is a tie, choose the processes in alphabetical order
impractical and unrealistic in practice
(f) Preemptive Priority Algorithm
when a process arrives at the ready queue with a
higher priority than the running process,
the new one preempts the running process;
low priority processes can starve
- UNIX also has priorities; it uses aging to prevent starvation
- aging: if a process has not received service for a long time, its priority is increased again
(g) Multilevel Feedback-Queue Scheduling (FB)
priorities are implicit in the position of the queue that the ready
process is currently waiting in
when process is running on the processor, the OS must remember which
queue it came from
after a process has executed for one time quantum, it is moved down one queue
process is placed at the back of the queue
this is close to what is used for the traditional UNIX scheduler
a ready process is initially (implicitly) given a high priority, then, as it uses time
while still remaining ready, its priority is lowered
Main Parameters for FB
1. how long of a time quantum a process gets (quantum q)
2. how many queues are present (number of queues Q)
3. the scheduling algorithm for each queue (usually RR at each level, but
only one time around at each level but the last level); text says that bottom
level is FCFS--this is inaccurate because a time quantum is used even at the
bottom level.
4. the method used to determine when to upgrade a process to a higher
priority queue (usually this is some type of aging, whereby a process that
has waited a long time in a queue (say 15 minutes) gets upgraded by one level)
5. the method used to determine when to demote a process to a lower-priority queue
(in the simple case, move down one level at the end of each quantum)
6. the method used to determine which queue a process will enter when
it becomes ready (in the simple case, all ready processes enter at the end
of queue 0)
Variations of FB
1. give queues quantums of different lengths, e.g. #1, queue 0 - 1, queue 1 - 2,
queue 2 - 3, queue 3 - 4, etc.;
e.g. #2, queue 0 - 1, queue 1 - 2, queue 2 - 4, queue 3 - 8, etc.
2. how many times a process executes before being removed from the queue:
fixed maximum for each queue i.e. - one as in the simple FB algorithm
given above
execute n times for each level n
for level n, execute 2**n times
3. use a time slice among the queues i.e. 50% for level 0; 30% for level 1; and 20% for level 2
the simple FB algorithm given above allows starvation
because processes with long bursts
get no processor time for as long as those with short bursts are present
Scheduling Handouts
Blank forms for scheduling trace in PDF format
Trace of scheduling algorithms in PDF format
Example 1: Solaris (hercules)
-
Solaris uses four or more scheduling classes,
including Real-Time, System, Time-Sharing, and Interactive.
-
Almost every class uses a different scheduling algorithm
-
Multilevel feedback-queue algorithm is used for the Time-Sharing and Interactive classes.
-
Scheduling is done on the basis of threads rather than processes
-
Threads in (say) the Time-Sharing class are assigned a priority number from 0 to 59, with 59 representing
the highest priority.
-
Each priority level is assigned a time quantum, with short quanta for
high priority levels and
long quanta for low priority levels.
-
At the end of a time quantum, the priority of a ready thread is lowered by 10,
until it reaches 0 (the lowest queue)
-
When a thread switches from WAITING to READY, its priority is raised to between
50 and 59, depending on its previous priority, e.g., old priority was 0 to 9,
new priority is 50.
-
According to the text (7th edition, p. 177), the running thread
can be preempted in the midst of its quantum.
-
The text also states that if several threads have the same priority, they
are run in a round robin fashion. This statement is misleading. Each
thread is selected in turn from the queue, but at the end of its
quantum it will be moved to a different queue (unless it is already
in the lowest queue, which is indeed run in a round-robin fashion).
Example 2: Windows XP Scheduling
-
Windows XP uses a quantum-based, preemptive priority scheduling algorithm
-
Threads are scheduled rather than processes.
-
Since the preemptive priority algorithm is implemented with multiple
queues, it can also be considered a multiple feedback-queue algorithm.
However, each class of thread is normally restricted to a small band
of 5 priority levels, from 2 below the base priority for its process
to 2 above. Each band of priorities overlaps with the band above
it and the band below it.
-
Preemption can occur for any of 4 reasons:
-
higher-priority thread becomes ready
-
thread terminates
-
time quantum exhausted
-
thread performs a blocking system call, such as for
I/O, in which case it leaves the READY state and
enters a WAITING state.
-
32 priority levels are used,
where priority 31 is the highest priority and
priority 0 is the lowest priority
-
memory management thread: priority 0
- text shows idle thread with priority 1, which is
higher than the memory management thread, but it does not
explain how this could work
-
variable class of priorities (1-15)
-
real-time class of priorities (16-31)
-
Threads in the real-time class have fixed priorities
-
The running thread is always one with the highest priority level.
-
If no ready thread exists, the idle thread is run.
-
When a thread's time quantum runs out, its priority is lowered (by one
it appears), but its priority is never lowered too far
-
When a thread becomes READY after WAITING, it is given a priority boost,
with the largest boost for waiting for keyboard I/O and a smaller boost
for waiting for disk.
- any thread of the process associated with the
window that the user is currently interacting with (the foreground
process) is given a priority boost and as well has its
quantum size tripled.
Example 3: Linux 2.5 Scheduling
-
For more info, try google on Linux 2.5 Scheduler (you
should reach www.samspublishing.com)
-
Linux 2.5 uses a preemptive, priority algorithm
with 140 possible priority values.
-
The current priority of a process is determined from two parts:
-
The static priority (or nice value) set by the user for the
process. The static priority is in the range -20 to 19,
where -20 is the highest value.
-
-
Real-time processes have performance guarantees from the OS
-
Real-time processes have fixed priorities.
-
The scheduler uses a special data structure called a
runqueue.
The runqueue includes two groups of queues,
one referred to as the active priority array and the
other called the expired priority array.
-
At a particular time, processes are being scheduled from one priority array
while the other is accumulating ready processes. When the active
priority array is emptied,
the roles of the two priority arrays are swapped so
that the ``active'' becomes the ``expired'' and vice versa.
-
Within a priority array, there is a separate queue for every priority level
-
A bit map is used to store info about which queues contain any processes
-
The highest priority queue that is not empty is found using the bit
map; the text and the Sams website refer to this as O(1) time since
the number of queues is fixed. It would be equally correct to say
that a search down 140 queues is done in O(1) since 140 is fixed.
With the bit map approach and assuming 32 bit words, at most
ceiling(140 / 32) = ceiling(4.375) = 5
words need to be examined to determine the highest priority nonempty queue.
-
The highest priority processes are granted LONGER quanta and the
lower priority processes are granted SHORTER quanta.
-
The highest priority process is run for up to its quantum
and then removed from the CPU, assigned a new priority (probably
lower), and placed in the other priority array.
-
If the running process is preempted, which I assume can only
be done by a higher priority real-time process, it is put
back on the front of the queue, with its time remaining in the
quantum adjusted, and run again when the CPU is free.
-
After all processes in the active runqueue have had an opportunity
to execute, the scheduler switches to the other runqueue.
-
A process that becomes READY after WAITING is given a priority
based on how long it was in the WAITING state. If it was WAITING
a long time, it is given a higher priority (-5 from its normal
priority)
Return to Contents