Scheduling of Processes (original) (raw)
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)
- another name is Shortest Process Next algorithm
- a better name may be shortest next-CPU-burst first
- assumes we know the length of the next CPU burst of all ready processes
- the length of a cpu burst is the length of time a process would continue executing if given the processor and not preempted
- SJF estimates the length of the next burst based on the lengths of recent cpu bursts
- starts with a default expected burst length for a new process
- suppose that time intervals are numbered 1 for first cpu burst, 2 for second cpu burst, etc.
- default length is e(1), the expected length of time for the first cpu burst
- unlike other scheduling algorithms, this algorithm assumes that information about a process's burst length is stored between the times when it is ready.
- in keeping with the need for efficiency, only a small amount of info is stored and only a simple calculation is performed
- we can weight the previous expectation (representing all previous bursts) and the most recent burst with any two weights that add up to 1, e.g., say 0.5 and 0.5, or 0.9 for previous expectations and 0.1 for actual time for most recent cpu burst
- the expected burst length is calculated as follows:
a(t) = actual amount of time required during cpu burst t
e(t) = amount of time that was expected for cpu burst t
e(t+1) = expected time during the next cpu burst
then... e(t+1) = 0.5 * e(t) + 0.5 * a(t)
- **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
- choose the process expected to take the least time, run it first until the end of its CPU burst
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.