Scheduling
Scheduling
First-come, first-served (FCFS), which schedules tasks in the order in which they request the CPU. By far the
simplest CPU-scheduling algorithm is the first-come first-serve (FCFS) scheduling algorithm. With this
scheme, the process that requests the CPU first is allocated to the CPU first. The implementation of the FCFS
policy is easily managed with a FIFO queue. When a process enters the ready queue, its PCB is linked onto
the tail of the queue. When the CPU is free, it is allocated to the process at the head of the queue. The
running process is then removed from the queue.
On the negative side, the average waiting time under the FCFS policy is often quite long.
Example:
Let processes P1, P2, and P3 arrive at the ready queue in that order and let the run times (CPU burst times) of
these processes be as follows:
These processes are served by the CPU in FCFS order. The result can be shown in the following Gantt chart:
P1 P2 P3
0 24 27 30
If these processes arrived in the following order: P2, P3, P1 then the Gantt chart would be as follows:
P2 P3 P1
0 3 6 30
Exercise 1:
Consider the following processes with the relative CPU bursts which arrive together in the order given.
Process CPU burst
A 16
B 4
C 1
1
Draw the Gantt charts, and find out the average waiting time for FCFS CPU scheduling algorithm if:
a) Coming order is A, B, C.
b) Coming order is C, B, A
Exercise 2:
Consider the following five processes:
Find out the order with the minimum average waiting time for the FCFS CPU scheduling algorithm. Draw
the Gantt charts.
Note also 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. The
FCFS algorithm is thus particularly troublesome for interactive systems, where it is important that each
process gets a share of the CPU at regular intervals. It would be disastrous to allow one process to keep the
CPU for an extended period.
2
SJF Scheduling Algorithm (non-preemptive)
Shortest-job-first (SJF), which schedules tasks in order of the length of the tasks’ next CPU
burst. S JF Scheduling Algorithm chooses the process that has the smallest next CPU burst.
When the CPU is available, it is assigned to the process that has the smallest next CPU burst. If the next CPU
bursts of two processes are the same, FCFS scheduling is used to break the tie. Note that a more appropriate
term for this scheduling method would be the shortest-next-CPU-burst algorithm because scheduling depends
on the length of the next CPU burst of a process, rather than its total length. We use the term SJF because
most people and textbooks use this term to refer to this type of scheduling.
Example:
Assume there are 4 ready processes with their next CPU burst time as follows:
Using the SJF scheduling algorithm, these processes are scheduled according to the following Gantt Chart:
P4 P1 P3 P2
0 3 9 16 24
NOTE:
If the processes (in order P1, P2, P3, P4) are scheduled using the FCFS algorithm then the average waiting
time is (0+6+14+21) /4 = 10.25 ms.
P1 P2 P3 P4
0 6 14 21 24
→ SJF minimizes the average wait time because it services smaller processes before large ones.
● The main problem with the SJF algorithm is to know for each process the next CPU burst time!!!
However, some techniques exist to predict this next CPU burst time.
● Another problem is that starvation of long processes may occur.
The SJF scheduling algorithm is provably optimal, in that it gives the minimum average waiting time for a
given set of processes.
Although the SJF algorithm is optimal, it cannot be implemented at the level of CPU scheduling, as there is
no way to know the length of the next CPU burst. One approach to this problem is to try to approximate SJF
scheduling.
3
Turnaround time (TAT). From the point of view of a particular process, the important criterion
is how long it takes to execute that process. The interval from the time of submission of a process
to the time of completion is the turnaround time. Turnaround time is the sum of the periods spent
waiting in the ready queue, executing on the CPU, and doing I/O.
Waiting time. The CPU-scheduling algorithm does not affect the amount of time during which a
process executes or does I/O. It affects only the amount of time that a process spends waiting in
the ready queue. Waiting time is the sum of the periods spent waiting in the ready queue.
Exercise. Consider the following processes which are in the ready queue in the given order with the relative
next CPU bursts.
4
SRTN: Shortest Remaining Time Next (preemptive)
This is the preemptive version of SJF. The currently executing process will be preempted from the CPU if a
process with a shorter CPU burst time arrives.
Example:
Consider the following four processes:
Process Arrival time CPU burst time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
P1 P2 P4 P1 P3
0 1 5 10 17 26
W (P1) = 10-1 = 9 ms tat (P1) = 17 ms
W (P2) = 0 ms tat (P2) = 5-1 = 4 ms How to process preemptive scheduling:
W (P3) = 17-2 = 15 ms tat (P3) = 26-2 = 24 ms
W (P4) = 5-3 = 2 ms tat (P4) = 10-3 = 7 ms time P1 P2 P3 P4
0 8
Avg. waiting time: Average tat = 13 ms 1 7 4
= (9+0+15+2) / 4 2 7 3 9
= 26/4 3 7 2 9 5
= 6.5 ms …
P1 P2 P4 P3
0 8 12 17 26
W (P1) = 0 ms (P2) = 8-1 = 7 ms
W (P3) = 17-2 = 15 ms (P4) = 12-3= 9 ms
Exercise:
Consider the following four processes
5
Round Robin (RR) Scheduling (preemptive)
The round-robin (RR) scheduling algorithm is similar to FCFS scheduling, but preemption is added to enable
the system to switch between processes. A small unit of time, called a time quantum or time slice, is defined.
A time quantum is generally from 10 to 100 milliseconds in length. The ready queue is treated as a circular
queue. The CPU scheduler goes around the ready queue, allocating the CPU to each process for a time
interval of up to 1 time quantum.
Example:
Consider the following set of process that arrives at time 0 with the order A, B, C, D and the following CPU
burst time. Find the average waiting time with RR of quantum: 10 ms
A B C D A B C B B
0 10 20 30 36 46 56 60 70 80
Important note: smaller time quantum increases the number of context switches! So, time quantum should
be large with respect to the context switching time.
Exercise:
Consider the following set of processes with the order A, B, C, D and the following CPU burst time. Find the
average waiting time with RR of quantum: 2 time units.
6
Priority scheduling (preemptive and non-preemptive)
The basic principle of the priority scheduling algorithm is to assign a priority order to each ready process.
The ready process with the highest priority is allowed to run. Equal priority processes are scheduled
randomly. Priority scheduling can be of:
● Non-preemptive ( a process runs up to the end of its CPU burst time)
● Preemptive (a running process can be preempted by a process with a higher priority which joined the
ready queue later)
Example:
B E A C D
0 1 6 16 18 19
Example:
Draw the Gantt chart and compute the average waiting time in case of:
a) Non-preemptive
b) Preemptive.
a) Non-preemptive:
D A E C B
0 60 160 310 610 620
a) Preemptive:
D A E A C B
0 60 80 230 310 610 620
Note: SJF is a case of the priority scheduling where priority = 1/ (next CPU burst time).
Example. Suppose that the following processes arrive for execution at the times indicated. Each process will
run the listed amount of time.
A) What is the average waiting time for these processes with the FCFS scheduling algorithm?
P1 arrives and starts executing at 0.0 and terminates at 8, thus turnaround time is (8-0.0) = 8
P2 arrives at 0.4, starts executing at time 8 and terminates at 12, thus turnaround time is (12-0.4) =
11.6
P3 arrives at 1.0, starts executing at 12 and terminates at 13, and thus has a turnaround time is
(13-1)=12.
Average turnaround time = (8 + 11.6 + 12)/3 = 10.53
B) What is the average waiting time for these processes with the SJF (no preemption) scheduling algorithm?
P1 arrives and starts executing at time 0.0 and terminates at 8, thus the turnaround time is (8-0) = 8.
P2 arrives at 0.4, starts executing at 9 (after P3 has executed), and terminates at 13, thus the
turnaround time is (13-0.4) = 12.6.
P3 arrives at 1.0, starts executing at 8 and terminates at 9, thus the turnaround time is (9-1) = 8.
Average turnaround time = (8+12.6+8)/3 = 9.53
C) The SJF algorithm is supposed to improve performance, but notice that we chose to run process P1 at time
0 because we did not know that two shorter processes would arrive soon. Compute what the average
turnaround time will be if the CPU is left idle for the first1 time unit and then SJF scheduling is used.
Remember that processes P1 and P2 are waiting during this idle time, so their waiting time may increase.
This algorithm could be known as future-knowledge scheduling.
Remember that the CPU is left idle for the first 1 time unit.
P1 arrives time 0.0, starts executing at 6 (after P2 and P3 have executed) and terminates at 14, thus the
8
turnaround time is (14-0) = 14.
P2 arrives at 0.4, starts executing at 2.0 (after P3 has executed), and terminates at 6, thus the turnaround
time is (6-0.4) = 5.6.
P3 arrives at 1.0, starts executing at 1 (it has the shortest burst and starts executing first) and terminates at
2, thus the turnaround time is (2-1) = 1.
Average turnaround time = (14+5.6+1)/3 = 6.87
Note: A major problem with the priority scheduling is starvation: a low priority process may wait forever
before being executed. (MIT IBM 7094 crashed in 1973. They found a ready process submitted in 1967 and
still waiting for execution).
A solution to the problem of the indefinite blockage of low-priority processes is ageing. Ageing involves
gradually increasing the priority of processes that wait in the system for a long time. For example, if
priorities range from 127 (low) to 0 (high), we could periodically (say, every second) increase the priority of
a waiting process by 1. Eventually, even a process with an initial priority of 127 would have the highest
priority in the system and would be executed. In fact, it would take a little over 2 minutes for a priority-127
process to age to a priority-0 process.
Another option is to combine round-robin and priority scheduling in such a way that the system executes the
highest-priority process and runs processes with the same priority using round-robin scheduling. Let’s
Consider five processes P1, P2, P3, P4, and P5 with the following CPU burst times:
Using priority scheduling with round-robin for processes with equal priority, we would schedule these
processes according to the following Gantt chart using a time quantum of 2 milliseconds:
P4 P2 P3 P2 P3 P2 P3 P1 P5 P1 P5
0 7 9 11 13 15 16 20 22 24 26 27
In this example, process P4 has the highest priority, so it will run to completion. Processes P2 and P3 have
the next-highest priority, and they will execute in a round-robin fashion. Notice that when process P2 finishes
at time 16, process P3 is the highest-priority process, so it will run until it completes execution. Now, only
processes P1 and P5 remain, and as they have equal priority, they will execute in round-robin order until they
complete.
9
More Exercises
Exercise. Suppose that the following processes arrive for execution at the times indicated. Each process will
run the listed amount of time.
Draw the Gantt chart and compute the average waiting time in case of:
a) FCFS
b) Round robin with a quantum of 2 time units
c) SJF (non-preemptive)
d) SRTN
10
Exercise.
Consider five processes P1, P2, P3, P4, and P5 with the following CPU burst times:
Assume that overhead time for process switching and scheduling functions are negligible (assumed to be 0).
Draw the Gantt chart and compute the average waiting time in case of:
e) FCFS
f) SJF (non-preemptive)
g) SRTN
h) Round robin with a quantum of 3 time units.
i) Multi-level feedback queuing using three queues with the following parameters:
i. Queue 0 – quantum of 2 time units (after which process is moved to Queue 1)
ii. Queue 1 – quantum of 4 time units (after which process is moved to Queue 2)
iii. Queue 2 – FCFS
iv. All process that becomes ready is added to Queue 0.
v. A process that arrives in Queue 0 preempts any executing process that belongs to either
Queue 1 or Queue 2.
vi. Processes in Queue 1 are allocated to the CPU only when Queue 0 is empty.
vii. Processes in Queue 2 are allocated to the CPU only with both Queue 0 and Queue 1 are
empty.
11
Summary
● CPU scheduling is the task of selecting a waiting process from the ready queue and allocating the
CPU to it. The CPU is allocated to the selected process by the dispatcher.
● Scheduling algorithms may be either preemptive (where the CPU can be taken away from a process)
or non-preemptive (where a process must voluntarily relinquish control of the CPU). Almost all
modern operating systems are preemptive.
● Scheduling algorithms can be evaluated according to the following five criteria: (1) CPU utilization,
(2) throughput, (3) turnaround time, (4)waiting time, and (5) response time.
● First-come, first-served (FCFS) scheduling is the simplest scheduling algorithm, but it can cause short
processes to wait for very long processes.
● Shortest-job-first (SJF) scheduling is provably optimal, providing the short- est average waiting time.
Implementing SJF scheduling is difficult, however, because predicting the length of the next CPU
burst is difficult.
● Round-robin (RR) scheduling allocates the CPU to each process for a time quantum. If the process
does not relinquish the CPU before its time quantum expires, the process is preempted, and another
process is scheduled to run for a time quantum.
● Priority scheduling assigns each process a priority, and the CPU is allocated to the process with the
highest priority. Processes with the same priority can be scheduled in FCFS order or using RR
scheduling.
● Multilevel queue scheduling partitions processes into several separate queues arranged by priority,
and the scheduler executes the processes in the highest-priority queue. Different scheduling
algorithms may be used in each queue.
● Multilevel feedback queues are similar to multilevel queues, except that a process may migrate
between different queues.
● Earliest-deadline-first (EDF) scheduling assigns priorities according to the deadline. The earlier the
deadline, the higher the priority; the later the deadline, the lower the priority.
12