Unit 04 - CPU Scheduling Algorithms
Unit 04 - CPU Scheduling Algorithms
Structure:
4.1 Introduction
Objectives
4.2 Basic Concepts of Scheduling
4.3 Scheduling Algorithms
4.4 Evaluation of CPU Scheduling Algorithms
4.5 Summary
4.6 Terminal Questions
4.7 Answers
4.1 Introduction
Dear student, in the last unit you have studied about process management.
In this unit, let’s explore various CPU scheduling algorithms. The CPU
scheduler selects a process from among the ready processes to execute on
the CPU. CPU scheduling is the basis for multi-programmed operating
systems. CPU utilization increases by switching the CPU among ready
processes instead of waiting for each process to terminate before executing
the next. In this unit we will discuss various CPU scheduling algorithms
such as First-Come-First-Served, Shortest-Job-First, and Round-Robin etc.
Objectives:
After studying this unit, you should be able to:
explain basic scheduling concepts
describe different scheduling algorithms
discuss the evaluation of scheduling algorithms
till the process completes execution (Figure 4.1). Process execution begins
with a CPU burst followed by an I/O burst and then another CPU burst and
so on. Eventually, a CPU burst will terminate the execution. An I/O bound
job will have short CPU bursts and a CPU bound job will have long CPU
bursts.
:
:
Load memory
Add to memory CPU burst
Read from file
Wait for I/O I/O burst
Load memory
Make increment CPU burst
Write into file
I/O burst
Wait for I/O
Load memory
Add to memory CPU burst
Read from file
CPU Scheduler
Whenever the CPU becomes idle, the operating system must select one of
the processes in the ready queue to be executed. The short-term scheduler
(or CPU scheduler) carries out the selection process. The scheduler selects
from among the processes in memory that are ready to execute, and
allocates the CPU to one of them.
Note that the ready queue is not necessarily a first-in, first-out (FIFO) queue.
As we shall see when we consider the various scheduling algorithms, a
ready queue may be implemented as a FIFO queue, a priority queue, a tree,
or simply an unordered linked list. Conceptually, however, all the processes
in the ready queue are lined up waiting for a chance to run on the CPU. The
records in the queue are generally PCBs of the processes.
Preemptive / Non-preemptive Scheduling
CPU scheduling decisions may take place under the following four
circumstances. When a process:
1. switches from running state to waiting (an I/O request)
2. switches from running state to ready state (expiry of a time slice)
3. switches from waiting to ready state (completion of an I/O)
4. terminates
Scheduling under condition (1) or (4) is said to be non-preemptive. In non-
preemptive scheduling, a process once allotted the CPU keeps executing
until the CPU is released either by a switch to a waiting state or by
termination. Preemptive scheduling occurs under condition (2) or (3). In
preemptive scheduling, an executing process is stopped executing and
returned to the ready queue to make the CPU available for another ready
process. Windows used non-preemptive scheduling up to Windows 3.x, and
started using pre-emptive scheduling with Win95. Note that pre-emptive
scheduling is only possible on hardware that supports a timer interrupt. It is
to be noted that pre-emptive scheduling can cause problems when two
processes share data, because one process may get interrupted in the
middle of updating shared data structures.
Preemption also has an effect on the design of the operating-system kernel.
During the processing of a system call, the kernel may be busy with an
active on behalf of a process. Such activities may involve changing
important kernel data (for instance, I/O queues). What happens if the
process is preempted in t1: middle of these changes, and the kernel (or the
device driver) needs to read (modify the same structure). Chaos ensues.
Some operating systems, including most versions of UNIX, deal with this
problem by waiting either for a system call to complete, or for an I/O block to
take place, before doing a context switch. This scheme ensures that the
kernel structure is simple, since the kernel will not preempt a process while
P1 P2 P3
0 24 27 30
0 3 6 30
Average waiting time = (0 + 3 + 6) / 3 = 9 / 3 = 3 ms.
Average turnaround time = (3 + 6 + 30) / 3 = 39 / 3 = 13 ms.
Thus, if processes with smaller CPU burst times arrive earlier, then average
waiting and average turnaround times are lesser. The algorithm also suffers
from what is known as a convoy effect. Consider the following scenario. Let
there be a mix of one CPU bound process and many I/O bound processes
in the ready queue. The CPU bound process gets the CPU and executes
(long I/O burst). In the meanwhile, I/O bound processes finish I/O and wait
for CPU, thus leaving the I/O devices idle. The CPU bound process releases
the CPU as it goes for an I/O. I/O bound processes have short CPU bursts
and they execute and go for I/O quickly. The CPU is idle till the CPU bound
process finishes the I/O and gets hold of the CPU. The above cycle repeats.
This is called the convoy effect. Here small processes wait for one big
process to release the CPU. Since the algorithm is non-preemptive in nature,
it is not suited for time sharing systems.
Shortest-Job-First Scheduling
Another approach to CPU scheduling is the shortest job first algorithm. In
this algorithm, the length of the CPU burst is considered. When the CPU is
available, it is assigned to the process that has the smallest next CPU burst.
Hence this is named shortest job first. In case there is a tie, FCFS
scheduling is used to break the tie. As an example, consider the following
set of processes P1, P2, P3, P4 and their CPU burst times:
Process Burst time (ms)
P1 6
P2 8
P3 7
P4 3
Using SJF algorithm, the processes would be scheduled as shown below.
P4 P1 P3 P2
0 3 9 16 24
the time required to complete a job as given by the user can be used to
schedule. SJF algorithm is therefore applicable in long-term scheduling. The
algorithm cannot be implemented for CPU scheduling as there is no way to
accurately know in advance the length of the next CPU burst. Only an
approximation of the length can be used to implement the algorithm.
But the SJF scheduling algorithm is provably optimal and thus serves as a
benchmark to compare other CPU scheduling algorithms. SJF algorithm
could be either preemptive or non-preemptive. If a new process joins the
ready queue with a shorter next CPU burst then what is remaining of the
current executing process, then the CPU is allocated to the new process. In
case of non-preemptive scheduling, the current executing process is not
preempted and the new process gets the next chance, it being the process
with the shortest next CPU burst.
Given below are the arrival and burst times of four processes P1, P2, P3
and P4.
Process Arrival time (ms) Burst time (ms)
P1 0 8
P2 1 4
P3 2 9
P4 3 5
If SJF preemptive scheduling is used, the following Gantt chart shows the
result.
P1 P2 P4 P1 P3
0 1 5 10 17 26
Average waiting time = ((10 – 1) + 0 + (17 – 2) + (15 – 3)) / 4 = 26 / 4 = 6.5
ms.
If non-preemptive SJF scheduling is used, the result is as follows:
P1 P2 P4 P3
0 8 12 17 26
Priority Scheduling
In priority scheduling each process can be associated with a priority. CPU is
allocated to the process having the highest priority. Hence this is named
priority scheduling. Equal priority processes are scheduled according to
FCFS algorithm.
The SJF algorithm is a particular case of the general priority algorithm. In
this case priority is the inverse of the next CPU burst time. Larger the next
CPU burst, lower is the priority and vice versa. In the following example, we
will assume lower numbers to represent higher priority.
Process Priority Burst time (ms)
P1 3 10
P2 1 1
P3 3 2
P4 4 1
P5 2 5
Using priority scheduling, the processes are scheduled as shown in the
Gantt chart:
P2 P5 P1 P3 P4
0 1 6 16 18 19
Let the duration of a time slice be 4 ms, which is to say CPU switches
between processes every 4 ms in a round-robin fashion. The Gantt chart
below shows the scheduling of processes.
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Average waiting time = (4 + 7 + (10 – 4)) / 3 = 17/ 3 = 5.66 ms.
If there are 5 processes in the ready queue that is n = 5, and one time slice
is defined to be 20 ms that is q = 20, then each process will get 20 ms or
one time slice every 100 ms. Each process will never wait for more than
(n – 1) x q time units.
The performance of the RR algorithm is very much dependent on the length
of the time slice. If the duration of the time slice is indefinitely large then the
RR algorithm is the same as FCFS algorithm. If the time slice is too small,
then the performance of the algorithm deteriorates because of the effect of
frequent context switching. A comparison of time slices of varying duration
and the context switches they generate on only one process of 10 time units
is shown below.
Process Time= 10
Quantum Context
switch
12 0
0 10
6 1
0 6 10
1 9
0 1 2 3 4 5 6 7 8 9 10
The above example shows that the time slice should be large with respect to
the context switch time, else, if RR scheduling is used the CPU will spend
more time in context switching.
Multi-level Queue Scheduling
Processes can be classified into groups. For example, interactive processes,
system processes, batch processes, student processes and so on.
Processes belonging to a group have a specified priority. This algorithm
partitions the ready queue into as many separate queues as there are
groups. Hence this is named multilevel queue scheduling. Based on certain
properties, process is assigned to one of the ready queues. Each queue can
have its own scheduling algorithm like FCFS or RR. For example, queue for
interactive processes could be scheduled using RR algorithm where queue
for batch processes may use FCFS algorithm. An illustration of multilevel
queues is shown below (Figure 4.2).
Queues themselves have priorities. Each queue has absolute priority over
low priority queues that are a process in a queue with lower priority will not
be executed until all processes in a queue with higher priority have finished
executing. If a process in a lower priority queue is executing (higher priority
queues are empty) and a process joins a higher priority queue, then the
executing process is preempted to make way for a process in the higher
priority queue.
This priority on the queues themselves may lead to starvation. To overcome
this problem, time slices may be assigned to queues when each queue gets
some amount of CPU time. The duration of the time slices may be different
for queues depending on the priority of the queues.
Multi-level Feedback Queue Scheduling
In the previous multilevel queue scheduling algorithm, processes once
assigned to queues do not move or change queues. Multilevel feedback
queues allow a process to move between queues. The idea is to separate
out processes with different CPU burst lengths. All processes could initially
join the highest priority queue. Processes requiring longer CPU bursts are
pushed to lower priority queues. I/O bound and interactive processes remain
in higher priority queues. Aging could be considered to move processes
from lower priority queues to higher priority to avoid starvation. An
illustration of multilevel feedback queues is shown in Figure 4.3.
As shown in Figure 4.3, a process entering the ready queue joins queue 0.
RR scheduling algorithm with q = 8 is used to schedule processes in queue
0. If the CPU burst of a process exceeds 8 ms, then the process preempted,
deleted from queue 0 and joins the tail of queue 1. When queue 0 becomes
empty, then processes in queue 1 will be scheduled. Here also RR
scheduling algorithm is used to schedule processes but q = 16. This will
give processes a longer time with the CPU. If a process has a CPU burst
still longer, then it joins queue 3 on being preempted. Hence highest priority
processes (processes having small CPU bursts, that is I/O bound
processes) remain in queue 1 and lowest priority processes (those having
long CPU bursts) will eventually sink down. The lowest priority queue could
be scheduled using FCFS algorithm to allow processes to complete
execution.
Multilevel feedback scheduler will have to consider parameters such as
number of queues, scheduling algorithm for each queue, criteria for
upgrading a process to a higher priority queue, criteria for downgrading a
process to a lower priority queue and also the queue to which a process
initially enters.
Multiple-processor Scheduling
When multiple processors are available, then the scheduling gets more
complicated, because now there is more than one CPU which must be kept
busy and in effective use at all times. Multi-processor systems may be
heterogeneous, (different kinds of CPUs), or homogenous, (all the same
kind of CPU). This book will restrict its discussion to homogenous systems.
In homogenous systems any available processor can then be used to run
any processes in the queue. Even within homogenous multiprocessor, there
are sometimes limitations on scheduling. Consider a system with an I/O
device attached to a private bus of one processor. Processes wishing to use
that device must be scheduled to run on that processor; otherwise the
device would not be available.
If several identical processors are available, then load sharing can occur. It
would be possible to provide a separate queue for each processor. In this
case, however one processor could be idle, with an empty queue, while
another processor was very busy. To prevent this situation, we use a
common ready queue. All processes go into one queue and are scheduled
onto any available processor.
In such scheme, one of two scheduling approaches may be used. In one
approach, each processor is self–scheduling. Each processor examines the
common ready queue and selects a process to execute. We must ensure
that two processors do not choose the same process, and that processes
are not lost from the queue. The other approach avoids this problem by
The average waiting times for FCFS, SJF, and RR are 28ms, 13ms, and
23ms respectively. Deterministic modeling is fast and easy, but it requires
specific known input, and the results only apply for that particular set of input.
However, by examining multiple similar cases, certain trends can be
observed. (Like the fact that for processes arriving at the same time, SJF
will always yield the shortest average wait time.)
Queuing Models
Specific process data is often not available, particularly for future times.
However, a study of historical performance can often produce statistical
descriptions of certain important parameters, such as the rate at which new
processes arrive, the ratio of CPU bursts to I/O times, the distribution of
CPU burst times and I/O burst times, etc.
Armed with those probability distributions and some mathematical formulas,
it is possible to calculate certain performance characteristics of individual
waiting queues. For example, Little's Formula says that for an average
queue length of N, with an average waiting time in the queue of W, and an
average arrival of new jobs in the queue of Lambda, the these three terms
can be related by:
N = Lambda x W
Queuing models treat the computer as a network of interconnected queues,
each of which is described by its probability distribution statistics and
formulas such as Little's formula. Unfortunately real systems and modern
scheduling algorithms are so complex as to make the mathematics
intractable in many cases with real systems.
Simulations
Another approach is to run computer simulations of the different proposed
algorithms (and adjustment parameters) under different load conditions, and
to analyze the results to determine the "best" choice of operation for a
particular load pattern. Operating conditions for simulations are often
randomly generated using distribution functions similar to those described
above. A better alternative when possible is to generate trace tapes, by
monitoring and logging the performance of a real system under typical
expected work loads. These are better because they provide a more
accurate picture of system loads, and also because they allow multiple
simulations to be run with the identical process load, and not just statistically
equivalent loads. A compromise is to randomly determine system loads and
then save the results into a file, so that all simulations can be run against
identical randomly determined system loads.
Although trace tapes provide more accurate input information, they can be
difficult and expensive to collect and store, and their use increases the
Implementation
The only real way to determine how a proposed scheduling algorithm is
going to operate is to implement it on a real system. For experimental
algorithms and those under development, this can cause difficulties and
resistances among users who don't care about developing OS’s and are
only trying to get their daily work done. Even in this case, the measured
results may not be definitive, for at least two major reasons: (1) System
workloads are not static, but change over time as new programs are
installed, new users are added to the system, new hardware becomes
available, new work projects get started, and even societal changes. (For
example the explosion of the Internet has drastically changed the amount of
network traffic that a system sees and the importance of handling it with
rapid response times.) (2) As mentioned above, changing the scheduling
system may have an impact on the workload and the ways in which users
use the system.
Most modern systems provide some capability for the system administrator
to adjust scheduling parameters, either on the fly or as the result of a reboot
or a kernel rebuild.
Self Assessment Questions
7. To select an algorithm first we must define, the criteria on which we can
select the best algorithm. (True / False)
8. N = Lambda x W is _______________ formula.
4.5 Summary
Let’s summarize the key points covered in this unit:
The long-term scheduler provides a proper mix of CPU-I/O bound jobs
for execution. The short-term scheduler has to schedule these
processes for execution.
Scheduling can either be preemptive or non-preemptive. If preemptive,
then an executing process can be stopped and returned to ready state
to make the CPU available for another ready process. But if non-
preemptive scheduling is used then a process once allotted the CPU
keeps executing until either the process goes into wait state because of
an I/O or it has completed execution.
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
shortest average waiting time. Implementing SJF scheduling is difficult
because predicting the length of the next CPU burst is difficult.
Multilevel queue algorithms allow different algorithms to be used for
various classes of processes.
4.7 Answers
Self Assessment Questions
1. True
2. CPU Scheduler
3. a) 40%, 90%
4. False
5. Shortest-Job-First Scheduling
6. Round-Robin
7. True
8. Little’s
Terminal Questions
1. Many algorithms exist for CPU scheduling. Various criteria have been
suggested for comparing these CPU scheduling algorithms. Common
criteria include: CPU utilization, Throughput, Turnaround time, Waiting
time, Response time etc. (Refer Section 4.2)
2. First Come First Served scheduling algorithm is one of the very brute
force algorithms. A process that requests for the CPU first is allocated
the CPU first. Hence, the name first come first serve. The FCFS
algorithm is implemented by using a first-in-first-out (FIFO) queue
structure for the ready queue. (Refer Section 4.3)
3. The main disadvantage with the SJF algorithm lies in knowing the length
of the next CPU burst. In case of long-term or job scheduling in a batch
system, the time required to complete a job as given by the user can be
used to schedule. SJF algorithm is therefore applicable in long-term
scheduling. (Refer Section 4.3)
4. Computer simulations of the different proposed algorithms (and
adjustment parameters) are run under different load conditions, and the
results are analyzed to determine the "best" choice of operation for a
particular load pattern. Operating conditions for simulations are often
randomly generated using distribution functions. (Refer Section 4.4)