CPU SCHEDULING
ALGORITHMS
Monday 15 July 2024 Ankur Biswas 1
SCHEDULING DECISION
• If there is only a single process to be executed by the processor, then
scheduling is not required.
• Since in multi-programming, there are multiple processes running at
all times, scheduling is used.
• Due to process switching, when a process is interrupted, there is a
need to schedule another process from the ready queue. The following
events can become the reason for scheduling:
Monday 15 July 2024 Ankur Biswas 2
SCHEDULING DECISION (CONTD.)
• When an executing process
finishes its execution and exits,
then another process is required
for execution.
• If there is no process in the ready
queue, then scheduling is done
on the job queue to select a job
and send it to the ready queue.
Monday 15 July 2024 Ankur Biswas 3
SCHEDULING DECISION (CONTD.)
• When the executing process
needs to wait for an I/O or
resource, it is blocked.
Therefore, there is a need to
select another process for
execution.
• Therefore, the scheduler is called
for scheduling another process
from the ready queue, which is
further sent for execution.
Monday 15 July 2024 Ankur Biswas 4
SCHEDULING DECISION (CONTD.)
• When an I/O or resource being
used by any process is released,
then the blocked process, waiting
for the I/O or resource, goes back
to the ready queue.
• At this moment, there is a need
to perform scheduling, in order
to give a chance to the recently
admitted process.
Monday 15 July 2024 Ankur Biswas 5
SCHEDULING DECISION (CONTD.)
• In a multi-user time-sharing
environment, a fixed time
period/time slice is allotted to
every process, so that there is a
uniform response to every
process. When a process finishes
its allotted time slice, it moves
back to the ready queue.
Monday 15 July 2024 Ankur Biswas 6
SCHEDULING DECISION (CONTD.)
• When an executing process
creates its child process, then
scheduling is performed to give
the newly created process a
chance for execution.
Monday 15 July 2024 Ankur Biswas 7
SCHEDULING DECISION (CONTD.)
• When a newly added process in
the ready queue has higher
priority compared to that of the
running process, there is need to
temporarily stop the execution of
the running process and
scheduling is performed, so that
the higher priority process gets
the chance to execute.
Monday 15 July 2024 Ankur Biswas 8
SCHEDULING DECISION (CONTD.)
• If there is an error or exception
in the process or hardware, then
the running process may need to
be stopped and sent back to the
ready queue. After this,
scheduling is performed to select
another process
Monday 15 July 2024 Ankur Biswas 9
SCHEDULING DECISION (CONTD.)
• If all the processes are waiting for any resource or I/O, there is a need
to suspend some blocked process and make space for a new process.
• In this case, all schedulers are needed.
• To suspend a blocked process, a medium-term scheduler is needed.
• Then the long-term scheduler is called to send a job from the job queue to the
ready queue.
• Finally a short-term scheduler is called to dispatch this process to the
processor.
Monday 15 July 2024 Ankur Biswas 10
SCHEDULING TYPES
• The scheduling of processes is based on two broad categories:
• Non Pre-emptive Scheduling: When a process is assigned to the
processor, it is allowed to execute to its completion, that is, a system
cannot take away the processor from the process until it exits. In that
case, it is called non pre-emptive scheduling.
• In this type of scheduling, the process may also voluntarily release the processor, for
example, when the process needs to wait for an I/O.
• Thus, a new process will be allocated to the processor, only if the running process
completes its execution or gets blocked due to a resource or I/O.
Monday 15 July 2024 Ankur Biswas 11
SCHEDULING TYPES
• Pre-emptive Scheduling: If a running process is interrupted in
between, even if it has neither voluntarily released the processor nor
exited, it is known as pre-emptive scheduling.
• In this type of scheduling, the system may stop the execution of the
running process and after that, the context switch may provide the
processor to another process.
• The interrupted process is put back into the ready queue and will be
scheduled sometime in the future, according to the scheduling policy.
Monday 15 July 2024 Ankur Biswas 12
PRE-EMPTION
• Pre-emption can be done in the following situations:
i. When a new process with higher priority, as compared to the running
process, arrives in the ready queue.
ii. When a resource or I/O is released, an interrupt occurs, and due to this, a
blocked process arrives in the ready queue.
iii. A timer interrupt occurs. The timer interrupt may be used for two purposes.
One is a periodic timer to implement a multi-user time-sharing system to
give a response to every user. The other timer interrupt may be for a process
that maliciously or accidentally holds the processor.
Monday 15 July 2024 Ankur Biswas 13
USER-BASED SCHEDULING GOALS
Turnaround Time
• The time elapsed between the submission of a job and its termination is called the
turnaround time.
• Turnaround time includes all time periods, such as time spent waiting for getting entry in
the ready queue, waiting time in the ready queue, execution time, waiting time for any
resource or I/O, and so on.
• In other words, the turnaround time of a process is:
• where is turnaround time of a process,
• is the waiting time of the process in the ready queue,
• and is the total service or execution time of the process
• The scheduling algorithm should be designed such that the turnaround time is minimal so that a
process need not wait long in the system and performs its functions on time.
Monday 15 July 2024 Ankur Biswas 14
USER-BASED SCHEDULING GOALS
Weighted or Normalized Turnaround Time
• If turnaround time is divided by the execution time of the process, it
becomes weighted or normalized turnaround time.
• Where is weighted or normalized turnaround time.
• The minimum possible value of is 1 which means that the process does not
wait and gets the execution immediately.
• Increasing values indicate the delay in getting the processor or poor service.
• Thus, should also be minimized while designing the scheduling algorithm.
Monday 15 July 2024 Ankur Biswas 15
USER-BASED SCHEDULING GOALS
Waiting Time
• A process spends its time in the ready queue and gets executed as per
the scheduling policy.
• Waiting time is the total time spent in the ready queue by a process.
• It includes all the time periods, starting from its arrival in the ready
queue to its completion.
• The scheduling policy should be such that the waiting time is less, so
that it does not wait too long in the ready queue and gets execution.
Monday 15 July 2024 Ankur Biswas 16
USER-BASED SCHEDULING GOALS
Response Time
• In multi-user and multi-tasking systems, the user processes are of
interactive nature, that is, they need attention immediately.
• Thus, response time is another important criterion from the user’s
viewpoint.
• It may be defined as the time period between the time of submission
of a process and the first response given by the process to the user.
• The scheduling algorithm must be designed such that the response
time is within an acceptable range.
Monday 15 July 2024 Ankur Biswas 17
SCHEDULING ALGORITHMS
FIRST COME FIRST SERVED (FCFS)
• There is always a simple approach to maintain a queue, that is, the item
that comes first will be served first.
• The person in first position will be served first.
• The ready queue can also be maintained with this approach.
• A ready queue is implemented as a linked list wherein the order of
arrival of the processes is maintained with the simple logic of first in,
first out (FIFO).
• Suppose there are four processes in the ready queue and they have
arrived in the order
• P1, P2, P3, and P4
Monday 15 July 2024 Ankur Biswas 18
FIRST COME FIRST SERVED (FCFS)
Monday 15 July 2024 Ankur Biswas 19
FIRST COME FIRST SERVED (FCFS)
Monday 15 July 2024 Ankur Biswas 20
FIRST COME FIRST SERVED (FCFS)
Monday 15 July 2024 Ankur Biswas 21
FIRST COME FIRST SERVED (FCFS)
Monday 15 July 2024 Ankur Biswas 22
FIRST COME FIRST SERVED (FCFS)
Monday 15 July 2024 Ankur Biswas 23
PRIORITY SCHEDULING
• In a computer system, all processes cannot be treated equally with
FCFS policy.
• In some systems like real-time systems, each process is well defined
with its functionality.
• It has a priority in the system, according to the importance of its
functionality.
• It means the arrival of the processes does not matter. If a process with
higher priority has arrived late or at its defined time,then it will be
executed first according to its priority.
Monday 15 July 2024 Ankur Biswas 24
PRIORITY SCHEDULING
Monday 15 July 2024 Ankur Biswas 25
PRIORITY SCHEDULING
Monday 15 July 2024 Ankur Biswas 26
PRIORITY SCHEDULING
Monday 15 July 2024 Ankur Biswas 27
PROBLEMS ON PROCESS SCHEDULING
• Draw the Gantt chart for the execution of the processes, showing their start and end times, using FCFS, Non-
Preemptive Priority Scheduling & Preemptive Priority Scheduling. Calculate turnaround time, normalized
turnaround time, waiting time for each process, average turnaround time, average normalized turnaround
time, and average waiting time for the system.
Process Priority Burst Time Arrival Time
P1 1 4 0
P2 2 3 0
P3 1 7 6
P4 3 4 11
P5 2 2 12
Monday 15 July 2024 Ankur Biswas 28
PROBLEMS ON PROCESS SCHEDULING
• Draw the Gantt chart for the execution of the processes, showing their start and end times, using FCFS, Non-
Preemptive Priority Scheduling & Preemptive Priority Scheduling. Calculate turnaround time, normalized
turnaround time, waiting time for each process, average turnaround time, average normalized turnaround
time, and average waiting time for the system.
Process Arrival Time Burst Time Priority
P1 0 5 2
P2 1 3 1
P3 2 6 4
P4 4 4 3
P5 6 2 5
Monday 15 July 2024 Ankur Biswas 29
SHORTEST PROCESS NEXT (SPN) OR
SHORTEST JOB FIRST (SJF)
• In this algorithm, at an instant of time, the processes are compared
based on their execution times.
• The process with the shortest execution time is executed first.
• This is a non-pre-emptive scheduling algorithm.
Monday 15 July 2024 Ankur Biswas 30
SHORTEST PROCESS NEXT (SPN) OR
SHORTEST JOB FIRST (SJF)
Monday 15 July 2024 Ankur Biswas 31
SHORTEST PROCESS NEXT (SPN) OR
SHORTEST JOB FIRST (SJF)
• Since the preference is based on the execution time of processes, the
shorter processes will be executed earlier, thereby increasing the
response time of processes.
• In comparison with FCFS, more processes will start responding and
therefore, the performance of the system, in terms of response time,
will increase.
• One problem in the implementation of this algorithm is that we must
know the execution time of every process in advance so that a
comparison of processes can be done.
• But it is not possible practically.
Monday 15 July 2024 Ankur Biswas 32
SHORTEST PROCESS NEXT (SPN) OR
SHORTEST JOB FIRST (SJF)
• Consider the following scenario of
processes:
• Draw the Gantt chart for the
execution of the processes, showing
their start time and end time, using
SPN scheduling. Calculate
turnaround time, normalized
turnaround time, waiting time for
each process and average
turnaround time, average
normalized turnaround time, and
average waiting time for the system.
Monday 15 July 2024 Ankur Biswas 33
SHORTEST REMAINING TIME NEXT (SRN)/
SHORTEST REMAINING TIME FIRST (SRF)
• This algorithm also considers the execution time of processes as in
SPN.
• But it is a pre-emptive version of SPN.
• It means, here, we can pre-empt a process based on the execution
time.
• The process with the shortest execution time will always pre-empt
other processes.
• Since the processes may not be able to complete their execution as
they may be pre-empted, the preference for pre-emption will be based
on the remaining execution time of processes.
Monday 15 July 2024 Ankur Biswas 34
SHORTEST REMAINING TIME NEXT (SRN)/
SHORTEST REMAINING TIME FIRST (SRF)
• Draw the Gantt chart for the
execution of the processes,
showing their start time and end
time, using FCFS, SPN, and SRN
scheduling. Calculate turnaround
time, normalized turnaround
time, waiting time for each
process and average turnaround
time, average normalized
turnaround time, and average
waiting time for the system.
Monday 15 July 2024 Ankur Biswas 35
SHORTEST REMAINING TIME NEXT (SRN)/
SHORTEST REMAINING TIME FIRST (SRF)
Monday 15 July 2024 Ankur Biswas 36
SHORTEST REMAINING TIME NEXT (SRN)/
SHORTEST REMAINING TIME FIRST (SRF)
Process P1 gets the execution as it arrives first. At time 1, P2 arrives,
and its execution time is less than that of P1. Therefore, P2 pre-
empts P1 and gets the execution. Similarly, P3 pre-empts P2 as the
execution time of P3 is less than that of P2. P3 completes its
execution as its execution time is less than others. At time 5, there
are two processes P2 and P4 having execution time 4. In this case,
we adopt FCFS scheduling. Therefore, P2 is given a chance to
execute. After completion of P2, P4, and then P1, get execution.
Monday 15 July 2024 Ankur Biswas 37
SHORTEST REMAINING TIME NEXT (SRN)/
SHORTEST REMAINING TIME FIRST (SRF)
Monday 15 July 2024 Ankur Biswas 38
SHORTEST REMAINING TIME NEXT (SRN)/
SHORTEST REMAINING TIME FIRST (SRF)
Monday 15 July 2024 Ankur Biswas 39
SHORTEST REMAINING TIME NEXT (SRN)/
SHORTEST REMAINING TIME FIRST (SRF)
• SRN is probably the best scheduling algorithm.
• In the above example, we can see that the average turnaround time and waiting time
are reduced as compared to FCFS, thereby, increasing the performance of the system.
• But again, we need to know in advance, the execution time of every process as in
SPN.
• Therefore, despite being the optimum scheduling algorithm, we cannot use it.
• The only solution to this problem is to estimate the execution time for a process from
its past history of execution.
• Thus, this scheduling algorithm can be used if we have past data of execution time of
the processes so that the next CPU burst of a process can be estimated.
• While calculating, the recent past values must be given high weights as compared to
older values, because recent values will reflect closer value of the actual burst time.
Monday 15 July 2024 Ankur Biswas 40
ROUND ROBIN SCHEDULING (RR)
• Since the concept of multi-user and multi-tasking systems is to share
the processor time among processes, we can design the algorithm such
that each arriving process gets the same amount of time for execution.
• If each process gets the same processor time, the response will be
equally good for all the processes, and neither the short nor long
process will suffer from starvation.
Monday 15 July 2024 Ankur Biswas 41
ROUND ROBIN SCHEDULING (RR)
Monday 15 July 2024 Ankur Biswas 42
ROUND ROBIN SCHEDULING (RR)
• Consider the following scenario of
processes with time quantum = 2:
• Draw the Gantt chart for the
execution of the processes, showing
their start time and end time, using
round-robin scheduling. Calculate
turnaround time, normalized
turnaround time, waiting time for
each process and average turnaround
time, average normalized turnaround
time, and average waiting time for
the system.
Monday 15 July 2024 Ankur Biswas 43
ROUND ROBIN SCHEDULING (RR)
Monday 15 July 2024 Ankur Biswas 44
Thank You
Monday 15 July 2024 Ankur Biswas 45