You are on page 1of 42

Process Management - Lesson 3 – Scheduling algorithms

Processor scheduling algorithms


Processor scheduling plays an important role in the basic framework of the operating system.
This is due to the fact that the processor is one of the primary resources of the computer system.
When a multiprogramming computer system has a choice of processes to execute, it must have a
strategy for deciding which process to run at a given time and this enables the efficient utilization
of its resources. The strategy used by the scheduler to carry out the selection of a process for
execution is known as a processor scheduling algorithms, processor scheduling policy or
processor scheduling discipline.
Schedulers’ performance criteria or scheduling methodology
A number of scheduling algorithms are available for processor scheduling. Each scheduling
algorithm influences the resource utilization, overall system performance and quality of service
provided to the user. Therefore operating system designers must consider a number of
performance evaluation criteria while selecting the scheduling algorithm to use on a particular
system.
The purpose of a CPU (processor) scheduling algorithm is to maximize fairness, CPU
(processor) utilization, balanced utilization and throughput while minimizing turnaround, waiting
and response time. However, no scheduling criteria optimizes all the scheduling criteria.
Thus, the performance of an algorithm is evaluated on the basis of average measures, e.g. an
algorithm that minimizes the average waiting time is considered as a good algorithm since this
improves the overall efficiency of the system.
However, in the case of response time, minimizing the average is not a good criterion rather the
variance in the response time of the processes should be minimized since it is not desirable to
have a process with a high response time as compared to other processes.
Features of a good scheduling algorithm
A good scheduling algorithm should exhibit the following features;
 Fairness: A scheduling discipline is fair if all similar processes are treated the same and
no process can suffer indefinite postponement due to scheduling issues.
 Predictability: A given process should always run in about the same amount of time
under a similar system loads.
 Robustness: System performance should degrade gracefully i.e. it should not
immediately collapse) under heavy loads.

1 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

Types of scheduling algorithms


A wide variety of algorithms are used for CPU (processor) scheduling. These scheduling
algorithms fall into two categories namely:
 Non-preemptive scheduling algorithms
 Preemptive scheduling algorithms
Non-preemptive scheduling algorithms
A scheduling algorithm is said to be non-preemptive, voluntary or cooperative if, once the
operating system has assigned a processor to a process it cannot remove that processor from that
process until the process voluntarily releases it (in case the process has to wait for I/O or some
other event to occur) or the process terminates, i.e. a process once given a processor, runs to
completion or until it voluntarily relinquishes the processor. In other words, the decision to
schedule a process is made only when the currently running process switches to either the
waiting or terminated state. Some examples of non-preemptive scheduling algorithm include:
 First Come First Served (FCFS) or First In First Out (FIFO).
 Shortest Job First (SJF).
 Highest Response Ratio Next (HRRN) scheduling.
In short term scheduling, non-preemption implies that the running process retains ownership of
allocated resources including the processor, until it voluntarily surrenders controls to the
operating system i.e. the running process is not forced to relinquish ownership of its resources.
Preemptive scheduling algorithms
A scheduling algorithm is said to be preemptive, if the operating system can forcibly take or
remove the processor from a running process before it runs to completion and allocate the
processor to some other process. The preempted process is put back in the ready queue and
resumes its execution when it is scheduled again. Thus, a process may be scheduled many times
before its completion. In preemptive scheduling, the decision to schedule another process is
made whenever an interrupt occurs causing the currently running process to switch to ready state
or a process having a higher priority than the currently running process is ready to execute. Some
examples of preemptive scheduling algorithms include:
 Shortest Remaining Time Next (SRTN)
 Priority based scheduling.
 Round Robin (RR) scheduling.

2 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

In preemptive scheduling, a running process may be replaced by a higher priority process any
time. This is accomplished by activating the scheduler whenever an event that changes the state
of the system is detected.
Commonly used processor scheduling algorithms
A process scheduler schedules different processes to be assigned the processor based on a
particular scheduling algorithms. The most common process scheduling algorithms include:
First Come First Served (FCFS) or First In First Out (FIFO)
First-In-First Out (FIFO) also known as First Come First Served (FCFS) is the simplest
scheduling algorithm. As the name implies, the processes are executed in the order of their
arrival in the ready queue, which means the process that enters the ready queue first gets the
CPU (processor) first. FCFS is non-preemptive scheduling algorithm.
Therefore, once a process gets the CPU (processor), it retains the control of the CPU (processor)
until it blocks or terminates. The implementation of a FCFS scheduling algorithm is achieved
through a ready queue, which is easily managed as a First in First Out (FIFO) queue as shown in
the diagram below

Figure 1: Non preemptive scheduling queuing diagram

When the first process enters the ready queue, it immediately gets the CPU (processor), and
starts executing. Meanwhile, other processes enter the system and are added to the end of the
queue by inserting their PCBs in the queue. When the currently running process completes or
blocks, the CPU is allocated to the process at the head of the ready queue and its PCB is removed
from the queue. In case a currently running process was blocked and later transits into the ready
state, it joins the ready queue at the tail, i.e. Its PCB is linked to the end of the ready queue.

3 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

Characteristics
 It is a non-preemptive scheduling algorithm.
 It is fair since all processes are treated equally i.e. each process is given processor time.
 It is unfair since unimportant processes make important processes wait.
 It is bias to short jobs since long processes make short processes wait
 It is easy to understand and implement
 Its implementation is based on FIFO queue.
 Processes are executed on first come, first served basis.
 Poor in performance as average wait time is high.
Advantages
 First-In-First Out (FIFO) algorithm does not include any complex logic, it just places
process requests in a queue and executes them one by one.
 It is simple and easy to implement as processes are simply to be added at the tail and
removed from the head of the ready queue.
 It is non-preemptive hence once a process starts executing it runs to completion without
any interruptions.
 Starvation does not occur, since eventually, every process will get a chance to run.
 It is well suited for batch systems where the longer time periods for each process are
often acceptable.
Disadvantages
 It is unfair since unimportant processes make important processes wait.
 It is bias to short jobs since long processes make short processes wait
 It is bias to short jobs since long processes make short processes wait resulting in lower
CPU (processor) utilization.
 The average waiting time is not minimal and therefore it is not applicable where
performance is a major issue.
 It is not suitable for time sharing systems where each process should get the same amount
of CPU (processor) time.
 It reduces the CPU and I/O devices utilization under some circumstances, e.g. where
there are a long CPU bound process and many short I/O bound processes.

4 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

 Throughput is not emphasized.


Class exercise one
The table below shows four processes: P0, P1, P2 and P3. Use it to calculate the wait time for
each process and eventually the average wait time. Draw a Gantt chart to show the order of
execution. Calculate the turnaround time for each process and eventually the average turnaround
time. The operating system uses First-In-First Out (FIFO scheduling algorithm.
Process Arrival time Execute time (CPU burst time) Service time
P0 0 5 0
P1 1 3 5
P2 2 8 8
P3 3 6 16
Solution:
The wait time
Wait time of each process is as follows:
Process Wait time = Service time – Arrival time
P0 0–0=0
P1 5–1=4
P2 8–2=6
P3 16 – 3 = 13
The average wait time
Average wait time = (0+4+6+13)/4 = 5.75
The Gantt chart
Since the operating system is using First In First Out (FIFO) scheduling algorithm then, the order
in which the processor will be allocated to the processes is determined by the processes
individual arrival time, with the first one to arrive being allocated for the processor first, in the
case above, process P0 since it has the least arrival time i.e. 0.
The amount of time a process spends in a processor is determined by the process’s execute time
or CPU burst time. The execute time also determines the time the execution of the next process
to be allocated for the processor begins, bearing in mind that it is only the first process to be
allocated for the processor whose execution time begins at the zero mark unless otherwise stated
while all others do not.

5 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

Adding the execute time for each process to the time the previous process completed gives the
time when the next process to be allocated for the processor begins to execute and hence the
Gantt chart showing the order of execution will look like the diagram below:

1. Since the operating system uses the First-In-First Out (FIFO scheduling algorithm and
process P0 has the least arrival time, it will be allocated the processor first and will start
executing at 0 unit time. It will spend 5 unit time in the processor (execution time or CPU
burst time) to complete at 5 unit time.
2. Process P1 is the second process to be allocated for the processor since it has the second
least arrival time i.e. 1 and will start executing at 5-unit time. It will spend 3 unit time in
the processor to complete at 8 unit time.
3. Process P2 is the third process to be allocated for the processor since it has the third least
arrival time i.e. 2 and will start executing at 8-unit time. It will spend 8 unit time in the
processor to complete at 16-unit time.
4. Finally, process P3 is the last process to be allocated for the processor since it has the
highest arrival time i.e. 2 and will start executing at 16-unit time. It will spend 6-unit time
in the processor to complete at 22 unit time.
The turnaround time
Turnaround time of each process is as follows:
Process Turnaround time = Terminate time – Arrival time
P0 5–0=5
P1 8– 1 = 7
P2 16 – 2 = 14
P3 22 – 3 = 19
The average turnaround time
Average wait time = (5+7+14+19)/4 = 11.25 unit time

6 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

Class exercise two


Consider four processes P1, P2, P3 and P4 with their arrival times and required CPU burst (in
milliseconds) as shown in the following table:
Process P1 P2 P3 P4
Arrival time 0 2 3 5
CPU burst (ms) 15 6 7 5
Draw a Gantt chart to show the order of execution. Calculate the average waiting time and
average turnaround time. The operating system uses First-In-First Out (FIFO scheduling
algorithm.
Solution
The Gantt chart:
The processes will be executed as shown in the diagram below:

1. Since the operating system uses the First-In-First Out (FIFO scheduling algorithm and
process P1 has the least arrival time i.e. 0, it will be allocated the processor first and will
start executing at 0 unit time. It will spend 15 ms in the processor (execution time or CPU
burst time) to complete at 15 ms.
2. Process P2 is the second process to be allocated for the processor since it has the second
least arrival time i.e. 2 and will start executing at 15 ms. It will spend 6 unit time in the
processor to complete at 21 ms.
3. Process P3 is the third process to be allocated for the processor since it has the third least
arrival time i.e. 3 and will start executing at 21 ms. It will spend 7 unit time in the
processor to complete at 28 ms.
4. Finally, process P4 is the last process to be allocated for the processor since it has the
highest arrival time i.e. 5 and will start executing at 28 ms. It will spend 5 ms in the
processor to complete at 33 ms.

7 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

The turnaround time


Turnaround time of each process is as follows:
Process Turnaround time = Terminate time – Arrival time
P1 15 – 0 = 15
P2 21 – 2 = 19
P3 28 – 3 = 25
P4 33 – 5 = 28
The average turnaround time
Average wait time = (15+19+25+28)/4 = 21.75 ms
The wait time
Wait time of each process is as follows:
Process Wait time = Turnaround time – CPU burst time (Execute time)
P1 15 – 15 = 0
P2 19 – 6 = 13
P3 25 – 7 = 18
P4 28 – 5 = 23
The average wait time
Average wait time = (0+13+18+23)/4 = 13.5 ms
Note: The performance of FCFS scheduling algorithm largely depends on the order of arrival of
processes in the ready queue, that is whether the processes having long CPU bursts or execute
time enter before those having short CPU bursts or execute time or vice versa. To illustrate this,
assume that the processes in the above example enter the ready queue in the order shown in the
table below
Process P4 P2 P3 P1
Arrival time 0 2 3 5
CPU burst (ms) 15 6 7 5
Now the processes will execute as shown in the Gantt chart below:

8 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

Hence:
The turnaround time
Turnaround time of each process is as follows:
Process Turnaround time = Terminate time – Arrival time
P1 33 – 5 = 28
P2 11 – 2 = 9
P3 18 – 3 = 15
P4 5–0=5
The average turnaround time
Average wait time = (28+9+15+5)/4 = 14.25 ms
The wait time
Wait time of each process is as follows:
Process Wait time = Turnaround time – CPU burst time (Execute time)
P1 28 – 15 = 13
P2 9–6=5
P3 15 – 7 = 8
P4 5 – 15= -10
The average wait time
Average wait time = (13+5+8+10)/4 = 6 ms
Note: Ignore the negative sign in P4’s wait time
It is clear that if the processes having shorter CPU burst execute before those having longer CPU
burst, the average waiting and turnaround time reduces significantly.
Practice exercise one
The table below shows four processes: P1, P2, P3, P4 and P5. The operating system uses First-In-
First Out (FIFO) scheduling algorithm.

9 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

Process Arrival time CPU burst time


P1 0 9
P2 1 5
P3 2 2
P4 3 6
P5 4 8

Required:
i. Draw a Gantt chart to show the order of execution
ii. Calculate the wait time for each process and eventually the average wait time
iii. Calculate the turnaround time for each process and eventually the average turnaround
time
Practice exercise two
The table below shows details of FCFS scheduling algorithm. Use it to answer the question that
follow
Process Arrival time Execution time Service time
P0 0 5 0
P1 1 3 5
P2 2 8 8
P3 3 6 16
Required:
i. Calculate the wait time for each process labeled P0, P1, P2 and P3
ii. Calculate the average wait time for all the processes.
iii. Draw a Gantt chart to show the order of execution
iv. Calculate the turnaround time for each process labeled P0, P1, P2 and P3
v. Calculate the turnaround time for all the processes.
Practice exercise three
The table below shows different processes and their respective burst times during inter process
communication. Use it to answer the questions that follow

10 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

Process Burst time


P1 32
P2 6
P3 9
P4 12

Suppose the processes arrive at time 0 in the order P2, P4, P1 and P3 respectively.
Required:
Assuming the First In First Out (FIFO) scheduling algorithm:
i. Draw a Gantt chart to show the order of execution
ii. Calculate the wait time for each process labeled P1, P2, P3 and P4
iii. Calculate the average wait time for all the processes.
iv. Calculate the turnaround time for each process labeled P1, P2, P3 and P4
v. Calculate the turnaround time for all the processes.
Shortest Job First (SJF) scheduling
The Shortest Job First also known as Shortest Process Next (SNP), Short Job Next (SJN) or
Shortest Request Next (SRN), is a non-preemptive scheduling algorithm that schedules the
processes according to the length of CPU burst, execute time or runtime they require. At any
point in time, among all the ready processes, the one having the shortest (smallest) CPU burst,
execute time or runtime is scheduled first.
Thus, a process has to wait until all the processes shorter than it have been executed. This tends
to reduce the number of waiting processes and also the number of processes waiting behind large
processes.
Note:
 In case two processes have the same CPU burst, execute time or runtime, they are
scheduled in the FCFS order.
 Shortest Job First is of two types, Non-preemptive and preemptive. The preemptive
version of Shortest Job First scheduling is known as Shortest Remaining Time

11 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

Characteristics
 It favours short processes.
 It is a non-preemptive scheduling algorithm
 It selects processes for service in a manner ensuring the next one will complete and the
leave the computer system as soon possible.
 It reduce the number of waiting processes and also the number of processes waiting
behind large processes.
 It selects the job with the shortest service time hence is bias to long jobs.
 The processor should know in advance how much time a process will take.
 It is optimal if all the jobs/processes are available at the same time i.e. all processes either
arrival time is 0 for all, or arrival time is the same for all

Advantages
 It favours short processes and eliminates the bias against them experienced in First-In-
First Out (FIFO) algorithm.
 Throughput is increased since more processes can be executed in a lesser amount of time.
 It is easy to implement in batch systems where required processor time is known in
advance.
 It eliminates the variance in waiting and turnaround times.
 It minimizes average waiting and turnaround time

Disadvantages
 It is difficult to implement as it needs to know the length of CPU burst of processes in
advance. In practice, having the prior knowledge of the required execute time of
processes is difficult.
 It is bias towards processes having longer CPU burst. This is because as long as the short
processes continue to enter the ready queue, the long processes will not be allowed to get
the CPU. This results in starvation of long processes
 Longer processes and thus they will have more waiting time and eventually suffer
starvation.
 Elapsed time i.e., execution-completed-time must be recorded, it results to an additional
overhead on the processor.
12 Complied by Mr. P.K Munene
Process Management - Lesson 3 – Scheduling algorithms

Class exercise one


The table below shows four processes: P0, P1, P2 and P3. Use it to calculate the wait time for
each process and eventually the average wait time. Draw a Gantt chart to show the order of
execution. Calculate the turnaround time for each process and eventually the average turnaround
time. The operating system uses Shortest Job First (SJF) scheduling
Process Arrival time Execute time Service time
P0 0 5 3
P1 1 3 0
P2 2 8 16
P3 3 6 8
Solution:
The wait time
Wait time of each process is as follows:

Process Wait time = Service time – Arrival time


P0 3–0=3
P1 0–1=0
P2 16– 2 = 14
P3 8–3=5

The average wait time


Average wait time = (3+0+14+5)/4 = 5.5
The Gantt chart
Since the operating system is using a non-preemptive version of Shortest Job Next (SJN)
scheduling algorithm then, the order in which the processor will be allocated to the processes is
determined by the processes individual execute time with the process with the least execute time
being allocated for the processor first and running to completion without being interrupted.
However, if the processes arrive at different times, then the process with the least arrival time in
allocated the processor first, and the next process to be allocated for the processor will be the
process with the least CPU burst time or execute time. In the case above, process P0 will be

13 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

allocated the processor first since it has the least arrival time i.e. 0 and process P1 will be the
second process to be allocated the processor since it has the least execute time i.e. 3.
More so, if all the processes arrive at the time then only the CPU burst time or execute time is
considered with the process with the least CPU burst time or execute time being allocated for the
processor first.
The amount of time a process spends in a processor is determined by the process’s execute time
or CPU burst time. The execute time also determines the time the execution of the next process
to be allocated for the processor begins, bearing in mind that it is only the first process to be
allocated for the processor whose execution time begins at the zero mark unless otherwise stated
while all others do not.
Adding the execute time for each process to the time the previous process completed gives the
time when the next process to be allocated for the processor begins to execute and hence the
Gantt chart showing the order of execution will look like the diagram below:

1. Since the operating system uses the non-preemptive version of Shortest Job Next (SJN)
scheduling algorithm and process P0 has the least arrival time i.e. 0 it will be allocated the
processor first and will start executing at 0 unit time. It will spend 5 unit time in the
processor (execution time or CPU burst time) to complete at 5 unit time.
2. Process P1 is the second process to be allocated for the processor since it has the least
execute time i.e. 3 and will start executing at 5 unit time. It will spend 3 unit time in the
processor to complete at 8 unit time.
3. Process P3 is the third process to be allocated for the processor since it has the second
least execute time i.e. 6, ignoring Process P0 since it was the first to be allocated for the
processor and will start executing at 8 unit time. It will spend 6 unit time in the processor
to complete at 14 unit time.
4. Finally, process P2 is the last process to be allocated for the processor since it has the
third highest execute time and will start executing at 14 unit time. It will spend 8-unit
time in the processor to complete at 22 unit time.

14 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

The turnaround time


Turnaround time of each process is as follows:
Process Turnaround time = Terminate time – Arrival time
P0 5 – 0= 5
P1 8–1=7
P2 14 – 2 = 12
P3 22 – 3 = 21
The average turnaround time
Average wait time = (5+7+12+21)/4 = 11.25 unit time
Class exercise two
Consider four processes P1, P2, P3 and P4 with their arrival times and required CPU burst (in
milliseconds) as shown in the following table:
Process P1 P2 P3 P4
Arrival time 0 1 3 4
CPU burst (ms) 7 5 2 3
Draw a Gantt chart to show the order of execution. Calculate the average waiting time and
average turnaround time. The operating system uses the Shortest Job First (SJF) scheduling
Solution:
The Gantt chart:
The processes will be executed as shown in the diagram below:

1. Since the operating system uses the Shortest Job First (SJF) scheduling algorithm and
process P1 has the least arrival time i.e. 0, it will be allocated the processor first and will
start executing at 0-unit time. It will spend 7 ms in the processor (execution time or CPU
burst time) to complete at 7 ms.

15 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

2. Process P3 is the second process to be allocated for the processor since it has the least
execute time i.e. 2 and will start executing at 7 ms. It will spend 2 unit time in the
processor to complete at 9 ms.
3. Process P4 is the third process to be allocated for the processor since it has the second
least execute time i.e. 3 and will start executing at 9 ms. It will spend 3-unit time in the
processor to complete at 12 ms.
4. Finally, process P2 is the last process to be allocated for the processor since it has the
third least execute time i.e. 5 and will start executing at 12 ms. It will spend 5 ms in the
processor to complete at 17 ms.
The turnaround time
Turnaround time of each process is as follows:
Process Turnaround time = Terminate time – Arrival time
P1 7–0=7
P2 17 – 1 = 16
P3 9–3=6
P4 12 – 4 = 8
The average turnaround time
Average wait time = (7+16+6+8)/4 = 9.25 ms
The wait time
Wait time of each process is as follows:
Process Wait time = Turnaround time – CPU burst time (Execute time)
P1 7–7=0
P2 16 – 5 = 11
P3 6–2=4
P4 8–3=5
The average wait time
Average wait time = (0+11+4+5)/4 = 5 ms
Practice exercise one
A computer system has a set of five processes P1, P2, P3, P4 and P5 on the queue and which arrive
at time 0, in that order. Each of the processes has a CPU burst time shown in the table below. Usr
it to answer the questions that follow

16 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

Process Burst time (Ms)


P1 12
P2 25
P3 6
P4 9
P5 15

Required:
i. Draw a Gantt chart to show the order of execution
ii. Calculate the wait time for each process and eventually the average wait time, assuming a
non-preemptive Shortest Job First (SJF) scheduling algorithm.
iii. Calculate the turnaround time for each process and eventually the average turnaround
time
Practice exercise two
The table below shows details in a computer system. Use it to answer the questions that follow
Process Arrival time Run time
W 0 2
X 2 7
Y 2 20
Z 3 3
Required:
i. Draw a Gantt chart to show the order of execution
ii. Calculate the wait time for each process and eventually the average wait time, assuming a
non-preemptive Shortest Job First (SJF) scheduling algorithm.
iii. Calculate the turnaround time for each process and eventually the average turnaround
time.
iv. Explain three advantages of the system

17 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

Practice exercise three


Four batch processes namely A, B, C and D are to be run in a computer system. The processes
arrive at the same time and have run time of 6, 4, 7 and 5 respectively. Assuming a non-
preemptive Shortest Job First (SJF) scheduling algorithm.
a) Calculate:
i. The turnaround time for each process
ii. The mean turnaround time
iii. The waiting time for each process
iv. The average waiting time
b) Illustrate the order of process completion
Shortest Remaining Time (SRT) scheduling
Shortest Remaining Time (SRT) scheduling also known as Shortest Time to Go (STG) is a
preemptive version of Shortest Job First (SJF). It takes into account the length of the remaining
CPU burst of the processes rather than the whole length in order to schedule them i.e. the
scheduler selects the process with the shortest remaining processing time or shortest estimated
runtime to completion. A newly arriving process with a shorter estimated run time preempts a
running process with a larger runtime to completion.
While a process is being executed, the CPU or processor can be removed from it (preempted)
and assigned to a newly arriving process with a CPU or processor burst time of the new process
is shorter than its remaining CPU burst.
Note: If at any point of time, the remaining CPU burst of two processes becomes equal, they are
scheduled in the First Come First Served (FCFS) order.
Characteristics
 Shortest Remaining Time (SRT) is the preemptive version of the Shortest Job Next (SJN)
algorithm.
 The processor is allocated to the process closest to completion but it can be preempted by
a newer ready process with shorter time to completion
 Impossible to implement in interactive systems where required processor time is not
known.
 It is often used in batch environment where short processes need to be given preference.
 It attempts to increase throughput by servicing small arriving processes.

18 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

Advantages
 Processes have short CPU burst time hence execute fast.
 Average turnaround time is less.
Disadvantages
 Long burst (CPU bound) processes have increased mean waiting time hence long
processes may suffer starvation.
 Difficult to implement since CPU burst time must be known in advanced or estimated
and this is difficult to do.
 A running process, which has run almost to completion, may be preempted by a new job
with a very small run time creating an unnecessary overhead.
Class exercise one
The table below shows five processes: P1, P2, P3 and P4. Draw a Gantt chart to show the order of
execution. Calculate the turnaround time for each process and eventually the average turnaround
time. Calculate the wait time for each process and eventually the average wait time. The
operating system uses Shortest Remaining Time (SRT) scheduling
Process Arrival time CPU burst time
P1 0 21
P2 1 5
P3 2 6
P4 3 2
Solution:
The Gantt chart
Since the operating system is using a preemptive version of Shortest Job Next (SJN) or Shortest
Remaining Time (SRT) scheduling algorithm then, the order in which the processor will be
allocated to the processes is determined by the processes individual remaining execute time with
the process with the least remaining execute time being allocated for the processor first.
However a running process can be preempted by a newly arriving process whose execute time is
smaller than the remaining execute time of the currently running process. In the case above,
process P1 will be allocated the processor first since it has the least arrival time i.e. 0 and process
P2 will be the second process to be allocated the processor since it has the second least arrival
time i.e. 1. However, on the arrival of process P2, the remaining execute time for process P1 will

19 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

is compared to that of P2 to determine if process be P1 will continue executing or will be


preempted by process P2.
Note: If all the processes arrive at the time then only the CPU burst time or execute time is
considered with the process with the least CPU burst time or execute time being allocated for the
processor first.
The amount of time a process spends in a processor is determined by the process’s execute time
or CPU burst time. The execute time also determines the time the execution of the next process
to be allocated for the processor begins, bearing in mind that it is only the first process to be
allocated for the processor whose execution time begins at the zero mark unless otherwise stated
while all others do not.
Adding the execute time for each process to the time the previous process completed gives the
time when the next process to be allocated for the processor begins to execute and hence the
Gantt chart showing the order of execution will look like the diagram below:

1. Since the operating system uses the preemptive version of Shortest Job Next (SJN) or
Shortest Remaining Time (SRT) scheduling algorithm and P1 has the least arrival time i.e.
0, it will be allocated the processor first and will start executing at 0 unit time.
2. After 1 ms, process P2 arrives with a burst time of 5 ms which is less than the burst time
of process P1, hence the process P1 with 1 ms done, 20 ms left, is preempted and
process P2 is allocated for the processor and beings to execute.
3. After 1 ms, of executing process P2, process P3 with a burst time of 6 ms which is greater
than the remaining burst time of process P2, arrives. Thus, process P3 does not preempt
process P2 and process P2 continue to execute.
4. After another 1 ms of executing process P2, process P4 with a burst time of 2 ms which is
less than the remaining burst time of process P2, arrives. As a result P2 with 2 ms done, 3
ms left is preempted and P4 is allocated for the processor and beings to execute.
5. Process P4 runs to completion to complete at 5 ms.

20 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

6. Process P2 is allocated for the processor and runs to completion to complete at


7. Finally, process P1, is allocated for the processor and runs to completion to complete at 32
ms.
The turnaround time
Turnaround time of each process is as follows:
Process Turnaround time = Terminate time – Arrival time
P1 34 – 0 = 34
P2 8–1=9
P3 14 – 2 = 12
P4 5–3=2
The average turnaround time
Average wait time = (34+9+12+2)/4 = 14.25 ms
The wait time
Wait time of each process is as follows:
Process Wait time = Turnaround time – CPU burst time (Execute time)
P1 34 – 21 = 13
P2 9–5=4
P3 12– 6 = 6
P4 2 – 2= 0
The average wait time
Average wait time = (13+4+6+0)/4 = 5.75 ms
Practice exercise one
The table below shows five processes: P1, P2, P3 P4 and P5. Draw a Gantt chart to show the order
of execution. Calculate the turnaround time for each process and eventually the average
turnaround time. Calculate the wait time for each process and eventually the average wait time.
The operating system uses Shortest Remaining Time (SRT) scheduling

21 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

Process Arrival time CPU burst time


P1 0 3
P2 2 6
P3 4 4
P4 6 5
P5 8 2
Practice exercise one
Consider four processes P1, P2, P3 and P4 with their arrival times and required CPU burst (in
milliseconds) as shown in the following table:
Process Arrival time CPU burst time
P1 0 7
P2 1 5
P3 3 2
P4 4 3
Draw a Gantt chart to show the order of execution. Calculate the average waiting time and
average turnaround time. The operating system uses Shortest Remaining Time (SRT) scheduling
Round Robin (RR) scheduling
Round Robin (RR) scheduling algorithm, also known as time slicing scheduling is similar to
First In First Out (FIFO) with preemption based on a clock added. Processes are dispatched on a
First In First Out (FIFO) basis but are given a limited amount of processor of time referred to as
a time slice or a quantum. A clock interrupt is generated at periodic intervals.
When the interrupt occurs, the currently running process is placed in the ready queue and the
next ready process is selected on a First-come, First-serve basis. This process is known as time
slicing, because each process is given a slice of time before being preempted. One of the
following happens:
 The process may have a CPU burst or execute time of less than the time quantum
 CPU burst or CPU burst or execute time of the currently executing process may be longer
than the time quantum, if a process does not complete before its quantum expires, a
context switch occurs, the operating system preempts it and gives the processor to the
next waiting process. The operating system then places the preempted process at the back

22 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

(tail) of the ready queue to wait a second quantum and this continues until the process
runs to completion.

Figure 2: Round Robin scheduling

In Round Robin scheduling, the principal design issue is the length of the time quantum or time-
slice to be used. If the quantum is very short, then short processes will move quickly. In the
figure below process, P1 is dispatched to a processor, where it executes either until completion,
in which case it exists the system or until its time slice expires, at which point it is preempted and
placed at the tail of the ready queue. The scheduler then dispatched process P2.

Ready queue Completion


P1 P3 P2 P1 Dispatch
Processor

Preemption

Characteristics

 Round robin is a preemptive process-scheduling algorithm.


 Each process is provided for a fixed time to execute known as a quantum.
 Once a process is executed for a given time period, it is preempted and other processes
execute for a given time period.
 Context switching is used to save states of preempted processes

Advantages
 Round Robin is effective in a general-purpose, times-sharing system or transaction-
processing system.
 Each process is served by the processor for a fixed time quantum, so all processes are
given the same priority.
23 Complied by Mr. P.K Munene
Process Management - Lesson 3 – Scheduling algorithms

 Starvation does not occur since in each round robin cycle, each process is given a fixed
time to execute.
 Overhead on processor is low
 Good response time for short processes.

Disadvantages

 The throughput in RR largely depends on the choice of the length of the time quantum. If
time quantum is longer than needed, it tends to exhibit the same behavior as First Come
First Served (FCFS).
 If time quantum is shorter than needed, the number of times that the processor switches
from one process to another process, increases. This leads to decrease in processor
efficiency.
 Care must be taken in choosing quantum value.
 Processing overhead is there in handling clock interrupt.
 Throughput is low if time quantum is too small.

Performance of Round Robin (RR) scheduling


 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 for more than (n-1)*q time units until the next time quantum. With five
processes and a time quantum of 20, each process will get up to 20-unit time every 100-
unit time.
Factors effecting the overall performance of Round Robin (RR) scheduling
The performance of Round Robin (RR) scheduling depends on the following factors
 Size of the time quantum: If time quantum is larger than the CPU burst, then it is the
same as First Come First Served (FCFS) and thus performance degrade and if the time
quantum size is very small, then the number of content switches increases and the time
quantum almost equal the time taken to switch the CPU from one process to another.
Therefore 50% of time is spent in switching of processes making overhead too high.
 Number of context switching: The number of context switches should not be too many
to slow down the overall execution of all the processes. Time quantum should be large

24 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

with respect to the context switch time. This is to ensure that a process keeps CPU for a
maximum time as compared to the time spent in the context switching.
Class exercise one
The table below shows four processes: P0, P1, P2 and P3. Use it to calculate the wait time for
each process and eventually the average wait time. Draw a Gantt chart to show the order of
execution. Calculate the turnaround time for each process and eventually the average turnaround
time. The operating system uses round robin scheduling algorithm and the quantum = 4
Process Arrival time Execute time Service time
P1 1 3 24
P2 2 8 3
P3 3 6 3
Solution:
The wait time
Wait time of each process is as follows:

Process Wait time = Service time – Arrival time


P1 24 – 1 = 23
P2 3–2=1
P3 3–3=0
The average wait time
Average wait time = (23+1+ 0)/3 = 8
The Gantt chart
Since the operating system is using Round Robin (RR) scheduling algorithm then, the order in
which the processor will be allocated to the processes is determined by the processes individual
arrival time, with the first one to arrive being allocated for the processor first, in the case above,
process P1 since it has the least amount of arrival time i.e. 1. Round Robin implements the First
in First out (FIFO) scheme.
The amount of time a process spends in a processor is determined by the process’s execute time
or CPU burst time. The execute time also determines the time the execution of the next process
to be allocated for the processor begins, bearing in mind that it is only the first process to be
allocated for the processor whose execution time begins at the zero mark unless otherwise stated
while all others do not.
25 Complied by Mr. P.K Munene
Process Management - Lesson 3 – Scheduling algorithms

Adding the execute time for each process to the time the previous process completed gives the
time when the next process to be allocated for the processor begins to execute and hence the
Gantt chart showing the order of execution will look like the diagram below:

1. Using a time quantum of 4-unit time, then process P1 is allocated for since it has the least
arrival time i.e. 1 and is runs for the first quantum but does not run to completion since it
requires another 20-unit time to complete (service time – first quantum), hence it is
preempted after the first quantum and placed at the tail of the ready queue to await the a
second allocation of the processor (second quantum)
2. The processor is allocated to the next process in the queue, process P2 since it has the
second least arrival time i.e. 2. Process P2 does not need 4 unit time to run completion
since its service time is 3 hence it completes before its quantum expires and exits the
system.
3. The processor is allocated to the next process, process P3 since it has the third least arrival
time i.e. 3. Process P3 also does not need 4 unit time to run to completion since its service
time is 3 hence it completes before its quantum expires and exits the system.
4. The processor is allocated to process P1 for an additional time quantum. On the expiry of
the second quantum it is immediately allocated a third, fourth, five and six quantum since
it is the only runnable process in the ready queue, to run to completion.

Note:
 In Round Robin scheduling algorithm, no process is allocated the processor for more than
one-time quantum in a row unless it is the only runnable process in the ready queue.
 If a process’s service time exceeds one-time quantum, that process is preempted and
placed at tail of the ready queue.

26 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

The turnaround time


Turnaround time of each process is as follows:
Process Turnaround time = Terminate time – Arrival time
P1 30 – 1 = 29
P2 7–2=5
P3 10 – 3 = 7
The average turnaround time
Average wait time = (29+5+7)/3 = 13.67 ms
Practice exercise one
Consider four processes P1, P2, and P3 with their required CPU burst (in milliseconds) as shown
in the following table:
Process Arrival time CPU burst time
P1 0 25
P2 1 5
P3 2 5
Draw a Gantt chart to show the order of execution. Calculate the average waiting time and
average turnaround time. The operating system uses the Round Robin (RR) scheduling
Practice exercise two
The table below shows five processes and their respective burst times which arrive for
processing at the corresponding times
Process Arrival time CPU burst time
P1 0 5
P2 1 16
P3 2 8
P4 3 10
P5 4 4

Draw a Gantt chart to show the order of execution. Calculate the average waiting time and
average turnaround time. The operating system uses the Round Robin (RR) scheduling.

27 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

Priority scheduling
In priority scheduling, a priority (an integer) is associated with each process and the processor is
always allocated to the process with the highest priority. Priority of a process represents its
likelihood of being scheduled next. Generally, the smallest integer is considered to be the highest
priority. Equal priority processes are scheduled in First Come First serve order. Priorities are
assigned to processes by the user or the system at process creation time and may be of two types
namely:
 Static priorities: These are priorities that remain fixed i.e. they do not change easily,
they remain the same for a fixed period of time and are not responsive to changes in the
environment.
 Dynamic priorities: These are priorities that are responsive to changes in the
environment or changing need and are easy to implement.
When a scheduling selection is to be made, the scheduler starts at the highest priority ready
queue RQ0. There is one or more processes in the queue, a process is selected using some
scheduling policy. If RQ0 is empty, then RQ1 is examined, and so on as shown in the diagram
below

Figure 3: Priority scheduling queueing diagram

A common problem with priority based scheduling is the possibility that low priority processes
maybe effectively locked out by the higher priority ones. A solution to this problem is aging
priority, in which the priority of each process is gradually increased after the process spends a

28 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

certain amount of time in the system. Eventually the older process attains high priority and are
assured of processor time. This act is known as aging and its purpose is to prevent indefinite
postponement of processes.
Characteristics

 Priority scheduling is a non-preemptive algorithm and one of the most common


scheduling algorithms in batch systems.
 Each process is assigned a priority. Process with highest priority is to be executed first
and so on
 Process with some priority are executed on first come first served basis.
 Priority can be decided based on memory requirements, time requirements or any other
resource requirement.

Advantages
 The priority of a process can be selected based on memory requirement, time requirement
or user preference. For example, a high end game will have better graphics, which means
the process, which updates the screen in a game, will have higher priority to achieve
better graphics performance.
 Good response for the highest priority processes
Disadvantages
 A second scheduling algorithm is required to schedule the processes which have same
priority.
 In preemptive priority scheduling, a higher priority process can execute ahead of an
already executing lower priority process. If lower priority process keeps waiting for
higher priority processes, starvation occurs.
Class exercise one
The table below shows four processes: P0, P1, P2 and P3. Use it to calculate the wait time for
each process and eventually the average wait time. Draw a Gantt chart to show the order of
execution. Calculate the turnaround time for each process and eventually the average turnaround
time. The operating system uses priority-scheduling algorithm

29 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

Process Arrival time Execute time Priority Service time


P0 0 5 1 9
P1 1 3 2 6
P2 2 8 1 14
P3 3 6 3 0
Solution:
The wait time
Wait time of each process is as follows:

Process Wait time = Service time – Arrival time


P0 9–0=9
P1 6–1=5
P2 14– 2 = 12
P3 0– 0 = 0

The average wait time


Average wait time = (9+5+12+0)/4 = 6.5 unit time
The Gantt chart
Since the operating system is using priority-scheduling algorithm then, the order in which the
processor will be allocated to the processes is determined by the priority of each individual
process, with the process with the highest priority being allocated for the processor first, in the
case above process P0 or P2 since they have the least priority value (integer) i.e. 1 hence the
highest priority. However, processes with the same priority are executed on a first come first
served basis and their order is determined by the arrival time of each process with the process
that arrived first being allocated for the processor first, in this case process P0 since it has the
least amount of arrival time i.e. 0 as compared to that of P2 which is 2.
The amount of time a process spends in a processor is determined by its execute time. The
execute time also determines the time the execution of the next process to be allocated for the
processor begins, bearing in mind that it is only the first process to be allocated for the processor
whose execution time begins at the zero mark while all others do not. Adding the execute time
for each process to the time the previous process completed gives the time when the next process

30 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

to be allocated for the processor begins to execute and hence the Gantt chart showing the order
of execution will look like the diagram below:

1. Since the operating system uses the priority scheduling algorithm P0 or P2 will be
considered first since they have the least priority value (integer) i.e. 1 hence the highest
priority.
2. Since processes with the same priority are executed on a first come first served basis P0
will be allocated for the processor first since P0 since it has the least amount of arrival
time i.e. 0 as compared to that of P2 which is 2 and will start executing at 0 unit time. It
will spend 5 unit time in the processor (execution time or CPU burst time) to complete at
5 unit time.
3. Process P3 will be allocated the processor next since, although it has an equal priority
with process P0, it has the second least arrival time i.e. 2 and will start executing at 5 unit
time. It will spend 8 unit time in the processor (execution time or CPU burst time) to
complete at 13 unit time.
4. Process P2 will be allocated the processor next since it has the second least priority value
(integer) i.e. 2 and will start executing at 13 unit time. It will spend 3 unit time in the
processor (execution time or CPU burst time) to complete at 16 unit time.
5. Finally, process P3 will be allocated the processor last since it has the highest priority
value (integer) i.e. 3 and will start executing at 16 unit time. It will spend 6 unit time in
the processor (execution time or CPU burst time) to complete at 22 unit time.

31 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

The turnaround time


Turnaround time of each process is as follows:
Process Turnaround time = Terminate time – Arrival time
P0 5–0=5
P1 16 – 1 = 15
P2 13 – 2 = 11
P3 22 – 3 = 19
The average turnaround time
Average wait time = (5+15+11+19)/4 = 12.5 ms
Practice exercise
Consider four processes P1, P2, P3 and P4 with their required CPU burst (in milliseconds) as
shown in the following table:
Process CPU burst time Priority
P1 5 3
P2 10 2
P3 8 4
P4 3 1
Draw a Gantt chart to show the order of execution. Calculate the average waiting time and
average turnaround time. The operating system uses the priority scheduling algorithm
Highest Response Ration Next (HRRN) scheduling
Highest Response Ration Next (HRRN) attempts to correct some of the weakness in Shortest Job
Next (SJN), particularly the excessive bias against longer processes and the excessive favoritism
toward short processes. Highest Response Ration Next (HRRN) calculates dynamic priorities
according to the function
Priority = Waiting time + Service time
Service time
The process with the highest priority value is allocated for the processor first.

32 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

Class exercise one


Consider process P0 and process P1 with run time of 10 and 50 ms respectively. After each has
waited for 5 ms, their priorities are:
P0 = 5+10 = 1.5 ms P1 = 5+50 = 1.1 ms
10 50
On this basis process, P0 is allocated for the processor first since it has a high priority value i.e.
1.5 ms.
Note: As time passes, the wait time will become more significant. If P1 has been waiting for say
30 minutes, then its priority would be
P1 = 30+50 = 1.6
50
Since the service time appears in the denomination, shorter processes receives preference.
However, because the waiting time appears in the numerator, longer processes that have been
waiting will also be given favourable treatment. This technique is similar to aging and prevents
the scheduler from indefinitely postponing processes. In this technique, a process cannot be
starved since the effect of the wait time is the numerator of the priority expression will
predominate over short processes with smaller wait time.
Characteristics

 Highest Response Ration Next (HRRN) is a preemptive scheduling algorithm in which


each process’s priority is a function not only of its service time but also of its time spent
waiting for service.
 When a process obtains its priority, it runs to completion.
Advantages
 Compromises First Come First Served (FCFS) for long processes and Short Job Next
(SJN) for short processes by considering waiting and service times.
 Throughput is high.
 Good response time
Disadvantages
There is processor overhead.

33 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

Practice exercise
The table below shows four processes: P0, P1, P2 and P3. Use it to calculate the Response Ratio
for each process. Draw a Gantt chart to show the order of execution. Calculate the turnaround
time for each process and eventually the average turnaround time. The operating system uses
Highest Response Ration Next (HRRN) scheduling.
Process Arrival time Execute time Wait time Service time
P0 0 5 3 3
P1 1 3 0 0
P2 2 8 14 16
P3 3 6 5 8
Multilevel queue scheduling
Multilevel queue scheduling algorithm partitions the ready queue into separate queues. The
processes are permanently assigned to one queue, depending on their properties such as the size
of memory or the type of the process. Each queue uses a different independent scheduling
algorithm, e.g. the ready queue can be portioned into three ready queues. One ready queue for
system processes, one for interactive processes (foreground processes) and one for batch
processes (background processes).
The system processes ready queue may follow the Shortest Job First (SJF) scheduling algorithm,
the interactive processes ready queue may follow the Round Robin (RR) scheduling algorithm
while the batch processes ready queue may follow the First Come First Served (FCFS)
scheduling algorithm as shown in the diagram below

Figure 4: Multilevel queues

34 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

The idea is to separate process with different CPU burst characteristics. The process scheduler
then alternatively selects processes from each queue and assigns them to the processor based on
the algorithm assigned to the queue. The system processes ready queue has the highest priority of
all and the batch process ready queue has the lowest priority of all, each queue has absolute
priority over the lower priority queues.

This means that no process in the batch queue could run unless the queues for system processes
and interactive processes are empty. If it happens that an interactive process enters the ready
queue while a batch process is running, then the batch process is preempted.
Characteristics
 Multiple queues are maintained for processes with common characteristics.
 Each queue can have its own scheduling algorithms.
 Priorities are assigned to each queue.
Advantages
 As each type of process is in a different queue, it is easier to execute high priority
processes first without incurring additional overhead.
Disadvantges
 No process in the lower queue can be executed unless all upper queues are empty. If a
higher priority process enters the ready queue while a lower priority process is running
then the lower priority process is preempted. This leads to starvation.
Multilevel Feedback Queue Scheduling (MLFQ)
The Multilevel Feedback Queue scheduling algorithm allows a process to move between the
queues. Rather than have fixed classes of processes allocated to specific queues, each process
may start at the top level queue. A new process enters the queuing network at the tail of the
highest queue, assigned the highest priority and lowest quantum. The process progresses through
that queue in First In First Out (FIFO) order until the process obtains a processor.
If the process completes it execution, or it relinquishes the processor to wait for an I/O
completion, it exists the queuing network. If a process’s quantum expires before the process
voluntarily relinquishes the processor, the operating system preempts it, places it at the tail of the
next lower level queue, reduces it priority and increases it quantum.

35 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

As long as the process uses the full quantum provided at each level and does not complete by
then, it continues to move to the tail of the next lower level queue, its priority being reduced and
its quantum being increased as shown in the diagram below

Figure 5: Multilevel feedback queues

Usually, there is some bottom level queue through which the process circulates round robin until
it completes. The process that next gets the processor is the one at the head of the highest non-
empty queue in the multilevel feedback queue.
Since a running process is preempted by one arriving in a higher queue, a process in a lower
queue can suffer indefinite postponement, if a higher queue always contains at least one process.
Since the scheduler increases a process’s quantum size as the process moves to each lower level
queue, the longer the process has been in the queuing network, the larger the quantum it receives
each time it obtains the processor. Now, look at the diagram and explanation below to
understand it properly.

36 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

Assuming that queue 1 and 2 follow round robin with time quantum 4 and 8 respectively and queue
3 follows FCFS. One implementation of MFQS is given below:

1. When a process starts executing then it first enters queue 1.


2. In queue 1 process executes for 4 unit and if it completes in this 4 unit or it gives CPU for
I/O operation in this 4 unit than the priority of this process does not change and if it again
comes in the ready queue than it again starts its execution in Queue 1.
3. If a process in queue 1 does not complete in 4 unit then its priority gets reduced and it shifted
to queue 2.
4. Above points 2 and 3 are also true for queue 2 processes but the time quantum is 8 unit. In a
general case if a process does not complete in a time quantum than it is shifted to the lower
priority queue.
5. In the last queue, processes are scheduled in FCFS manner.
6. A process in lower priority queue can only execute only when higher priority queues are
empty.
7. A process running in the lower priority queue is interrupted by a process arriving in the higher
priority queue.
In general, a multilevel feedback queue scheduler is defined by the following parameters:
 The number of queues.
 The scheduling algorithm for each queue.
 The method used to determine when to upgrade a process to a higher-priority queue.
 The method used to determine when to demote a process to a lower-priority queue.
 The method used to determine which queue a process will enter when that process needs
service.
Characteristics
 Allows a process to move between the queues.
Advantages
 Prevents starvation of processes since a process that waits too long in a lower priority
queue may be moved to a higher priority queue.
Disadvantges
 Starvation is possible, if new jobs are continuously entering.

37 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

 Moving the process around queue produce more CPU overhead.


Class exercise
Consider four processes P1, P2, P3 and P4 with their required CPU burst (in milliseconds) and
priority queues as shown in the following table:
Process Arrival time CPU burst time Queue number
P1 0 4 1
P2 1 3 1
P3 2 8 2
P4 10 5 1
Priority of queue 1 is greater than queue 2. Queue 1 uses Round Robin (Time Quantum = 2) and
queue 2 uses FCFS. Draw a Gantt chart to show the order of execution. Calculate the turnaround
time for each process and eventually the average turnaround time. Calculate the wait time for
each process and eventually the average wait time.
The Gantt chart
Since the operating system is using Multilevel Feedback Queue scheduling algorithm then, the
order in which the processor will be allocated to the processes is determined by the priority of
each individual process, which is in turn determined by the queue the process is on and the
process with the highest priority being allocated for the processor first, in the case above process
P1 and P2 since they are queue 1 which is of high priority than queue 2.
The amount of time a process spends in a processor is determined by its execute time. The
execute time also determines the time the execution of the next process to be allocated for the
processor begins, bearing in mind that it is only the first process to be allocated for the processor
whose execution time begins at the zero mark while all others do not. Adding the execute time
for each process to the time the previous process completed gives the time when the next process
to be allocated for the processor begins to execute and hence the Gantt chart showing the order
of execution will look like the diagram below:

38 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

P1 P2 P1 P2 P3 P4 P3

4 6 15 20
0 2 7 10
1. Since the operating system uses the Multilevel Feedback Queue scheduling algorithm and
queue 1 is of a higher priority than queue 2 then processes P1 and P2 will be considered
first since they are both in queue 1 and hence have the highest priority. However, process
P1 has the least arrival time hence it is allocate for the processor before P2 and will start
executing at 0 ms. It will spend 2 ms ( allowable quantum time) in the processor
(execution time or CPU burst time) to be preempted at 2 ms since the allowable quantum
time expires.
2. Process P2 will be allocated for the processor next since, it is in queue 1 and has the
second least arrival time and will start executing at 2 ms. It will spend 2 ms ( allowable
quantum time) in the processor (execution time or CPU burst time) to be preempted at 4
ms since the allowable quantum time expires.
3. Process P1 will again be allocated for the processor to run for a second allowable quantum
and will start executing at 4 ms. It will spend 2 ms (allowable quantum time) in the
processor (execution time or CPU burst time) to complete at 6 ms since it required only
two allowable quantum i.e. 4 ms to run to completion.
4. Process P2 will again be allocated for the processor to run for a second allowable quantum
and will start executing at 6 ms. It will spend 1 ms in the processor (execution time or
CPU burst time) to complete at 7 ms since it required only one and a half allowable
quantum i.e. 3 ms to run to completion and it completes before its second quantum
expires.
5. Process P3 will be allocated for the processor next since, it is in queue 2 and there is no
process in queue 1. It will start executing at 7 ms, however after only one and a half
allowable quantum i.e. 3 ms it is preempted because process P4 arrives at queue 1 which
is of a higher priority than queue 2.

39 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

6. Process P4 will be allocated for the processor next and will start executing at 10 ms. It
will spend two and half allowable quantum i.e. 5 ms in the processor (execution time or
CPU burst time) to complete at 15 ms.
7. Finally, process P3 will be allocated for the processor and will start executing at 15 ms.
However, it had previously run for one and a half allowable quantum i.e. 3 ms and
currently requires only two and half allowable quantum i.e. 5 ms to run to completion.
Thus, It will spend 5 ms in the processor (remaining execution time or CPU burst time) to
complete at 20 ms
The turnaround time
Turnaround time of each process is as follows:
Process Turnaround time = Terminate time – Arrival time
P1 6–0=6
P2 7–1=6
P3 20 – 2 = 18
P4 15 – 10 = 5
The average turnaround time
Average wait time = (6+6+18+5)/4 = 8.75 ms
The wait time
Wait time of each process is as follows:
Process Wait time = Turnaround time – CPU burst time (Execute time)
P1 6–4=2
P2 6–3=3
P3 18 – 8 = 10
P4 5–5=0
The average wait time
Average wait time = (2+3+10+0)/4 = 3.75 ms
Practice exercise
Consider four processes P1, P2, P3 P4 and with their required CPU burst (in milliseconds) and
priority queues as shown in the following table:

40 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

Process Arrival time CPU burst time Queue number


P1 0 10 2
P2 3 7 1
P3 4 6 2
P4 12 5 1
P5 18 8 1

Priority of queue 1 is greater than queue 2. Queue 1 has a Time Quantum (TQ) of 4 and queue 2
has a Time Quantum (TQ) of 3. Draw a Gantt chart to show the order of execution. Calculate the
turnaround time for each process and eventually the average turnaround time. Calculate the wait
time for each process and eventually the average wait time.
Load balancing
Load balancing is the process of evenly distributing the workload across all processors so as to
ensure that no one processor is overworked while another underworked or idle. Scheduling
algorithms are also known as load balancing methods.
Goals of load balancing
Load balancing aims to
 Optimize resource use.
 Maximize throughput.
 Minimize response time.
 Avoid overload of any single resource.
 Avoid crashing of the system.

Types of load balancing


There are two main types load balancing namely:
 Static load balancing: This type of load balancing the load is assigned to the processors
before application runs.
 Dynamic load balancing: This type of load balancing the load is assigned to the
processors after application runs. It monitors changes on the processor workload and
redistributes the workload.
Approaches to load balancing
There are two main approaches to load balancing namely;

41 Complied by Mr. P.K Munene


Process Management - Lesson 3 – Scheduling algorithms

 Push migration: In this approach, overloaded processes in a particular processor are


pushed to another processor for processing.
 Pull migration: In this approach, overloaded processes in a particular processor are
pulled from that processor and put into another processor for processing.
References
 William Stallings (2009) Operating systems internals and design principles. Dorling
Kindersley publishing, Inc.
 H.M Deitel, P.J Deitel, D.R Choffnes (2007) Operating systems. Pearson Education, Inc.
and Dorling Kindersley publishing, Inc.
 D.M Dhamdhere (2003) Operating systems, a concept based approach. Tata McGraw Hill
Publishing Company Limited.
 Milan Milenkovic (2002) Operating systems, concepts and design. Tata McGraw Hill
Publishing Company Limited.
 A.S Tanenbaum, A.S Woodhull (2002) Operating systems design and implementation.
Pearson Education, Inc.
 Rohit Khurana (2014) Operating Systems, Vika publishing house PVT Limited.
 Iresh A.Dhotre (2009) Operating Systems, Technical Publish Pune.

42 Complied by Mr. P.K Munene

You might also like