Scheduling Algorithms Update
Scheduling Algorithms Update
Scheduling Algorithms Update
Scheduling Algorithms
for a single processing core
Week 11
CPU scheduling
P1 P2 P3
0 24 27 30
P2 P3 P1
0 3 6 30
• Note that the FCFS scheduling algorithm is non-preemptive. Once the CPU has been allocated to a
process, that process keeps the CPU until it releases the CPU, either by terminating or by requesting
I/O.
Shortest-Job-First (SJF) Scheduling
Associate with each process the length of its next CPU burst
Use these lengths to schedule the process with the shortest time
more appropriate name would be the shortest-next-CPU-burst
If the next CPU bursts of two processes are the same, FCFS scheduling is used to
break the tie.
SJF is optimal –
It gives minimum average waiting time for a given set of processes
Moving a short process before a long one decreases the waiting time of the short process
more than it increases the waiting time of the long process.
Consequently, the average waiting time decreases.
BUT:
The difficulty is knowing the length of the next CPU request
However, how to know the length of the next CPU burst
Could ask the user; (in a batch system, we may do so, but what about interactive )
What about approximation or prediction?
Shortest-Job-First (SJF) Scheduling
Two schemes:
nonpreemptive – once CPU given to the process it
cannot be preempted until completes its CPU burst
preemptive – if a new process arrives with CPU burst
length less than remaining time of current executing
process, preempt. This scheme is know as the
Shortest-Remaining-Time-First (SRTF)
SJF is optimal – gives minimum average waiting time for
a given set of processes
SJF Example 1
ProcessArriva Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
SJF scheduling chart
P4 P1 P3 P2
0 3 9 16 24
P1 P3 P2 P4
0 3 7 8 12 16
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
The choice arises when a new process arrives at the ready queue while a
previous process is still executing
The next CPU burst of the newly arrived process may be shorter than what
is left of the currently executing process.
Shortest-Remaining-Time- First
Example of Shortest-Remaining-Time-First
Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Preemptive SJF Gantt Chart
P1 P2 P4 P1 P3
0 1 5 10 17 26
If there are n processes in the ready queue and the time quantum is q, then each
process gets 1/n of the CPU time in chunks of at most q time units at once.
No process waits more than (n-1)*q time units.
The average waiting time under the RR policy is often long.
Performance ??
The ready queue is a circular FIFO queue, new processes are added to the tail (end)
If q is large enough FCFS
If q is small enough q must be large with respect to context switch, otherwise
overhead is too high; not worthy
Example 1 : RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
The Gantt chart is:
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Real Time Processes: i.e. the scheduling algorithm itself (scheduling process);
i.e. the computer inside the Engine Control Unit in a car has to manage the engine
at every moment based on what the driver wants to do (in real-time fashion).
Scheduling is a real-time process too
Try out:
Multilevel Queue
Ready queue is partitioned into separate queues:
foreground (interactive)
background (batch)
Three queues:
Q0 – RR with time quantum 8 milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS
Scheduling
A new job enters queue Q0 which is served
FCFS. When it gains CPU, job receives 8
milliseconds. If it does not finish in 8
milliseconds, job is moved to queue Q1.
At Q1 job is again served FCFS and receives 16
additional milliseconds. If it still does not
complete, it is preempted and moved to queue
Q2.
Multilevel Feedback Queues