You are on page 1of 16

Scheduling algorithms

What is CPU Scheduling?


CPU scheduling is a process/procedure which allows one process to use the CPU
while the execution of another process is on hold (in waiting state) due to
unavailability of any resource like CPU, Memory I/O etc, thereby making full use
of CPU. The aim of CPU scheduling is to make the system efficient, fast and fair.
Whenever the CPU becomes idle, the operating system must select one of the
processes in the ready queue to be executed. The selection process is carried out
by the short-term scheduler (or CPU scheduler). The scheduler selects from among
the processes in memory that are ready to execute, and allocates the CPU to one of
them.

Types of CPU Scheduling


CPU scheduling decisions may take place under the following four circumstances:

1. When a process switches from the running state to the waiting state (for I/O


request or invocation of wait for the termination of one of the child processes).
2. When a process switches from the running state to the ready state (for
example, when an interrupt occurs).
3. When a process switches from the waiting state to the ready state (for
example, completion of I/O).
4. When a process terminates

CPU Scheduling: Scheduling Criteria:


There are many different criteria to check when considering the "best" scheduling
algorithm. When we are dealing with some CPU scheduling algorithms then we
encounter with some confusing terms like CPU utilization, Burst time, Arrival
time, Exit time, Waiting time, Response time, Turnaround time, and throughput.
These parameters are used to find the performance of a system.
CPU Utilization
To make out the best use of the CPU and not to waste any CPU cycle, the CPU
would be working most of the time (Ideally 100% of the time). Considering a real
system, CPU usage should range from 40% (lightly loaded) to 90% (heavily
loaded.)
Burst time
Every process in a computer system requires some amount of time for its
execution. This time is both the CPU time and the I/O time. The CPU time is the
time taken by CPU to execute the process. While the I/O time is the time taken by
the process to perform some I/O operation. In general, we ignore the I/O time and
we consider only the CPU time for a process. So, Burst time is the total time
taken by the process for its execution on the CPU.

Arrival time
Arrival time is the time when a process enters into the ready state and is ready for
its execution.

Here in the above example, the arrival time of all the 3 processes are 0 ms, 1 ms,
and 2 ms respectively.

Exit time
Exit time is the time when a process completes its execution and exit from the
system.
Response time
Response time is the time spent when the process is in the ready state and gets the
CPU for the first time. For example, here we are using the First Come First Serve
CPU scheduling algorithm for the below 3 processes:

Here, the response time of all the 3 processes are:

 P1: 0 ms
 P2: 7 ms because the process P2 have to wait for 8 ms during the execution
of P1 and then after it will get the CPU for the first time. Also, the arrival
time of P2 is 1 ms. So, the response time will be 8-1 = 7 ms.
 P3: 13 ms because the process P3 have to wait for the execution of P1 and
P2 i.e. after 8+7 = 15 ms, the CPU will be allocated to the process P3 for
the first time. Also, the arrival of P3 is 2 ms. So, the response time for P3
will be 15-2 = 13 ms.
Response time = Time at which the process gets the CPU for the first time -
Arrival time

Waiting time
Waiting time is the total time spent by the process in the ready state waiting for
CPU. For example, consider the arrival time of all the below 3 processes to be 0
ms, 0 ms, and 2 ms and we are using the First Come First Serve scheduling
algorithm.
Then the waiting time for all the 3 processes will be:

 P1: 0 ms
 P2: 8 ms because P2 have to wait for the complete execution of P1 and
arrival time of P2 is 0 ms.
 P3: 13 ms becuase P3 will be executed after P1 and P2 i.e. after 8+7 = 15
ms and the arrival time of P3 is 2 ms. So, the waiting time of P3 will be:
15-2 = 13 ms.
Waiting time = Turnaround time - Burst time

In the above example, the processes have to wait only once. But in many other
scheduling algorithms, the CPU may be allocated to the process for some time
and then the process will be moved to the waiting state and again after some time,
the process will get the CPU and so on.

There is a difference between waiting time and response time. Response time is
the time spent between the ready state and getting the CPU for the first time. But
the waiting time is the total time taken by the process in the ready state.
Turnaround time
Turnaround time is the total amount of time spent by the process from coming in
the ready state for the first time to its completion.

Turnaround time = Burst time + Waiting time

or

Turnaround time = Exit time - Arrival time

For example, if we take the First Come First Serve scheduling algorithm, and the
order of arrival of processes is P1, P2, P3 and each process is taking 2, 5, 10
seconds. Then the turnaround time of P1 is 2 seconds because when it comes at
0th second, then the CPU is allocated to it and so the waiting time of P1 is 0 sec
and the turnaround time will be the Burst time only i.e. 2 seconds. The
turnaround time of P2 is 7 seconds because the process P2 have to wait for 2
seconds for the execution of P1 and hence the waiting time of P2 will be 2
seconds. After 2 seconds, the CPU will be given to P2 and P2 will execute its
task. So, the turnaround time will be 2+5 = 7 seconds. Similarly, the turnaround
time for P3 will be 17 seconds because the waiting time of P3 is 2+5 = 7 seconds
and the burst time of P3 is 10 seconds. So, turnaround time of P3 is 7+10 = 17
seconds.

Different CPU scheduling algorithms produce different turnaround time for the
same set of processes. This is because the waiting time of processes differ when
we change the CPU scheduling algorithm.

Throughput
Throughput is a way to find the efficiency of a CPU. It can be defined as the
number of processes executed by the CPU in a given amount of time. For
example, let's say, the process P1 takes 3 seconds for execution, P2 takes 5
seconds, and P3 takes 10 seconds. So, throughput, in this case, the throughput
will be (3+5+10)/3 = 18/3 = 6 seconds.
What is Dispatcher?
A dispatcher is a special program which comes into play after the scheduler. When
the scheduler completes its job of selecting a process, it is the dispatcher which
takes that process to the desired state/queue. The dispatcher is the module that
gives a process control over the CPU after it has been selected by the short-term
scheduler. This function involves the following:
1. Switching context
2. Switching to user mode
3. Jumping to the proper location in the user program to restart that program
Example:
There are 4 processes in the ready queue, P1, P2, P3, P4; Their arrival times are t0,
t1, t2, t3 respectively. A First in First out (FIFO) scheduling algorithm is used.
Because P1 arrived first, the scheduler will decide it is the first process that should
be executed, and the dispatcher will remove P1 from the ready queue and give it to
the CPU. The scheduler will then determine P2 to be the next process that should
be executed, so when the dispatcher returns to the queue for a new process, it will
take P2 and give it to the CPU. This continues in the same way for P3, and then P4.
Properties DISPATCHER SCHEDULER

Scheduler is something
Dispatcher is a module that gives which selects a process
control of CPU to the process among various
Definition: selected by short term scheduler processes

There are 3 types of


There are no different types in scheduler i.e. Long-
dispatcher. It is just a code term, Short-term,
Types: segment. Medium-term

Working of dispatcher is dependent Scheduler works


on scheduler. Means dispatcher independently. It works
have to wait until scheduler selects immediately when
Dependency: a process. needed
Properties DISPATCHER SCHEDULER

Scheduler works on
Dispatcher has no specific various algorithm such
Algorithm: algorithm for its implementation as FCFS, SJF, RR etc.

Time taken by scheduler


The time taken by dispatcher is is usually negligible.
Time Taken: called dispatch latency. Hence we neglect it.

Dispatcher is also responsible for:


Context Switching, Switch to user The only work of
mode, Jumping to proper location scheduler is selection of
Functions: when process again restarted processes.

Scheduling Algorithms
To decide which process to execute first and which process to execute last to
achieve maximum CPU utilization, computer scientists have defined some
algorithms are:

 First-Come, First-Served (FCFS) Scheduling


 Shortest-Job-Next (SJN) Scheduling
 Priority Scheduling
 Shortest Remaining Time
 Round Robin(RR) Scheduling
 Multiple-Level Queues Scheduling

These algorithms are either non-preemptive or preemptive.

Non-Preemptive Scheduling
Under non-preemptive scheduling, once the CPU has been allocated to a process,
the process keeps the CPU until it releases the CPU either by terminating or by
switching to the waiting state.
This scheduling method is used by the Microsoft Windows 3.1 and by the Apple
Macintosh operating systems.
It is the only method that can be used on certain hardware platforms, because It
does not require the special hardware (for example: a timer) needed for preemptive
scheduling.

Preemptive Scheduling:
In this type of Scheduling the tasks are usually assigned with priorities. At times it
is necessary to run a certain task that has a higher priority before another task
although it is running. Therefore, the running task is interrupted for some time and
resumed later when the priority task has finished its execution.
First Come First Serve (FCFS)

 Jobs are executed on first come, first serve basis.


 It is a non-preemptive, pre-emptive scheduling algorithm.
 Easy to understand and implement.
 Its implementation is based on FIFO queue.
 Poor in performance as average wait time is high.
 Example:

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

Average Wait Time: (0+4+6+13) / 4 = 5.75

Shortest Job Next (SJN) :


 This is also known as shortest job first, or SJF
 This is a non-preemptive, pre-emptive scheduling algorithm.
 Best approach to minimize waiting time.
 Easy to implement in Batch systems where required CPU time is known in
advance.
 Impossible to implement in interactive systems where required CPU time is
not known.
 The processer should know in advance how much time process will take.
Wait time of each process is as follows −

Process Wait Time : Service Time - Arrival Time

P0 3-0=3

P1 0-0=0

P2 16 - 2 = 14

P3 8-3=5

Average Wait Time: (3+0+14+5) / 4 = 5.50

Priority Based Scheduling


 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.
 Processes with same priority are executed on first come first served basis.
 Priority can be decided based on memory requirements, time requirements
or any other resource requirement.

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

Average Wait Time: (9+5+12+0) / 4 = 6.5

Shortest Remaining Time


 Shortest remaining time (SRT) is the preemptive version of the SJN
algorithm.
 The processor is allocated to the job closest to completion but it can be
preempted by a newer ready job with shorter time to completion.
 Impossible to implement in interactive systems where required CPU time is
not known.
 It is often used in batch environments where short jobs need to give
preference.

Round Robin Scheduling
 Round Robin is the preemptive process scheduling algorithm.
 Each process is provided a fix time to execute, it is called a quantum.
 Once a process is executed for a given time period, it is preempted and other
process executes for a given time period.
 Context switching is used to save states of preempted processes.

Wait time of each process is as follows −

Process Wait Time : Service Time - Arrival Time


P0 (0 - 0) + (12 - 3) = 9

P1 (3 - 1) = 2

P2 (6 - 2) + (14 - 9) + (20 - 17) = 12

P3 (9 - 3) + (17 - 12) = 11

Average Wait Time: (9+2+12+11) / 4 = 8.5


Multiple-Level Queues Scheduling
Another class of scheduling algorithms has been created for situations in which
processes are easily classified into different groups.
For example: A common division is made between foreground(or interactive)
processes and background (or batch) processes. These two types of processes have
different response-time requirements, and so might have different scheduling
needs. In addition, foreground processes may have priority over background
processes.
A multi-level queue scheduling algorithm partitions the ready queue into several
separate queues. The processes are permanently assigned to one queue, generally
based on some property of the process, such as memory size, process priority, or
process type. Each queue has its own scheduling algorithm.
For example: separate queues might be used for foreground and background
processes. The foreground queue might be scheduled by Round Robin algorithm,
while the background queue is scheduled by an FCFS algorithm.
In addition, there must be scheduling among the queues, which is commonly
implemented as fixed-priority preemptive scheduling. For example: The
foreground queue may have absolute priority over the background queue.

Multilevel Feedback Queue Scheduling


In a multilevel queue-scheduling algorithm, processes are permanently assigned to
a queue on entry to the system. Processes do not move between queues. This setup
has the advantage of low scheduling overhead, but the disadvantage of being
inflexible.
Multilevel feedback queue scheduling, however, allows a process to move between
queues. The idea is to separate processes with different CPU-burst characteristics.
If a process uses too much CPU time, it will be moved to a lower-priority queue.
Similarly, a process that waits too long in a lower-priority queue may be moved to
a higher-priority queue. This form of aging prevents starvation.

An example of a multilevel feedback queue can be seen in the below figure.

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.

The definition of a multilevel feedback queue scheduler makes it the most general
CPU-scheduling algorithm. It can be configured to match a specific system under
design. Unfortunately, it also requires some means of selecting values for all the
parameters to define the best scheduler. Although a multilevel feedback queue is
the most general scheme, it is also the most complex.

You might also like