SlideShare a Scribd company logo
Program for FCFS CPU Scheduling | Set 1
Last Updated : 21 Jul, 2023


Given n processes with their burst times, the task is to find average waiting time
and average turn around time using FCFS scheduling algorithm.
First in, first out (FIFO), also known as first come, first served (FCFS), is the
simplest scheduling algorithm. FIFO simply queues processes in the order that
they arrive in the ready queue.
In this, the process that comes first will be executed first and next process starts
only after the previous gets fully executed.
Here we are considering that arrival time for all processes is 0.
How to compute below times in Round Robin using a program?
1. Completion Time: Time at which process completes its execution.
2. Turn Around Time: Time Difference between completion time and arrival
time. Turn Around Time = Completion Time – Arrival Time
3. Waiting Time(W.T): Time Difference between turn around time and burst
time.
Waiting Time = Turn Around Time – Burst Time
In this post, we have assumed arrival times as 0, so turn around and
completion times are same.
Implementation:
1- Input the processes along with their burst time (bt).
2- Find waiting time (wt) for all processes.
3- As first process that comes need not to wait so
waiting time for process 1 will be 0 i.e. wt[0] = 0.
4- Find waiting time for all other processes i.e. for
process i ->
wt[i] = bt[i-1] + wt[i-1] .
5- Find turnaround time = waiting_time + burst_time
for all processes.
6- Find average waiting time =
total_waiting_time / no_of_processes.
7- Similarly, find average turnaround time =
total_turn_around_time / no_of_processes.
Program for FCFS CPU Scheduling | Set 2
(Processes with different arrival times)
Last Updated : 13 Sep, 2023


We have already discussed FCFS Scheduling of processes with same arrival
time. In this post, scenarios, when processes have different arrival times, are
discussed. Given n processes with their burst times and arrival times, the task
is to find the average waiting time and an average turn around time using
FCFS scheduling algorithm.
FIFO simply queues processes in the order they arrive in the ready queue.
Here, the process that comes first will be executed first and next process will
start only after the previous gets fully executed.
1. Completion Time: Time at which the process completes its execution.
2. Turn Around Time: Time Difference between completion time and arrival
time. Turn Around Time = Completion Time – Arrival Time
3. Waiting Time(W.T): Time Difference between turn around time and burst
time.
Waiting Time = Turn Around Time – Burst Time.
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
Service Time: Also known as Burst Time, this is the amount of time a
process requires to complete its execution on the CPU. It represents the time
the CPU spends executing instructions of that particular process.
Waiting Time: It refers to the total amount of time that a process spends
waiting in the ready queue before it gets a chance to execute on the CPU.
Program for Shortest Job First (or SJF) CPU
Scheduling | Set 1 (Non- preemptive)
Last Updated : 24 Mar, 2023


The shortest job first (SJF) or shortest job next, is a scheduling policy that selects
the waiting process with the smallest execution time to execute next. SJN, also
known as Shortest Job Next (SJN), can be preemptive or non-preemptive.
Characteristics of SJF Scheduling:
 Shortest Job first has the advantage of having a minimum average waiting
time among all scheduling algorithms.
 It is a Greedy Algorithm.
 It may cause starvation if shorter processes keep coming. This problem can be
solved using the concept of ageing.
 It is practically infeasible as Operating System may not know burst times and
therefore may not sort them. While it is not possible to predict execution time,
several methods can be used to estimate the execution time for a job, such as
a weighted average of previous execution times.
 SJF can be used in specialized environments where accurate estimates of
running time are available.
Algorithm:
 Sort all the processes according to the arrival time.
 Then select that process that has minimum arrival time and minimum Burst
time.
 After completion of the process make a pool of processes that arrives
afterward till the completion of the previous process and select that process
among the pool which is having minimum Burst time.
Shortest Job First Scheduling Algorithm
How to compute below times in SJF using a program?
 Completion Time: Time at which process completes its execution.
 Turn Around Time: Time Difference between completion time and arrival
time.
Turn Around Time = Completion Time – Arrival Time
 Waiting Time(W.T): Time Difference between turn around time and burst
time.
Waiting Time = Turn Around Time – Burst Time
Program for Non-Preemptive Shortest Job First CPU Scheduling
Non-Preemptive Shortest Job First algorithm can be implemented using Segment
Trees data structure. For detailed implementation of Non-Preemptive Shortest
Job First scheduling algorithm, please refer: Program for Non-Preemptive
Shortest Job First CPU Scheduling.
In this post, we have assumed arrival times as 0, so turn around and
completion times are same.
Examples to show working of Non-Preemptive Shortest Job First CPU
Scheduling Algorithm:
Example-1: Consider the following table of arrival time and burst time for five
processes P1, P2, P3, P4 and P5.
Process Burst Time Arrival Time
P1 6 ms 2 ms
P2 2 ms 5 ms
P3 8 ms 1 ms
P4 3 ms 0 ms
P5 4 ms 4 ms
The Shortest Job First CPU Scheduling Algorithm will work on the basis of steps
as mentioned below:
At time = 0,
 Process P4 arrives and starts executing
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
0-1ms P4 0ms 1ms 3ms 2ms
At time= 1,
 Process P3 arrives.
 But, as P4 still needs 2 execution units to complete.
 Thus, P3 will wait till P4 gets executed.
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
1-2ms
P4 0ms 1ms 2ms 1ms
P3 1ms P3 0ms 8ms 8ms
At time =2,
 Process P1 arrives and is added to the waiting table
 P4 will continue its execution.
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
2-3ms
P4 0ms 1ms 1ms 0ms
P3 1ms P3 0ms 8ms 8ms
P1 2ms P3, P1 0ms 6ms 6ms
At time = 3,
 Process P4 will finish its execution.
 Then, the burst time of P3 and P1 is compared.
 Process P1 is executed because its burst time is less as compared to P3.
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
3-4ms P3 1ms P3 0ms 8ms 8ms
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
P1 2ms P3 1ms 6ms 5ms
At time = 4,
 Process P5 arrives and is added to the waiting Table.
 P1 will continue execution.
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
4-5ms
P3 1ms P3 0ms 8ms 8ms
P1 2ms P3 1ms 5ms 4ms
P5 4ms P3, P5 0ms 4ms 4ms
At time = 5,
 Process P2 arrives and is added to the waiting Table.
 P1 will continue execution.
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
5-6ms
P3 1ms P3 0ms 8ms 8ms
P1 2ms P3 1ms 4ms 3ms
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
P5 4ms P3, P5 0ms 4ms 4ms
P2 5ms P3, P5, P2 0ms 2ms 2ms
At time = 6,
 Process P1 will finish its execution.
 The burst time of P3, P5, and P2 is compared.
 Process P2 is executed because its burst time is the lowest among all.
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
6-9ms
P3 1ms P3 0ms 8ms 8ms
P1 2ms P3 3ms 3ms 0ms
P5 4ms P3, P5 0ms 4ms 4ms
P2 5ms P3, P5, P2 0ms 2ms 2ms
At time=9,
 Process P2 is executing and P3 and P5 are in the waiting Table.
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
9-11ms
P3 1ms P3 0ms 8ms 8ms
P5 4ms P3, P5 0ms 4ms 4ms
P2 5ms P3, P5 2ms 2ms 0ms
At time = 11,
 The execution of Process P2 will be done.
 The burst time of P3 and P5 is compared.
 Process P5 is executed because its burst time is lower than P3.
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
11-15ms
P3 1ms P3 0ms 8ms 8ms
P5 4ms P3 4ms 4ms 0ms
At time = 15,
 Process P5 will finish its execution.
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
15-23ms P3 1ms 8ms 8ms 0ms
At time = 23,
 Process P3 will finish its execution.
 The overall execution of the processes will be as shown below:
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
0-1ms P4 0ms 1ms 3ms 2ms
1-2ms
P4 0ms 1ms 2ms 1ms
P3 1ms P3 0ms 8ms 8ms
2-3ms
P4 0ms 1ms 1ms 0ms
P3 1ms P3 0ms 8ms 8ms
P1 2ms P3, P1 0ms 6ms 6ms
3-4ms
P3 1ms P3 0ms 8ms 8ms
P1 2ms P3 1ms 6ms 5ms
4-5ms
P3 1ms P3 0ms 8ms 8ms
P1 2ms P3 1ms 5ms 4ms
P5 4ms P3, P5 0ms 4ms 4ms
5-6ms P3 1ms P3 0ms 8ms 8ms
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
P1 2ms P3 1ms 4ms 3ms
P5 4ms P3, P5 0ms 4ms 4ms
P2 5ms P3, P5, P2 0ms 2ms 2ms
6-9ms
P3 1ms P3 0ms 8ms 8ms
P1 2ms P3 3ms 3ms 0ms
P5 4ms P3, P5 0ms 4ms 4ms
P2 5ms P3, P5, P2 0ms 2ms 2ms
9-11ms
P3 1ms P3 0ms 8ms 8ms
P5 4ms P3, P5 0ms 4ms 4ms
P2 5ms P3, P5 2ms 2ms 0ms
11-15ms
P3 1ms P3 0ms 8ms 8ms
P5 4ms P3 4ms 4ms 0ms
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
15-23ms P3 1ms 8ms 8ms 0ms
Gantt chart for above execution:
Gantt chart
Now, let’s calculate the average waiting time for above example:
P4 = 0 – 0 = 0
P1 = 3 – 2 = 1
P2 = 9 – 5 = 4
P5 = 11 – 4 = 7
P3 = 15 – 1 = 14
Average Waiting Time = 0 + 1 + 4 + 7 + 14/5 = 26/5 = 5.2
Advantages of SJF:
 SJF is better than the First come first serve(FCFS) algorithm as it reduces the
average waiting time.
 SJF is generally used for long term scheduling
 It is suitable for the jobs running in batches, where run times are already
known.
 SJF is probably optimal in terms of average turnaround time.
Disadvantages of SJF:
 SJF may cause very long turn-around times or starvation.
 In SJF job completion time must be known earlier, but sometimes it is hard to
predict.
 Sometimes, it is complicated to predict the length of the upcoming CPU
request.
 It leads to the starvation that does not reduce average turnaround time.
Shortest Remaining Time First (Preemptive SJF)
Scheduling Algorithm
Last Updated : 07 May, 2024


In previous post, we have discussed Set 1 of SJF i.e. non-pre-emptive. In
this post we will discuss the pre-emptive version of SJF known as Shortest
Remaining Time First (SRTF).
In the Shortest Remaining Time First (SRTF) scheduling algorithm, the
process with the smallest amount of time remaining until completion is
selected to execute. Since the currently executing process is the one with
the shortest amount of time remaining by definition, and since that time
should only reduce as execution progresses, processes will always run until
they complete or a new process is added that requires a smaller amount of
time.
Examples to show working of Pre-emptive Shortest Job First CPU
Scheduling Algorithm:
Example-1: Consider the following table of arrival time and burst time for five
processes P1, P2, P3, P4 and P5.
Process Burst Time Arrival Time
P1 6 ms 2 ms
P2 2 ms 5 ms
P3 8 ms 1 ms
P4 3 ms 0 ms
P5 4 ms 4 ms
The Shortest Job First CPU Scheduling Algorithm will work on the basis of
steps as mentioned below:
At time = 0,
 Process P4 arrives and starts executing
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
0-1ms P4 0ms 1ms 3ms 2ms
At time= 1,
 Process P3 arrives.
 But, as P4 has a shorter burst time. It will continue execution.
 Thus, P3 will wait till P4 gets executed.
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
1-2ms
P4 0ms
P3
1ms 2ms 1ms
P3 1ms 0ms 8ms 8ms
At time =2,
 Process P1 arrives with burst time = 6
 As the burst time of P1 is more than that of P4
 Thus, P4 will continue its execution.
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
2-3ms
P4 0ms
P3,
P1
1ms 1ms 0ms
P3 1ms 0ms 8ms 8ms
P1 2ms 0ms 6ms 6ms
At time = 3,
 Process P4 will finish its execution.
 Then, the burst time of P3 and P1 is compared.
 Process P1 is executed because its burst time is less as compared to P3.
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
3-4ms
P3 1ms
P
3
0ms 8ms 8ms
P1 2ms 1ms 6ms 5ms
At time = 4,
 Process P5 arrives.
 Then the burst time of P3, P5, and P1 is compared.
 Process P5 gets executed first among them because its burst time is
lowest, and process P1 is preempted.
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
4-5ms
P3 1ms
P3,
P1
0ms 8ms 8ms
P1 2ms 0ms 5ms 5ms
P5 4ms 1ms 4ms 3ms
At time = 5,
 Process P2 arrives.
 The burst time of all processes are compared,
 Process P2 gets executed as its burst time is lowest among all.
 Process P5 is preempted.
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
5-6ms
P3 1ms
P3,
P5, P1
0ms 8ms 8ms
P1 2ms 0ms 5ms 5ms
P5 4ms 0ms 3ms 3ms
P2 5ms 1ms 2ms 1ms
At time = 6,
 Process P2 will keep executing.
 It will execute till time = 7 as the burst time of P2 is 2ms
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
6-7ms
P3 1ms
P3, P5,
P1
0ms 8ms 8ms
P1 2ms 0ms 5ms 5ms
P5 4ms 0ms 3ms 3ms
P2 5ms 1ms 1ms 0ms
At time=7,
 The process P2 finishes its execution.
 Then again the burst time of all remaining processes is compared.
 The Process P5 gets executed because its burst time is lesser than the
others.
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
7-10ms
P3 1ms
P3, P1
0ms 8ms 8ms
P1 2ms 0ms 5ms 5ms
P5 4ms 3ms 3ms 0ms
At time = 10,
 The process P5 will finish its execution.
 Then the burst time of the remaining processes P1 and P3 is compared.
 Thus, process P1 is executed as its burst time is less than P3
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
10-
15ms
P3 1ms
P3
0ms 8ms 8ms
P1 4ms 4ms 5ms 0ms
At time = 15,
 The process P1 finishes its execution and P3 is the only process left.
 P3 will start executing.
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
15-
23ms
P3 1ms 8ms 8ms 0ms
At time = 23,
 Process P3 will finish its execution.
 The overall execution of the processes will be as shown below:
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
0-1ms P4 0ms 1ms 3ms 2ms
1-2ms
P4 0ms
P3
1ms 2ms 1ms
P3 1ms 0ms 8ms 8ms
2-3ms
P4 0ms
P3,
P1
1ms 1ms 0ms
P3 1ms 0ms 8ms 8ms
P1 2ms 0ms 6ms 6ms
3-4ms
P3 1ms
P3
0ms 8ms 8ms
P1 2ms 1ms 6ms 5ms
4-5ms
P3 1ms
P3,
P1
0ms 8ms 8ms
P1 2ms 0ms 5ms 5ms
P5 4ms 1ms 4ms 3ms
5-6ms
P3 1ms
P3,
P5, P1
0ms 8ms 8ms
P1 2ms 0ms 5ms 5ms
P5 4ms 0ms 3ms 3ms
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
P2 5ms 1ms 2ms 1ms
6-7ms
P3 1ms
P3,
P5, P1
0ms 8ms 8ms
P1 2ms 0ms 5ms 5ms
P5 4ms 0ms 3ms 3ms
P2 5ms 1ms 1ms 0ms
7-10ms
P3 1ms
P3, P1
0ms 8ms 8ms
P1 2ms 0ms 5ms 5ms
P5 4ms 3ms 3ms 0ms
10-
15ms
P3 1ms
P3
0ms 8ms 8ms
P1 4ms 4ms 5ms 0ms
15-
23ms
P3 1ms 8ms 8ms 0ms
Gantt chart for above execution:
Gantt chart for SRTF
Now, lets calculate average waiting time and turn around time:
As we know,
 Turn Around time = Completion time – arrival time
 Waiting Time = Turn around time – burst time
Process
Completion
Time
Turn Around
Time Waiting Time
P1 15 15-2 = 13 13-6 = 7
P2 7 7-5 = 2 2-2 = 0
P3 23 23-1 = 22 22-8 = 14
P4 3 3-0 = 3 3-3 = 0
P5 10 10-4 = 6 6-4 = 2
Now,
 Average Turn around time = (13 + 2 + 22 + 3 + 6)/5 = 9.2
 Average waiting time = (7 + 0 + 14 + 0 + 2)/5 = 23/5 = 4.6
Some of the key characteristics of SRTF
 Preemptive: SRTF is a preemptive algorithm, which means that the
currently running process can be interrupted if a new process arrives with
a shorter burst time. This helps in ensuring that the processes with the
shortest burst times are executed first.
 Dynamic: SRTF is a dynamic algorithm, which means that it can adapt to
changes in the arrival time and burst time of processes. It constantly re-
evaluates the remaining burst time of each process and schedules the
process with the shortest remaining time.
 Low waiting time: SRTF is known for its low waiting time. By selecting the
process with the shortest remaining burst time, it ensures that the
processes with the shortest burst times are executed first, which reduces
the average waiting time of processes.
 SRTF has a higher complexity than other scheduling algorithms like FCFS
(First Come First Serve) and RR (Round Robin), because it requires
frequent context switches and preemptions.
Implementation of SRTF Algorithm:
Approach:
 Traverse until all process gets completely executed.
 Find process with minimum remaining time at every single time
lap.
 Reduce its time by 1.
 Check if its remaining time becomes 0
 Increment the counter of process completion.
 Completion time of current process = current_time + 1;
 Calculate waiting time for each completed process.
 wt[i]= Completion time – arrival_time-burst_time
 Increment time lap by one.
 Find turnaround time (waiting_time + burst_time).
Shortest Remaining Time First (Preemptive SJF)
Scheduling Algorithm
Last Updated : 07 May, 2024


In previous post, we have discussed Set 1 of SJF i.e. non-pre-emptive. In
this post we will discuss the pre-emptive version of SJF known as Shortest
Remaining Time First (SRTF).
In the Shortest Remaining Time First (SRTF) scheduling algorithm, the
process with the smallest amount of time remaining until completion is
selected to execute. Since the currently executing process is the one with
the shortest amount of time remaining by definition, and since that time
should only reduce as execution progresses, processes will always run until
they complete or a new process is added that requires a smaller amount of
time.
Examples to show working of Pre-emptive Shortest Job First CPU
Scheduling Algorithm:
Example-1: Consider the following table of arrival time and burst time for five
processes P1, P2, P3, P4 and P5.
Process Burst Time Arrival Time
P1 6 ms 2 ms
P2 2 ms 5 ms
P3 8 ms 1 ms
P4 3 ms 0 ms
P5 4 ms 4 ms
The Shortest Job First CPU Scheduling Algorithm will work on the basis of
steps as mentioned below:
At time = 0,
 Process P4 arrives and starts executing
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
0-1ms P4 0ms 1ms 3ms 2ms
At time= 1,
 Process P3 arrives.
 But, as P4 has a shorter burst time. It will continue execution.
 Thus, P3 will wait till P4 gets executed.
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
1-2ms
P4 0ms
P3
1ms 2ms 1ms
P3 1ms 0ms 8ms 8ms
At time =2,
 Process P1 arrives with burst time = 6
 As the burst time of P1 is more than that of P4
 Thus, P4 will continue its execution.
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
2-3ms
P4 0ms
P3,
P1
1ms 1ms 0ms
P3 1ms 0ms 8ms 8ms
P1 2ms 0ms 6ms 6ms
At time = 3,
 Process P4 will finish its execution.
 Then, the burst time of P3 and P1 is compared.
 Process P1 is executed because its burst time is less as compared to P3.
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
3-4ms P3 1ms 0ms 8ms 8ms
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
P1 2ms
P
3
1ms 6ms 5ms
At time = 4,
 Process P5 arrives.
 Then the burst time of P3, P5, and P1 is compared.
 Process P5 gets executed first among them because its burst time is
lowest, and process P1 is preempted.
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
4-5ms
P3 1ms
P3,
P1
0ms 8ms 8ms
P1 2ms 0ms 5ms 5ms
P5 4ms 1ms 4ms 3ms
At time = 5,
 Process P2 arrives.
 The burst time of all processes are compared,
 Process P2 gets executed as its burst time is lowest among all.
 Process P5 is preempted.
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
5-6ms P3 1ms 0ms 8ms 8ms
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
P1 2ms
P3,
P5, P1
0ms 5ms 5ms
P5 4ms 0ms 3ms 3ms
P2 5ms 1ms 2ms 1ms
At time = 6,
 Process P2 will keep executing.
 It will execute till time = 7 as the burst time of P2 is 2ms
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
6-7ms
P3 1ms
P3, P5,
P1
0ms 8ms 8ms
P1 2ms 0ms 5ms 5ms
P5 4ms 0ms 3ms 3ms
P2 5ms 1ms 1ms 0ms
At time=7,
 The process P2 finishes its execution.
 Then again the burst time of all remaining processes is compared.
 The Process P5 gets executed because its burst time is lesser than the
others.
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
7-10ms
P3 1ms
P3, P1
0ms 8ms 8ms
P1 2ms 0ms 5ms 5ms
P5 4ms 3ms 3ms 0ms
At time = 10,
 The process P5 will finish its execution.
 Then the burst time of the remaining processes P1 and P3 is compared.
 Thus, process P1 is executed as its burst time is less than P3
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
10-
15ms
P3 1ms
P3
0ms 8ms 8ms
P1 4ms 4ms 5ms 0ms
At time = 15,
 The process P1 finishes its execution and P3 is the only process left.
 P3 will start executing.
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
15-
23ms
P3 1ms 8ms 8ms 0ms
At time = 23,
 Process P3 will finish its execution.
 The overall execution of the processes will be as shown below:
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
0-1ms P4 0ms 1ms 3ms 2ms
1-2ms
P4 0ms
P3
1ms 2ms 1ms
P3 1ms 0ms 8ms 8ms
2-3ms
P4 0ms
P3,
P1
1ms 1ms 0ms
P3 1ms 0ms 8ms 8ms
P1 2ms 0ms 6ms 6ms
3-4ms
P3 1ms
P3
0ms 8ms 8ms
P1 2ms 1ms 6ms 5ms
4-5ms
P3 1ms
P3,
P1
0ms 8ms 8ms
P1 2ms 0ms 5ms 5ms
P5 4ms 1ms 4ms 3ms
5-6ms
P3 1ms
P3,
P5, P1
0ms 8ms 8ms
P1 2ms 0ms 5ms 5ms
P5 4ms 0ms 3ms 3ms
Time
Instanc
e
Proces
s
Arriva
l Time
Waitin
g
Table
Executio
n Time
Initia
l
Burs
t
Time
Remainin
g Burst
Time
P2 5ms 1ms 2ms 1ms
6-7ms
P3 1ms
P3,
P5, P1
0ms 8ms 8ms
P1 2ms 0ms 5ms 5ms
P5 4ms 0ms 3ms 3ms
P2 5ms 1ms 1ms 0ms
7-10ms
P3 1ms
P3, P1
0ms 8ms 8ms
P1 2ms 0ms 5ms 5ms
P5 4ms 3ms 3ms 0ms
10-
15ms
P3 1ms
P3
0ms 8ms 8ms
P1 4ms 4ms 5ms 0ms
15-
23ms
P3 1ms 8ms 8ms 0ms
Gantt chart for above execution:
Gantt chart for SRTF
Now, lets calculate average waiting time and turn around time:
As we know,
 Turn Around time = Completion time – arrival time
 Waiting Time = Turn around time – burst time
Process
Completion
Time
Turn Around
Time Waiting Time
P1 15 15-2 = 13 13-6 = 7
P2 7 7-5 = 2 2-2 = 0
P3 23 23-1 = 22 22-8 = 14
P4 3 3-0 = 3 3-3 = 0
P5 10 10-4 = 6 6-4 = 2
Now,
 Average Turn around time = (13 + 2 + 22 + 3 + 6)/5 = 9.2
 Average waiting time = (7 + 0 + 14 + 0 + 2)/5 = 23/5 = 4.6
Some of the key characteristics of SRTF
 Preemptive: SRTF is a preemptive algorithm, which means that the
currently running process can be interrupted if a new process arrives with
a shorter burst time. This helps in ensuring that the processes with the
shortest burst times are executed first.
 Dynamic: SRTF is a dynamic algorithm, which means that it can adapt to
changes in the arrival time and burst time of processes. It constantly re-
evaluates the remaining burst time of each process and schedules the
process with the shortest remaining time.
 Low waiting time: SRTF is known for its low waiting time. By selecting the
process with the shortest remaining burst time, it ensures that the
processes with the shortest burst times are executed first, which reduces
the average waiting time of processes.
 SRTF has a higher complexity than other scheduling algorithms like FCFS
(First Come First Serve) and RR (Round Robin), because it requires
frequent context switches and preemptions.
Implementation of SRTF Algorithm:
Approach:
 Traverse until all process gets completely executed.
 Find process with minimum remaining time at every single time
lap.
 Reduce its time by 1.
 Check if its remaining time becomes 0
 Increment the counter of process completion.
 Completion time of current process = current_time + 1;
 Calculate waiting time for each completed process.
 wt[i]= Completion time – arrival_time-burst_time
 Increment time lap by one.
 Find turnaround time (waiting_time + burst_time).
Longest Remaining Time First (LRTF) CPU
Scheduling Program
Last Updated : 10 May, 2023


We have given some processes with arrival time and Burst Time and we have to
find the completion time (CT), Turn Around Time(TAT), Average Turn Around
Time (Avg TAT), Waiting Time(WT), Average Waiting Time (AWT) for the given
processes.
Prerequisite: CPU Scheduling | Longest Remaining Time First (LRTF)
algorithm
LRTF is a preemptive scheduling algorithm. Its tie-breaker is FCFS and if FCFS
does not breaks the tie then, we use process id as the tie-breaker.
Example: Consider the following table of arrival time and burst time for four
processes P1, P2, P3, and P4.
Process Arrival time Burst Time
P1 1 ms 2 ms
P2 2 ms 4 ms
P3 3 ms 6 ms
p4 4 ms 8 ms
Gantt chart will be as following below,
Since completion time (CT) can be directly determined by Gantt chart, and
Turn Around Time (TAT)
= (Completion Time) - (Arrival Time)
Also, Waiting Time (WT)
= (Turn Around Time) - (Burst Time)
Therefore,
Output:
Total Turn Around Time = 68 ms
So, Average Turn Around Time = 68/4 = 17.00 ms
And, Total Waiting Time = 48 ms
So, Average Waiting Time = 12.00 ms
Algorithm:
 Step 1: Create a structure of process containing all necessary fields like AT
(Arrival Time), BT(Burst Time), CT(Completion Time), TAT(Turn Around
Time), WT(Waiting Time).
 Step 2: Sort the processes according to their arrival time (AT).
 Step 3: Initialize the current time (time variable) as 0, and find the process
with the largest burst time (BT) among all the processes that have arrived till
now. Execute that process for each single unit, i.e., increase the time variable
by 1 and reduce the BT of that process by 1.
 Step 4: If any process completes its execution, update its CT(Completion
Time) as the current time (time variable).
 Step 5: If the BT of the executing process becomes 0, then update its CT as the
current time (time variable) and remove it from the list of processes to be
executed.
 Step 6: If all processes have been executed, exit the loop.
 Step 7: After calculating the CT for each process, find the TAT (Turn Around
Time) and WT (Waiting Time).
(TAT = CT - AT)
(WT = TAT - BT)
Implementation of Algorithm3s
Longest Remaining Time First (LRTF) or
Preemptive Longest Job First CPU Scheduling
Algorithm
Last Updated : 12 Mar, 2024


Longest Remaining Time First (LRTF) is a preemptive version of Longest Job
First (LJF) scheduling algorithm. In this scheduling algorithm, we find the
process with the maximum remaining time and then process it, i.e. check for the
maximum remaining time after some interval of time(say 1 unit each) to check if
another process having more Burst Time arrived up to that time.
Characteristics of Longest Remaining Time First (LRTF)
 Among all the processes waiting in a waiting queue, CPU is always assigned to
the process having largest burst time.
 If two processes have the same burst time then the tie is broken
using FCFS i.e. the process that arrived first is processed first.
 LJF CPU Scheduling can be of both preemptive and non-preemptive type.
Advantages of Longest Remaining Time First (LRTF)
 No other process can execute until the longest job or process executes
completely.
 All the jobs or processes finishes at the same time approximately.
Disadvantages of Longest Remaining Time First (LRTF)
 This algorithm gives very high average waiting time and average turn-around
time for a given set of processes.
 This may lead to convoy effect.
 It may happen that a short process may never get executed and the system
keeps on executing the longer processes.
 It reduces the processing speed and thus reduces the efficiency and utilization
of the system.
Longest Remaining Time First (LRTF) CPU Scheduling Algorithm
 Step-1: First, sort the processes in increasing order of their Arrival Time.
 Step-2: Choose the process having least arrival time but with most Burst
Time.
 Step-3: Then process it for 1 unit. Check if any other process arrives upto that
time of execution or not.
 Step-4: Repeat the above both steps until execute all the processes.
Examples to show working of Preemptive Longest Job First CPU Scheduling
Algorithm:
Example-1: Consider the following table of arrival time and burst time for four
processes P1, P2, P3 and P4.
Processes Arrival time Burst Time
P1 1 ms 2 ms
P2 2 ms 4 ms
P3 3 ms 6 ms
P4 4 ms 8 ms
The Longest Remaining Time First CPU Scheduling Algorithm will work on
the basis of steps as mentioned below:
At time = 1,
 Available Process : P1. So, select P1 and execute 1 ms.
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
1-2ms P1 1ms 1ms 2ms 1ms
At time = 2,
 Available Process : P1, P2.
 So, select P2 and execute 1 ms (since B.T(P1) = 1 which is less than B.T(P2) =
4)
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
2-3ms
P1 1ms
P1
0ms 1ms 1ms
P2 2ms 1ms 4ms 3ms
At time = 3,
 Available Process : P1, P2, P3.
 So, select P3 and execute 1 ms (since, B.T(P1) = 1 , B.T(P2) = 3 , B.T(P3) = 6).
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
3-4ms
P1 1ms
P1, P2
0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
P3 3ms 1ms 6ms 5ms
At time = 4,
 Available processes: P1, P2, P3, P4.
 So, select P4 (as burst time of P4 is largest) and execute 1 ms (since, B.T(P1) =
1 , B.T(P2) = 3 , B.T(P3) = 5, B.T(P4) = 8).
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
4-5ms P1 1ms 0ms 1ms 1ms
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
P2 2ms
P1, P2,
P3
0ms 3ms 3ms
P3 3ms 0ms 5ms 5ms
P4 4ms 1ms 8ms 7ms
At time = 5,
 Available processes: P1, P2, P3, P4,
 Process P4 will continue its execution as no other process has burst time
larger than the Process P4
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
5-7ms
P1 1ms
P1, P2,
P3
0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
P3 3ms 0ms 5ms 5ms
P4 4ms 2ms 7ms 5ms
At time = 7,
 The processes P3 and P4 have same remaining burst time,
 hence If two processes have the same burst time then the tie is broken
using FCFS i.e. the process that arrived first is processed first.
 Therefore P3 will get executed for 1ms
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
7-8ms
P1 1ms
P1, P2,
P4
0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
P3 3ms 1ms 5ms 4ms
P4 4ms 0ms 5ms 5ms
At time = 8,
 Available processes: P1, P2, P3, P4,
 Process P4 will again continue its execution as no other process has burst
time larger than the Process P4
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
8-9ms
P1 1ms
P1, P2,
P3
0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
P3 3ms 0ms 4ms 4ms
P4 4ms 1ms 5ms 4ms
At time = 9,
 Available processes: P1, P2, P3, P4,
 Process P3 continue its execution on the basis of FCFS rule.
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
9-10ms
P1 1ms
P1, P2,
P4
0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
P3 3ms 1ms 4ms 3ms
P4 4ms 0ms 4ms 4ms
At time = 10,
 Available processes: P1, P2, P3, P4,
 Now again the burst time of P4 is largest, thus it will execute further.
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
10-
11ms
P1 1ms
P1, P2,
P3
0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
P3 3ms 0ms 3ms 3ms
P4 4ms 1ms 4ms 3ms
At time = 11,
 Available processes: P1, P2, P3, P4,
 Process P2 will continue its execution as the burst time of P2, P3, P4 is same
 Thus, in this case the further execution will be decided on the basis of FCFS i.e.
the process that arrived first is processed first.
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
11-
12ms
P1 1ms
P1, P3,
P4
0ms 1ms 1ms
P2 2ms 1ms 3ms 2ms
P3 3ms 0ms 3ms 3ms
P4 4ms 0ms 3ms 3ms
At time = 12,
 Available processes: P1, P2, P3, P4,
 Process P3 continue its execution on the basis of above explanation.
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
12-
13ms
P1 1ms
P1, P2,
P4
0ms 1ms 1ms
P2 2ms 0ms 2ms 2ms
P3 3ms 1ms 3ms 2ms
P4 4ms 0ms 3ms 3ms
At time = 13,
 Available processes: P1, P2, P3, P4,
 Now again the burst time of P4 is largest, thus it will execute further.
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
13-
14ms
P1 1ms
P1, P2,
P3
0ms 1ms 1ms
P2 2ms 0ms 2ms 2ms
P3 3ms 0ms 2ms 2ms
P4 4ms 1ms 3ms 2ms
At time = 14,
 Available processes: P1, P2, P3, P4
 Now, the process P2 will again begin to execute first among all
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
14-
15ms
P1 1ms
P1, P3,
P4
0ms 1ms 1ms
P2 2ms 1ms 2ms 1ms
P3 3ms 0ms 2ms 2ms
P4 4ms 0ms 2ms 2ms
At time = 15,
 Available processes: P1, P2, P3, P4, now P3 will execute
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
15-
16ms
P1 1ms
P1, P2,
P4
0ms 1ms 1ms
P2 2ms 0ms 1ms 1ms
P3 3ms 1ms 2ms 1ms
P4 4ms 0ms 2ms 2ms
At time = 16,
 Available processes: P1, P2, P3, P4,
 here, P4 will execute as it has the largest Burst time among all
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
16-
17ms
P1 1ms
P1, P2,
P3
0ms 1ms 1ms
P2 2ms 0ms 1ms 1ms
P3 3ms 0ms 1ms 1ms
P4 4ms 1ms 2ms 1ms
At time = 17,
 Available processes: P1, P2, P3, P4,
 Process P1 will execute here on the basis of above explanation
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
17-
18ms
P1 1ms
P2, P3,
P4
1ms 1ms 0ms
P2 2ms 0ms 1ms 1ms
P3 3ms 0ms 1ms 1ms
P4 4ms 0ms 1ms 1ms
At time = 18,
 Available processes: P2, P3, P4,
 Process P2 will execute.
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
18-
19ms
P2 2ms
P3, P4
1ms 1ms 0ms
P3 3ms 0ms 1ms 1ms
P4 4ms 0ms 1ms 1ms
At time = 19,
 Available processes: P3, P4,
 Process P3 will execute.
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
19-
20ms
P3 3ms
P4
1ms 1ms 0ms
P4 4ms 0ms 1ms 1ms
At time = 20,
 Process P4 will execute at the end.
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
20-21ms P4 4ms 1ms 1ms 0ms
At time = 22,
 Process P4 will finish its execution.
The overall execution of the processes will be as shown below
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
1-2ms P1 1ms 1ms 2ms 1ms
2-3ms
P1 1ms
P1
0ms 1ms 1ms
P2 2ms 1ms 4ms 3ms
3-4ms P1 1ms P1, P2 0ms 1ms 1ms
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
P2 2ms 0ms 3ms 3ms
P3 3ms 1ms 6ms 5ms
4-5ms
P1 1ms
P1, P2,
P3
0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
P3 3ms 0ms 5ms 5ms
P4 4ms 1ms 8ms 7ms
5-7ms
P1 1ms
P1, P2,
P3
0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
P3 3ms 0ms 5ms 5ms
P4 4ms 2ms 7ms 5ms
7-8ms
P1 1ms
P1, P2,
P4
0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
P3 3ms 1ms 5ms 4ms
P4 4ms 0ms 7ms 5ms
8-9ms
P1 1ms
P1, P2,
P3
0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
P3 3ms 0ms 4ms 4ms
P4 4ms 1ms 5ms 4ms
9-10ms
P1 1ms
P1, P2,
P4
0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
P3 3ms 1ms 4ms 3ms
P4 4ms 0ms 4ms 4ms
10-
11ms
P1 1ms
P1, P2,
P3
0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
P3 3ms 0ms 3ms 3ms
P4 4ms 1ms 4ms 3ms
11-
12ms
P1 1ms
P1, P3,
P4
0ms 1ms 1ms
P2 2ms 1ms 3ms 2ms
P3 3ms 0ms 3ms 3ms
P4 4ms 0ms 3ms 3ms
12-
13ms
P1 1ms
P1, P2,
P4
0ms 1ms 1ms
P2 2ms 0ms 2ms 2ms
P3 3ms 1ms 3ms 2ms
P4 4ms 0ms 3ms 3ms
13-
14ms
P1 1ms
P1, P2,
P3
0ms 1ms 1ms
P2 2ms 0ms 2ms 2ms
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
P3 3ms 0ms 2ms 2ms
P4 4ms 1ms 3ms 2ms
14-
15ms
P1 1ms
P1, P3,
P4
0ms 1ms 1ms
P2 2ms 1ms 2ms 1ms
P3 3ms 0ms 2ms 2ms
P4 4ms 0ms 2ms 2ms
15-
16ms
P1 1ms
P1, P2,
P4
0ms 1ms 1ms
P2 2ms 0ms 1ms 1ms
P3 3ms 1ms 2ms 1ms
P4 4ms 0ms 2ms 2ms
16-
17ms
P1 1ms
P1, P2,
P3
0ms 1ms 1ms
P2 2ms 0ms 1ms 1ms
Time
Instance Process
Arrival
Time
Waiting
Table
Execution
Time
Initial
Burst
Time
Remaining
Burst
Time
P3 3ms 0ms 1ms 1ms
P4 4ms 1ms 2ms 1ms
17-
18ms
P1 1ms
P2, P3,
P4
1ms 1ms 0ms
P2 2ms 0ms 1ms 1ms
P3 3ms 0ms 1ms 1ms
P4 4ms 0ms 1ms 1ms
18-
19ms
P2 2ms
P3, P4
1ms 1ms 0ms
P3 3ms 0ms 1ms 1ms
P4 4ms 0ms 1ms 1ms
19-
20ms
P3 3ms
P4
1ms 1ms 0ms
P4 4ms 0ms 1ms 1ms
20-
21ms
P4 4ms 1ms 1ms 0ms
Note: CPU will be idle for 0 to 1 unit time since there is no process available in
the given interval.
Gantt chart will be as following below:
Since, completion time (C.T) can be directly determined by Gantt chart, and
Turn Around Time (TAT)
= (Completion Time) – (Arrival Time)
Also, Waiting Time (WT)
= (Turn Around Time) – (Burst Time)
Therefore, final table look like,
Total Turn Around Time = 68 ms
So, Average Turn Around Time = 68/4 = 17.00 ms
And, Total Waiting Time = 48 ms
So Average Waiting Time = 48/4 = 12.00 ms
Example-2: Consider the following table of arrival time and burst time for four
processes P1, P2, P3,P4 and P5.
Processes Arrival Time Burst Time
P1 0ms 2ms
P2 0ms 3ms
P3 2ms 2ms
Processes Arrival Time Burst Time
P4 3ms 5ms
P5 4ms 4ms
Similarly example-1, Gantt chart for this example,
Since, completion time (CT) can be directly determined by Gantt chart, and
Turn Around Time (TAT)
= (Completion Time) – (Arrival Time)
Also, Waiting Time (WT)
= (Turn Around Time) – (Burst Time)
Therefore, final table look like,
Total Turn Around Time = 61 ms
So, Average Turn Around Time = 61/5 = 12.20 ms
And, Total Waiting Time = 45 ms
So, Average Waiting Time = 45/5 = 9.00 ms
"GeeksforGeeks helped me ace the GATE exam! Whenever I had any doubt
regarding any topic, GFG always helped me and made my concepts quiet clear." -
Anshika Modi | AIR 21
Choose GeeksforGeeks as your perfect GATE 2025 Preparation partner with
these newly launched programs
GATE CS & IT
GATE DS & AI
GATE Offline (Delhi/NCR)
Over 125,000+ students already trust us to be their GATE Exam guide. Join them
& let us help you in opening the GATE to top-tech IITs & NITs!
Program for Round Robin Scheduling for the
same Arrival time
Last Updated : 06 Dec, 2023


Round Robin is a CPU scheduling algorithm where each process is cyclically
assigned a fixed time slot. It is the preemptive version of the First come First Serve
CPU Scheduling algorithm.
 Round Robin CPU Algorithm generally focuses on Time Sharing technique.
 The period of time for which a process or job is allowed to run in a pre-
emptive method is called time quantum.
 Each process or job present in the ready queue is assigned the CPU for that
time quantum, if the execution of the process is completed during that time
then the process will end else the process will go back to the waiting
table and wait for its next turn to complete the execution.
Characteristics of Round Robin CPU Scheduling Algorithm
 It is simple, easy to implement, and starvation-free as all processes get a fair
share of CPU.
 One of the most commonly used techniques in CPU scheduling is a core.
 It is preemptive as processes are assigned CPU only for a fixed slice of time at
most.
 The disadvantage of it is more overhead of context switching.
Advantages of Round Robin CPU Scheduling Algorithm
 There is fairness since every process gets an equal share of the CPU.
 The newly created process is added to the end of the ready queue.
 A round-robin scheduler generally employs time-sharing, giving each job a
time slot or quantum.
 While performing a round-robin scheduling, a particular time quantum is
allotted to different jobs.
 Each process get a chance to reschedule after a particular quantum time in
this scheduling.
Disadvantages of Round Robin CPU Scheduling Algorithm
 There is Larger waiting time and Response time.
 There is Low throughput.
 There is Context Switches.
 Gantt chart seems to come too big (if quantum time is less for scheduling. For
Example:1 ms for big scheduling.)
 Time consuming scheduling for small quantum.
Examples to show working of Round Robin Scheduling Algorithm
Example-1: Consider the following table of arrival time and burst time for four
processes P1, P2, P3, and P4 and given Time Quantum = 2
Process Burst Time Arrival Time
P1 5 ms 0 ms
P2 4 ms 1 ms
P3 2 ms 2 ms
P4 1 ms 4 ms
The Round Robin CPU Scheduling Algorithm will work on the basis of steps as
mentioned below:
At time = 0,
 The execution begins with process P1, which has burst time 5.
 Here, every process executes for 2 milliseconds (Time Quantum Period). P2
and P3 are still in the waiting queue.
Time
Instanc
e
Proces
s
Arriva
l Time
Ready
Queu
e
Runnin
g
Queue
Executio
n Time
Initia
l
Burst
Time
Remainin
g Burst
Time
0-2ms P1 0ms P2, P3 P1 2ms 5ms 3ms
At time = 2,
 The processes P1 and P3 arrives in the ready queue and P2 starts executing
for TQ period
Time
Instanc
e
Proces
s
Arriva
l Time
Ready
Queu
e
Runnin
g
Queue
Executio
n Time
Initia
l
Burst
Time
Remainin
g Burst
Time
2-4ms
P1 0ms
P3, P1 P2
0ms 3ms 3ms
P2 1ms 2ms 4ms 2ms
At time = 4,
 The process P4 arrives in the ready queue,
 Then P3 executes for TQ period.
Time
Instanc
e
Proces
s
Arriva
l Time
Ready
Queu
e
Runnin
g
Queue
Executio
n Time
Initia
l
Burst
Time
Remainin
g Burst
Time
4-6ms
P1 0ms
P1, P4,
P2
P3
0ms 3ms 3ms
P2 1ms 0ms 2ms 2ms
P3 2ms 2ms 2ms 0ms
At time = 6,
 Process P3 completes its execution
 Process P1 starts executing for TQ period as it is next in the b.
Time
Instanc
e
Proces
s
Arriva
l Time
Ready
Queu
e
Runnin
g
Queue
Executio
n Time
Initia
l
Burst
Time
Remainin
g Burst
Time
6-8ms
P1 0ms
P4, P2 P1
2ms 3ms 1ms
P2 1ms 0ms 2ms 2ms
At time = 8,
 Process P4 starts executing, it will not execute for Time Quantum period as
it has burst time = 1
 Hence, it will execute for only 1ms.
Time
Instanc
e
Proces
s
Arriva
l Time
Ready
Queu
e
Runnin
g
Queue
Executio
n Time
Initia
l
Burst
Time
Remainin
g Burst
Time
8-9ms
P1 0ms
P2, P1 P4
0ms 3ms 1ms
P2 1ms 0ms 2ms 2ms
P4 4ms 1ms 1ms 0ms
At time = 9,
 Process P4 completes its execution
 Process P2 starts executing for TQ period as it is next in the ready queue
Time
Instanc
e
Proces
s
Arriva
l Time
Ready
Queu
e
Runnin
g
Queue
Executio
n Time
Initia
l
Burst
Time
Remainin
g Burst
Time
9-11ms
P1 0ms
P1 P2
0ms 3ms 1ms
P2 1ms 2ms 2ms 0ms
At time = 11,
 Process P2 completes its execution.
 Process P1 starts executing, it will execute for 1ms only
Time
Instanc
e
Proces
s
Arriva
l Time
Ready
Queu
e
Runnin
g
Queue
Executio
n Time
Initia
l
Burst
Time
Remainin
g Burst
Time
11-
12ms
P1 0ms P1 1ms 1ms 0ms
At time = 12,
 Process P1 completes its execution.
 The overall execution of the processes will be as shown below:
Time
Instanc
e
Proces
s
Arriva
l Time
Ready
Queu
e
Runnin
g
Queue
Executio
n Time
Initia
l
Burst
Time
Remainin
g Burst
Time
0-2ms P1 0ms P2, P3 P1 2ms 5ms 3ms
2-4ms P1 0ms P3, P1 P2 0ms 3ms 3ms
Time
Instanc
e
Proces
s
Arriva
l Time
Ready
Queu
e
Runnin
g
Queue
Executio
n Time
Initia
l
Burst
Time
Remainin
g Burst
Time
P2 1ms 2ms 4ms 2ms
4-6ms
P1 0ms
P1, P4,
P2
P3
0ms 3ms 3ms
P2 1ms 0ms 2ms 2ms
P3 2ms 2ms 2ms 0ms
6-8ms
P1 0ms
P4, P2 P1
2ms 3ms 1ms
P2 1ms 0ms 2ms 2ms
8-9ms
P1 0ms
P2, P1 P4
0ms 3ms 1ms
P2 1ms 0ms 2ms 2ms
P4 4ms 1ms 1ms 0ms
9-11ms
P1 0ms
P1 P2
0ms 3ms 1ms
P2 1ms 2ms 2ms 0ms
Time
Instanc
e
Proces
s
Arriva
l Time
Ready
Queu
e
Runnin
g
Queue
Executio
n Time
Initia
l
Burst
Time
Remainin
g Burst
Time
11-
12ms
P1 0ms P1 1ms 1ms 0ms
Gantt chart will be as following below:
Gantt chart for Round Robin Scheduling Algorithm
How to compute below times in Round Robin using a program?
 Completion Time: Time at which process completes its execution.
 Turn Around Time: Time Difference between completion time and arrival
time. Turn Around Time = Completion Time – Arrival Time
 Waiting Time(W.T): Time Difference between turn around time and burst
time.
Waiting Time = Turn Around Time – Burst Time
Now, lets calculate average waiting time and turn around time:
Processes AT BT CT TAT WT
P1 0 5 12 12-0 = 12 12-5 = 7
P2 1 4 11 11-1 = 10 10-4 = 6
P3 2 2 6 6-2 = 4 4-2 = 2
P4 4 1 9 9-4 = 5 5-1 = 4
Now,
 Average Turn around time = (12 + 10 + 4 + 5)/4 = 31/4 = 7.7
 Average waiting time = (7 + 6 + 2 + 4)/4 = 19/4 = 4.7
Example 2: Consider the following table of arrival time and burst time for three
processes P1, P2 and P3 and given Time Quantum = 2
Process Burst Time Arrival Time
P1 10 ms 0 ms
P2 5 ms 0 ms
P3 8 ms 0 ms
Similarly, Gantt chart for this example:
Gantt chart for example 2
Now, lets calculate average waiting time and turn around time:
Processes AT BT CT TAT WT
P1 0 10 23 23-0 = 23 23-10 = 13
P2 0 5 15 15-0 = 15 15-5 = 10
P3 0 8 21 21-0 = 21 21-8 = 13
Total Turn Around Time = 59 ms
So, Average Turn Around Time = 59/3 = 19.667 ms
And, Total Waiting Time = 36 ms
So, Average Waiting Time = 36/3 = 12.00 ms
Program for Round Robin Scheduling with arrival time as 0 for all processes
Steps to find waiting times of all processes
 Create an array rem_bt[] to keep track of remaining burst time of processes.
This array is initially a copy of bt[] (burst times array)
 Create another array wt[] to store waiting times of processes. Initialize this
array as 0.
 Initialize time : t = 0
 Keep traversing all the processes while they are not done. Do following
for i’th process if it is not done yet.
 If rem_bt[i] > quantum
 t = t + quantum
 rem_bt[i] -= quantum;
 Else // Last cycle for this process
 t = t + rem_bt[i];
 wt[i] = t – bt[i]
 rem_bt[i] = 0; // This process is over
Once we have waiting times, we can compute turn around time tat[i] of a process
as sum of waiting and burst times, i.e., wt[i] + bt[i].
Below is implementation of above steps.
Program for FCFS CPU Scheduling.program docx

More Related Content

Similar to Program for FCFS CPU Scheduling.program docx

exp 3.docx
exp 3.docxexp 3.docx
exp 3.docx
Ganesh Chavan
 
Cpu Scheduling Galvin
Cpu Scheduling GalvinCpu Scheduling Galvin
Cpu Scheduling Galvin
Sonali Chauhan
 
Ch5
Ch5Ch5
CPU SCHEDULING IN OPERATING SYSTEMS IN DETAILED
CPU SCHEDULING IN OPERATING SYSTEMS IN DETAILEDCPU SCHEDULING IN OPERATING SYSTEMS IN DETAILED
CPU SCHEDULING IN OPERATING SYSTEMS IN DETAILED
VADAPALLYPRAVEENKUMA1
 
Scheduling algorithm (chammu)
Scheduling algorithm (chammu)Scheduling algorithm (chammu)
Scheduling algorithm (chammu)
Nagarajan
 
OS_Ch6
OS_Ch6OS_Ch6
OSCh6
OSCh6OSCh6
Cpu scheduling
Cpu schedulingCpu scheduling
Cpu scheduling
Abhijith Reloaded
 
scheduling-algorithms.ppt
scheduling-algorithms.pptscheduling-algorithms.ppt
scheduling-algorithms.ppt
KIRANJOSE23
 
scheduling-algorithms.pdf
scheduling-algorithms.pdfscheduling-algorithms.pdf
scheduling-algorithms.pdf
DeepakM509554
 
Os..
Os..Os..
Os..
pri534
 
Csc4320 chapter 5 2
Csc4320 chapter 5 2Csc4320 chapter 5 2
Csc4320 chapter 5 2
pri534
 
Scheduling algorithms
Scheduling algorithmsScheduling algorithms
Scheduling algorithms
Chankey Pathak
 
LRTF.pptx
LRTF.pptxLRTF.pptx
LRTF.pptx
SayyadHussain4
 
Preemptive process example.pptx
Preemptive process example.pptxPreemptive process example.pptx
Preemptive process example.pptx
jamilaltiti1
 
OS Process Chapter 3.pdf
OS Process Chapter 3.pdfOS Process Chapter 3.pdf
OS Process Chapter 3.pdf
Kp Sharma
 
CH06.pdf
CH06.pdfCH06.pdf
CH06.pdf
ImranKhan880955
 
Cpu scheduling
Cpu schedulingCpu scheduling
Cpu scheduling
A. S. M. Shafi
 
CPU scheduling
CPU schedulingCPU scheduling
CPU scheduling
Amir Khan
 
CPU Scheduling
CPU SchedulingCPU Scheduling
CPU Scheduling
sammerkhan1
 

Similar to Program for FCFS CPU Scheduling.program docx (20)

exp 3.docx
exp 3.docxexp 3.docx
exp 3.docx
 
Cpu Scheduling Galvin
Cpu Scheduling GalvinCpu Scheduling Galvin
Cpu Scheduling Galvin
 
Ch5
Ch5Ch5
Ch5
 
CPU SCHEDULING IN OPERATING SYSTEMS IN DETAILED
CPU SCHEDULING IN OPERATING SYSTEMS IN DETAILEDCPU SCHEDULING IN OPERATING SYSTEMS IN DETAILED
CPU SCHEDULING IN OPERATING SYSTEMS IN DETAILED
 
Scheduling algorithm (chammu)
Scheduling algorithm (chammu)Scheduling algorithm (chammu)
Scheduling algorithm (chammu)
 
OS_Ch6
OS_Ch6OS_Ch6
OS_Ch6
 
OSCh6
OSCh6OSCh6
OSCh6
 
Cpu scheduling
Cpu schedulingCpu scheduling
Cpu scheduling
 
scheduling-algorithms.ppt
scheduling-algorithms.pptscheduling-algorithms.ppt
scheduling-algorithms.ppt
 
scheduling-algorithms.pdf
scheduling-algorithms.pdfscheduling-algorithms.pdf
scheduling-algorithms.pdf
 
Os..
Os..Os..
Os..
 
Csc4320 chapter 5 2
Csc4320 chapter 5 2Csc4320 chapter 5 2
Csc4320 chapter 5 2
 
Scheduling algorithms
Scheduling algorithmsScheduling algorithms
Scheduling algorithms
 
LRTF.pptx
LRTF.pptxLRTF.pptx
LRTF.pptx
 
Preemptive process example.pptx
Preemptive process example.pptxPreemptive process example.pptx
Preemptive process example.pptx
 
OS Process Chapter 3.pdf
OS Process Chapter 3.pdfOS Process Chapter 3.pdf
OS Process Chapter 3.pdf
 
CH06.pdf
CH06.pdfCH06.pdf
CH06.pdf
 
Cpu scheduling
Cpu schedulingCpu scheduling
Cpu scheduling
 
CPU scheduling
CPU schedulingCPU scheduling
CPU scheduling
 
CPU Scheduling
CPU SchedulingCPU Scheduling
CPU Scheduling
 

Recently uploaded

2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball playEric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
enizeyimana36
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
gerogepatton
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
mamamaam477
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
NazakatAliKhoso2
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 

Recently uploaded (20)

2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball playEric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 

Program for FCFS CPU Scheduling.program docx

  • 1. Program for FCFS CPU Scheduling | Set 1 Last Updated : 21 Jul, 2023   Given n processes with their burst times, the task is to find average waiting time and average turn around time using FCFS scheduling algorithm. First in, first out (FIFO), also known as first come, first served (FCFS), is the simplest scheduling algorithm. FIFO simply queues processes in the order that they arrive in the ready queue. In this, the process that comes first will be executed first and next process starts only after the previous gets fully executed. Here we are considering that arrival time for all processes is 0. How to compute below times in Round Robin using a program? 1. Completion Time: Time at which process completes its execution. 2. Turn Around Time: Time Difference between completion time and arrival time. Turn Around Time = Completion Time – Arrival Time 3. Waiting Time(W.T): Time Difference between turn around time and burst time. Waiting Time = Turn Around Time – Burst Time In this post, we have assumed arrival times as 0, so turn around and completion times are same.
  • 2. Implementation: 1- Input the processes along with their burst time (bt). 2- Find waiting time (wt) for all processes. 3- As first process that comes need not to wait so waiting time for process 1 will be 0 i.e. wt[0] = 0. 4- Find waiting time for all other processes i.e. for process i -> wt[i] = bt[i-1] + wt[i-1] . 5- Find turnaround time = waiting_time + burst_time for all processes. 6- Find average waiting time = total_waiting_time / no_of_processes. 7- Similarly, find average turnaround time = total_turn_around_time / no_of_processes.
  • 3. Program for FCFS CPU Scheduling | Set 2 (Processes with different arrival times) Last Updated : 13 Sep, 2023   We have already discussed FCFS Scheduling of processes with same arrival time. In this post, scenarios, when processes have different arrival times, are discussed. Given n processes with their burst times and arrival times, the task is to find the average waiting time and an average turn around time using FCFS scheduling algorithm. FIFO simply queues processes in the order they arrive in the ready queue. Here, the process that comes first will be executed first and next process will start only after the previous gets fully executed. 1. Completion Time: Time at which the process completes its execution. 2. Turn Around Time: Time Difference between completion time and arrival time. Turn Around Time = Completion Time – Arrival Time 3. Waiting Time(W.T): Time Difference between turn around time and burst time. Waiting Time = Turn Around Time – Burst Time.
  • 4. 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 Service Time: Also known as Burst Time, this is the amount of time a process requires to complete its execution on the CPU. It represents the time the CPU spends executing instructions of that particular process. Waiting Time: It refers to the total amount of time that a process spends waiting in the ready queue before it gets a chance to execute on the CPU.
  • 5. Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive) Last Updated : 24 Mar, 2023   The shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next. SJN, also known as Shortest Job Next (SJN), can be preemptive or non-preemptive. Characteristics of SJF Scheduling:  Shortest Job first has the advantage of having a minimum average waiting time among all scheduling algorithms.  It is a Greedy Algorithm.  It may cause starvation if shorter processes keep coming. This problem can be solved using the concept of ageing.  It is practically infeasible as Operating System may not know burst times and therefore may not sort them. While it is not possible to predict execution time, several methods can be used to estimate the execution time for a job, such as a weighted average of previous execution times.  SJF can be used in specialized environments where accurate estimates of running time are available. Algorithm:  Sort all the processes according to the arrival time.  Then select that process that has minimum arrival time and minimum Burst time.  After completion of the process make a pool of processes that arrives afterward till the completion of the previous process and select that process among the pool which is having minimum Burst time.
  • 6. Shortest Job First Scheduling Algorithm How to compute below times in SJF using a program?  Completion Time: Time at which process completes its execution.  Turn Around Time: Time Difference between completion time and arrival time. Turn Around Time = Completion Time – Arrival Time  Waiting Time(W.T): Time Difference between turn around time and burst time. Waiting Time = Turn Around Time – Burst Time Program for Non-Preemptive Shortest Job First CPU Scheduling Non-Preemptive Shortest Job First algorithm can be implemented using Segment Trees data structure. For detailed implementation of Non-Preemptive Shortest Job First scheduling algorithm, please refer: Program for Non-Preemptive Shortest Job First CPU Scheduling. In this post, we have assumed arrival times as 0, so turn around and completion times are same. Examples to show working of Non-Preemptive Shortest Job First CPU Scheduling Algorithm: Example-1: Consider the following table of arrival time and burst time for five processes P1, P2, P3, P4 and P5.
  • 7. Process Burst Time Arrival Time P1 6 ms 2 ms P2 2 ms 5 ms P3 8 ms 1 ms P4 3 ms 0 ms P5 4 ms 4 ms The Shortest Job First CPU Scheduling Algorithm will work on the basis of steps as mentioned below: At time = 0,  Process P4 arrives and starts executing Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 0-1ms P4 0ms 1ms 3ms 2ms At time= 1,  Process P3 arrives.  But, as P4 still needs 2 execution units to complete.  Thus, P3 will wait till P4 gets executed.
  • 8. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 1-2ms P4 0ms 1ms 2ms 1ms P3 1ms P3 0ms 8ms 8ms At time =2,  Process P1 arrives and is added to the waiting table  P4 will continue its execution. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 2-3ms P4 0ms 1ms 1ms 0ms P3 1ms P3 0ms 8ms 8ms P1 2ms P3, P1 0ms 6ms 6ms At time = 3,  Process P4 will finish its execution.  Then, the burst time of P3 and P1 is compared.  Process P1 is executed because its burst time is less as compared to P3. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 3-4ms P3 1ms P3 0ms 8ms 8ms
  • 9. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time P1 2ms P3 1ms 6ms 5ms At time = 4,  Process P5 arrives and is added to the waiting Table.  P1 will continue execution. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 4-5ms P3 1ms P3 0ms 8ms 8ms P1 2ms P3 1ms 5ms 4ms P5 4ms P3, P5 0ms 4ms 4ms At time = 5,  Process P2 arrives and is added to the waiting Table.  P1 will continue execution. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 5-6ms P3 1ms P3 0ms 8ms 8ms P1 2ms P3 1ms 4ms 3ms
  • 10. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time P5 4ms P3, P5 0ms 4ms 4ms P2 5ms P3, P5, P2 0ms 2ms 2ms At time = 6,  Process P1 will finish its execution.  The burst time of P3, P5, and P2 is compared.  Process P2 is executed because its burst time is the lowest among all. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 6-9ms P3 1ms P3 0ms 8ms 8ms P1 2ms P3 3ms 3ms 0ms P5 4ms P3, P5 0ms 4ms 4ms P2 5ms P3, P5, P2 0ms 2ms 2ms At time=9,  Process P2 is executing and P3 and P5 are in the waiting Table.
  • 11. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 9-11ms P3 1ms P3 0ms 8ms 8ms P5 4ms P3, P5 0ms 4ms 4ms P2 5ms P3, P5 2ms 2ms 0ms At time = 11,  The execution of Process P2 will be done.  The burst time of P3 and P5 is compared.  Process P5 is executed because its burst time is lower than P3. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 11-15ms P3 1ms P3 0ms 8ms 8ms P5 4ms P3 4ms 4ms 0ms At time = 15,  Process P5 will finish its execution. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 15-23ms P3 1ms 8ms 8ms 0ms At time = 23,  Process P3 will finish its execution.
  • 12.  The overall execution of the processes will be as shown below: Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 0-1ms P4 0ms 1ms 3ms 2ms 1-2ms P4 0ms 1ms 2ms 1ms P3 1ms P3 0ms 8ms 8ms 2-3ms P4 0ms 1ms 1ms 0ms P3 1ms P3 0ms 8ms 8ms P1 2ms P3, P1 0ms 6ms 6ms 3-4ms P3 1ms P3 0ms 8ms 8ms P1 2ms P3 1ms 6ms 5ms 4-5ms P3 1ms P3 0ms 8ms 8ms P1 2ms P3 1ms 5ms 4ms P5 4ms P3, P5 0ms 4ms 4ms 5-6ms P3 1ms P3 0ms 8ms 8ms
  • 13. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time P1 2ms P3 1ms 4ms 3ms P5 4ms P3, P5 0ms 4ms 4ms P2 5ms P3, P5, P2 0ms 2ms 2ms 6-9ms P3 1ms P3 0ms 8ms 8ms P1 2ms P3 3ms 3ms 0ms P5 4ms P3, P5 0ms 4ms 4ms P2 5ms P3, P5, P2 0ms 2ms 2ms 9-11ms P3 1ms P3 0ms 8ms 8ms P5 4ms P3, P5 0ms 4ms 4ms P2 5ms P3, P5 2ms 2ms 0ms 11-15ms P3 1ms P3 0ms 8ms 8ms P5 4ms P3 4ms 4ms 0ms
  • 14. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 15-23ms P3 1ms 8ms 8ms 0ms Gantt chart for above execution: Gantt chart Now, let’s calculate the average waiting time for above example: P4 = 0 – 0 = 0 P1 = 3 – 2 = 1 P2 = 9 – 5 = 4 P5 = 11 – 4 = 7 P3 = 15 – 1 = 14 Average Waiting Time = 0 + 1 + 4 + 7 + 14/5 = 26/5 = 5.2 Advantages of SJF:  SJF is better than the First come first serve(FCFS) algorithm as it reduces the average waiting time.  SJF is generally used for long term scheduling  It is suitable for the jobs running in batches, where run times are already known.  SJF is probably optimal in terms of average turnaround time. Disadvantages of SJF:  SJF may cause very long turn-around times or starvation.  In SJF job completion time must be known earlier, but sometimes it is hard to predict.  Sometimes, it is complicated to predict the length of the upcoming CPU request.  It leads to the starvation that does not reduce average turnaround time.
  • 15. Shortest Remaining Time First (Preemptive SJF) Scheduling Algorithm Last Updated : 07 May, 2024   In previous post, we have discussed Set 1 of SJF i.e. non-pre-emptive. In this post we will discuss the pre-emptive version of SJF known as Shortest Remaining Time First (SRTF). In the Shortest Remaining Time First (SRTF) scheduling algorithm, the process with the smallest amount of time remaining until completion is selected to execute. Since the currently executing process is the one with the shortest amount of time remaining by definition, and since that time should only reduce as execution progresses, processes will always run until they complete or a new process is added that requires a smaller amount of time. Examples to show working of Pre-emptive Shortest Job First CPU Scheduling Algorithm: Example-1: Consider the following table of arrival time and burst time for five processes P1, P2, P3, P4 and P5. Process Burst Time Arrival Time P1 6 ms 2 ms P2 2 ms 5 ms P3 8 ms 1 ms P4 3 ms 0 ms P5 4 ms 4 ms The Shortest Job First CPU Scheduling Algorithm will work on the basis of steps as mentioned below: At time = 0,  Process P4 arrives and starts executing
  • 16. Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 0-1ms P4 0ms 1ms 3ms 2ms At time= 1,  Process P3 arrives.  But, as P4 has a shorter burst time. It will continue execution.  Thus, P3 will wait till P4 gets executed. Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 1-2ms P4 0ms P3 1ms 2ms 1ms P3 1ms 0ms 8ms 8ms At time =2,  Process P1 arrives with burst time = 6  As the burst time of P1 is more than that of P4  Thus, P4 will continue its execution. Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 2-3ms P4 0ms P3, P1 1ms 1ms 0ms P3 1ms 0ms 8ms 8ms P1 2ms 0ms 6ms 6ms At time = 3,
  • 17.  Process P4 will finish its execution.  Then, the burst time of P3 and P1 is compared.  Process P1 is executed because its burst time is less as compared to P3. Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 3-4ms P3 1ms P 3 0ms 8ms 8ms P1 2ms 1ms 6ms 5ms At time = 4,  Process P5 arrives.  Then the burst time of P3, P5, and P1 is compared.  Process P5 gets executed first among them because its burst time is lowest, and process P1 is preempted. Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 4-5ms P3 1ms P3, P1 0ms 8ms 8ms P1 2ms 0ms 5ms 5ms P5 4ms 1ms 4ms 3ms At time = 5,  Process P2 arrives.  The burst time of all processes are compared,  Process P2 gets executed as its burst time is lowest among all.  Process P5 is preempted.
  • 18. Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 5-6ms P3 1ms P3, P5, P1 0ms 8ms 8ms P1 2ms 0ms 5ms 5ms P5 4ms 0ms 3ms 3ms P2 5ms 1ms 2ms 1ms At time = 6,  Process P2 will keep executing.  It will execute till time = 7 as the burst time of P2 is 2ms Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 6-7ms P3 1ms P3, P5, P1 0ms 8ms 8ms P1 2ms 0ms 5ms 5ms P5 4ms 0ms 3ms 3ms P2 5ms 1ms 1ms 0ms At time=7,  The process P2 finishes its execution.  Then again the burst time of all remaining processes is compared.  The Process P5 gets executed because its burst time is lesser than the others.
  • 19. Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 7-10ms P3 1ms P3, P1 0ms 8ms 8ms P1 2ms 0ms 5ms 5ms P5 4ms 3ms 3ms 0ms At time = 10,  The process P5 will finish its execution.  Then the burst time of the remaining processes P1 and P3 is compared.  Thus, process P1 is executed as its burst time is less than P3 Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 10- 15ms P3 1ms P3 0ms 8ms 8ms P1 4ms 4ms 5ms 0ms At time = 15,  The process P1 finishes its execution and P3 is the only process left.  P3 will start executing. Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 15- 23ms P3 1ms 8ms 8ms 0ms At time = 23,
  • 20.  Process P3 will finish its execution.  The overall execution of the processes will be as shown below: Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 0-1ms P4 0ms 1ms 3ms 2ms 1-2ms P4 0ms P3 1ms 2ms 1ms P3 1ms 0ms 8ms 8ms 2-3ms P4 0ms P3, P1 1ms 1ms 0ms P3 1ms 0ms 8ms 8ms P1 2ms 0ms 6ms 6ms 3-4ms P3 1ms P3 0ms 8ms 8ms P1 2ms 1ms 6ms 5ms 4-5ms P3 1ms P3, P1 0ms 8ms 8ms P1 2ms 0ms 5ms 5ms P5 4ms 1ms 4ms 3ms 5-6ms P3 1ms P3, P5, P1 0ms 8ms 8ms P1 2ms 0ms 5ms 5ms P5 4ms 0ms 3ms 3ms
  • 21. Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time P2 5ms 1ms 2ms 1ms 6-7ms P3 1ms P3, P5, P1 0ms 8ms 8ms P1 2ms 0ms 5ms 5ms P5 4ms 0ms 3ms 3ms P2 5ms 1ms 1ms 0ms 7-10ms P3 1ms P3, P1 0ms 8ms 8ms P1 2ms 0ms 5ms 5ms P5 4ms 3ms 3ms 0ms 10- 15ms P3 1ms P3 0ms 8ms 8ms P1 4ms 4ms 5ms 0ms 15- 23ms P3 1ms 8ms 8ms 0ms Gantt chart for above execution: Gantt chart for SRTF Now, lets calculate average waiting time and turn around time: As we know,  Turn Around time = Completion time – arrival time
  • 22.  Waiting Time = Turn around time – burst time Process Completion Time Turn Around Time Waiting Time P1 15 15-2 = 13 13-6 = 7 P2 7 7-5 = 2 2-2 = 0 P3 23 23-1 = 22 22-8 = 14 P4 3 3-0 = 3 3-3 = 0 P5 10 10-4 = 6 6-4 = 2 Now,  Average Turn around time = (13 + 2 + 22 + 3 + 6)/5 = 9.2  Average waiting time = (7 + 0 + 14 + 0 + 2)/5 = 23/5 = 4.6 Some of the key characteristics of SRTF  Preemptive: SRTF is a preemptive algorithm, which means that the currently running process can be interrupted if a new process arrives with a shorter burst time. This helps in ensuring that the processes with the shortest burst times are executed first.  Dynamic: SRTF is a dynamic algorithm, which means that it can adapt to changes in the arrival time and burst time of processes. It constantly re- evaluates the remaining burst time of each process and schedules the process with the shortest remaining time.  Low waiting time: SRTF is known for its low waiting time. By selecting the process with the shortest remaining burst time, it ensures that the processes with the shortest burst times are executed first, which reduces the average waiting time of processes.  SRTF has a higher complexity than other scheduling algorithms like FCFS (First Come First Serve) and RR (Round Robin), because it requires frequent context switches and preemptions. Implementation of SRTF Algorithm: Approach:  Traverse until all process gets completely executed.  Find process with minimum remaining time at every single time lap.  Reduce its time by 1.  Check if its remaining time becomes 0
  • 23.  Increment the counter of process completion.  Completion time of current process = current_time + 1;  Calculate waiting time for each completed process.  wt[i]= Completion time – arrival_time-burst_time  Increment time lap by one.  Find turnaround time (waiting_time + burst_time). Shortest Remaining Time First (Preemptive SJF) Scheduling Algorithm Last Updated : 07 May, 2024   In previous post, we have discussed Set 1 of SJF i.e. non-pre-emptive. In this post we will discuss the pre-emptive version of SJF known as Shortest Remaining Time First (SRTF). In the Shortest Remaining Time First (SRTF) scheduling algorithm, the process with the smallest amount of time remaining until completion is selected to execute. Since the currently executing process is the one with the shortest amount of time remaining by definition, and since that time should only reduce as execution progresses, processes will always run until
  • 24. they complete or a new process is added that requires a smaller amount of time. Examples to show working of Pre-emptive Shortest Job First CPU Scheduling Algorithm: Example-1: Consider the following table of arrival time and burst time for five processes P1, P2, P3, P4 and P5. Process Burst Time Arrival Time P1 6 ms 2 ms P2 2 ms 5 ms P3 8 ms 1 ms P4 3 ms 0 ms P5 4 ms 4 ms The Shortest Job First CPU Scheduling Algorithm will work on the basis of steps as mentioned below: At time = 0,  Process P4 arrives and starts executing Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 0-1ms P4 0ms 1ms 3ms 2ms At time= 1,  Process P3 arrives.  But, as P4 has a shorter burst time. It will continue execution.  Thus, P3 will wait till P4 gets executed.
  • 25. Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 1-2ms P4 0ms P3 1ms 2ms 1ms P3 1ms 0ms 8ms 8ms At time =2,  Process P1 arrives with burst time = 6  As the burst time of P1 is more than that of P4  Thus, P4 will continue its execution. Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 2-3ms P4 0ms P3, P1 1ms 1ms 0ms P3 1ms 0ms 8ms 8ms P1 2ms 0ms 6ms 6ms At time = 3,  Process P4 will finish its execution.  Then, the burst time of P3 and P1 is compared.  Process P1 is executed because its burst time is less as compared to P3. Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 3-4ms P3 1ms 0ms 8ms 8ms
  • 26. Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time P1 2ms P 3 1ms 6ms 5ms At time = 4,  Process P5 arrives.  Then the burst time of P3, P5, and P1 is compared.  Process P5 gets executed first among them because its burst time is lowest, and process P1 is preempted. Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 4-5ms P3 1ms P3, P1 0ms 8ms 8ms P1 2ms 0ms 5ms 5ms P5 4ms 1ms 4ms 3ms At time = 5,  Process P2 arrives.  The burst time of all processes are compared,  Process P2 gets executed as its burst time is lowest among all.  Process P5 is preempted. Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 5-6ms P3 1ms 0ms 8ms 8ms
  • 27. Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time P1 2ms P3, P5, P1 0ms 5ms 5ms P5 4ms 0ms 3ms 3ms P2 5ms 1ms 2ms 1ms At time = 6,  Process P2 will keep executing.  It will execute till time = 7 as the burst time of P2 is 2ms Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 6-7ms P3 1ms P3, P5, P1 0ms 8ms 8ms P1 2ms 0ms 5ms 5ms P5 4ms 0ms 3ms 3ms P2 5ms 1ms 1ms 0ms At time=7,  The process P2 finishes its execution.  Then again the burst time of all remaining processes is compared.  The Process P5 gets executed because its burst time is lesser than the others.
  • 28. Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 7-10ms P3 1ms P3, P1 0ms 8ms 8ms P1 2ms 0ms 5ms 5ms P5 4ms 3ms 3ms 0ms At time = 10,  The process P5 will finish its execution.  Then the burst time of the remaining processes P1 and P3 is compared.  Thus, process P1 is executed as its burst time is less than P3 Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 10- 15ms P3 1ms P3 0ms 8ms 8ms P1 4ms 4ms 5ms 0ms At time = 15,  The process P1 finishes its execution and P3 is the only process left.  P3 will start executing. Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 15- 23ms P3 1ms 8ms 8ms 0ms At time = 23,
  • 29.  Process P3 will finish its execution.  The overall execution of the processes will be as shown below: Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time 0-1ms P4 0ms 1ms 3ms 2ms 1-2ms P4 0ms P3 1ms 2ms 1ms P3 1ms 0ms 8ms 8ms 2-3ms P4 0ms P3, P1 1ms 1ms 0ms P3 1ms 0ms 8ms 8ms P1 2ms 0ms 6ms 6ms 3-4ms P3 1ms P3 0ms 8ms 8ms P1 2ms 1ms 6ms 5ms 4-5ms P3 1ms P3, P1 0ms 8ms 8ms P1 2ms 0ms 5ms 5ms P5 4ms 1ms 4ms 3ms 5-6ms P3 1ms P3, P5, P1 0ms 8ms 8ms P1 2ms 0ms 5ms 5ms P5 4ms 0ms 3ms 3ms
  • 30. Time Instanc e Proces s Arriva l Time Waitin g Table Executio n Time Initia l Burs t Time Remainin g Burst Time P2 5ms 1ms 2ms 1ms 6-7ms P3 1ms P3, P5, P1 0ms 8ms 8ms P1 2ms 0ms 5ms 5ms P5 4ms 0ms 3ms 3ms P2 5ms 1ms 1ms 0ms 7-10ms P3 1ms P3, P1 0ms 8ms 8ms P1 2ms 0ms 5ms 5ms P5 4ms 3ms 3ms 0ms 10- 15ms P3 1ms P3 0ms 8ms 8ms P1 4ms 4ms 5ms 0ms 15- 23ms P3 1ms 8ms 8ms 0ms Gantt chart for above execution: Gantt chart for SRTF Now, lets calculate average waiting time and turn around time: As we know,  Turn Around time = Completion time – arrival time
  • 31.  Waiting Time = Turn around time – burst time Process Completion Time Turn Around Time Waiting Time P1 15 15-2 = 13 13-6 = 7 P2 7 7-5 = 2 2-2 = 0 P3 23 23-1 = 22 22-8 = 14 P4 3 3-0 = 3 3-3 = 0 P5 10 10-4 = 6 6-4 = 2 Now,  Average Turn around time = (13 + 2 + 22 + 3 + 6)/5 = 9.2  Average waiting time = (7 + 0 + 14 + 0 + 2)/5 = 23/5 = 4.6 Some of the key characteristics of SRTF  Preemptive: SRTF is a preemptive algorithm, which means that the currently running process can be interrupted if a new process arrives with a shorter burst time. This helps in ensuring that the processes with the shortest burst times are executed first.  Dynamic: SRTF is a dynamic algorithm, which means that it can adapt to changes in the arrival time and burst time of processes. It constantly re- evaluates the remaining burst time of each process and schedules the process with the shortest remaining time.  Low waiting time: SRTF is known for its low waiting time. By selecting the process with the shortest remaining burst time, it ensures that the processes with the shortest burst times are executed first, which reduces the average waiting time of processes.  SRTF has a higher complexity than other scheduling algorithms like FCFS (First Come First Serve) and RR (Round Robin), because it requires frequent context switches and preemptions. Implementation of SRTF Algorithm: Approach:  Traverse until all process gets completely executed.  Find process with minimum remaining time at every single time lap.  Reduce its time by 1.  Check if its remaining time becomes 0
  • 32.  Increment the counter of process completion.  Completion time of current process = current_time + 1;  Calculate waiting time for each completed process.  wt[i]= Completion time – arrival_time-burst_time  Increment time lap by one.  Find turnaround time (waiting_time + burst_time).
  • 33. Longest Remaining Time First (LRTF) CPU Scheduling Program Last Updated : 10 May, 2023   We have given some processes with arrival time and Burst Time and we have to find the completion time (CT), Turn Around Time(TAT), Average Turn Around Time (Avg TAT), Waiting Time(WT), Average Waiting Time (AWT) for the given processes. Prerequisite: CPU Scheduling | Longest Remaining Time First (LRTF) algorithm LRTF is a preemptive scheduling algorithm. Its tie-breaker is FCFS and if FCFS does not breaks the tie then, we use process id as the tie-breaker. Example: Consider the following table of arrival time and burst time for four processes P1, P2, P3, and P4. Process Arrival time Burst Time P1 1 ms 2 ms P2 2 ms 4 ms P3 3 ms 6 ms p4 4 ms 8 ms Gantt chart will be as following below, Since completion time (CT) can be directly determined by Gantt chart, and Turn Around Time (TAT) = (Completion Time) - (Arrival Time) Also, Waiting Time (WT) = (Turn Around Time) - (Burst Time) Therefore, Output:
  • 34. Total Turn Around Time = 68 ms So, Average Turn Around Time = 68/4 = 17.00 ms And, Total Waiting Time = 48 ms So, Average Waiting Time = 12.00 ms Algorithm:  Step 1: Create a structure of process containing all necessary fields like AT (Arrival Time), BT(Burst Time), CT(Completion Time), TAT(Turn Around Time), WT(Waiting Time).  Step 2: Sort the processes according to their arrival time (AT).  Step 3: Initialize the current time (time variable) as 0, and find the process with the largest burst time (BT) among all the processes that have arrived till now. Execute that process for each single unit, i.e., increase the time variable by 1 and reduce the BT of that process by 1.  Step 4: If any process completes its execution, update its CT(Completion Time) as the current time (time variable).  Step 5: If the BT of the executing process becomes 0, then update its CT as the current time (time variable) and remove it from the list of processes to be executed.  Step 6: If all processes have been executed, exit the loop.  Step 7: After calculating the CT for each process, find the TAT (Turn Around Time) and WT (Waiting Time). (TAT = CT - AT) (WT = TAT - BT) Implementation of Algorithm3s
  • 35. Longest Remaining Time First (LRTF) or Preemptive Longest Job First CPU Scheduling Algorithm Last Updated : 12 Mar, 2024   Longest Remaining Time First (LRTF) is a preemptive version of Longest Job First (LJF) scheduling algorithm. In this scheduling algorithm, we find the process with the maximum remaining time and then process it, i.e. check for the maximum remaining time after some interval of time(say 1 unit each) to check if another process having more Burst Time arrived up to that time. Characteristics of Longest Remaining Time First (LRTF)  Among all the processes waiting in a waiting queue, CPU is always assigned to the process having largest burst time.  If two processes have the same burst time then the tie is broken using FCFS i.e. the process that arrived first is processed first.  LJF CPU Scheduling can be of both preemptive and non-preemptive type. Advantages of Longest Remaining Time First (LRTF)  No other process can execute until the longest job or process executes completely.  All the jobs or processes finishes at the same time approximately. Disadvantages of Longest Remaining Time First (LRTF)  This algorithm gives very high average waiting time and average turn-around time for a given set of processes.  This may lead to convoy effect.  It may happen that a short process may never get executed and the system keeps on executing the longer processes.  It reduces the processing speed and thus reduces the efficiency and utilization of the system. Longest Remaining Time First (LRTF) CPU Scheduling Algorithm  Step-1: First, sort the processes in increasing order of their Arrival Time.  Step-2: Choose the process having least arrival time but with most Burst Time.  Step-3: Then process it for 1 unit. Check if any other process arrives upto that time of execution or not.
  • 36.  Step-4: Repeat the above both steps until execute all the processes. Examples to show working of Preemptive Longest Job First CPU Scheduling Algorithm: Example-1: Consider the following table of arrival time and burst time for four processes P1, P2, P3 and P4. Processes Arrival time Burst Time P1 1 ms 2 ms P2 2 ms 4 ms P3 3 ms 6 ms P4 4 ms 8 ms The Longest Remaining Time First CPU Scheduling Algorithm will work on the basis of steps as mentioned below: At time = 1,  Available Process : P1. So, select P1 and execute 1 ms. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 1-2ms P1 1ms 1ms 2ms 1ms At time = 2,  Available Process : P1, P2.  So, select P2 and execute 1 ms (since B.T(P1) = 1 which is less than B.T(P2) = 4)
  • 37. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 2-3ms P1 1ms P1 0ms 1ms 1ms P2 2ms 1ms 4ms 3ms At time = 3,  Available Process : P1, P2, P3.  So, select P3 and execute 1 ms (since, B.T(P1) = 1 , B.T(P2) = 3 , B.T(P3) = 6). Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 3-4ms P1 1ms P1, P2 0ms 1ms 1ms P2 2ms 0ms 3ms 3ms P3 3ms 1ms 6ms 5ms At time = 4,  Available processes: P1, P2, P3, P4.  So, select P4 (as burst time of P4 is largest) and execute 1 ms (since, B.T(P1) = 1 , B.T(P2) = 3 , B.T(P3) = 5, B.T(P4) = 8). Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 4-5ms P1 1ms 0ms 1ms 1ms
  • 38. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time P2 2ms P1, P2, P3 0ms 3ms 3ms P3 3ms 0ms 5ms 5ms P4 4ms 1ms 8ms 7ms At time = 5,  Available processes: P1, P2, P3, P4,  Process P4 will continue its execution as no other process has burst time larger than the Process P4 Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 5-7ms P1 1ms P1, P2, P3 0ms 1ms 1ms P2 2ms 0ms 3ms 3ms P3 3ms 0ms 5ms 5ms P4 4ms 2ms 7ms 5ms At time = 7,  The processes P3 and P4 have same remaining burst time,  hence If two processes have the same burst time then the tie is broken using FCFS i.e. the process that arrived first is processed first.  Therefore P3 will get executed for 1ms
  • 39. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 7-8ms P1 1ms P1, P2, P4 0ms 1ms 1ms P2 2ms 0ms 3ms 3ms P3 3ms 1ms 5ms 4ms P4 4ms 0ms 5ms 5ms At time = 8,  Available processes: P1, P2, P3, P4,  Process P4 will again continue its execution as no other process has burst time larger than the Process P4 Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 8-9ms P1 1ms P1, P2, P3 0ms 1ms 1ms P2 2ms 0ms 3ms 3ms P3 3ms 0ms 4ms 4ms P4 4ms 1ms 5ms 4ms At time = 9,  Available processes: P1, P2, P3, P4,  Process P3 continue its execution on the basis of FCFS rule.
  • 40. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 9-10ms P1 1ms P1, P2, P4 0ms 1ms 1ms P2 2ms 0ms 3ms 3ms P3 3ms 1ms 4ms 3ms P4 4ms 0ms 4ms 4ms At time = 10,  Available processes: P1, P2, P3, P4,  Now again the burst time of P4 is largest, thus it will execute further. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 10- 11ms P1 1ms P1, P2, P3 0ms 1ms 1ms P2 2ms 0ms 3ms 3ms P3 3ms 0ms 3ms 3ms P4 4ms 1ms 4ms 3ms At time = 11,  Available processes: P1, P2, P3, P4,  Process P2 will continue its execution as the burst time of P2, P3, P4 is same
  • 41.  Thus, in this case the further execution will be decided on the basis of FCFS i.e. the process that arrived first is processed first. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 11- 12ms P1 1ms P1, P3, P4 0ms 1ms 1ms P2 2ms 1ms 3ms 2ms P3 3ms 0ms 3ms 3ms P4 4ms 0ms 3ms 3ms At time = 12,  Available processes: P1, P2, P3, P4,  Process P3 continue its execution on the basis of above explanation. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 12- 13ms P1 1ms P1, P2, P4 0ms 1ms 1ms P2 2ms 0ms 2ms 2ms P3 3ms 1ms 3ms 2ms P4 4ms 0ms 3ms 3ms At time = 13,  Available processes: P1, P2, P3, P4,
  • 42.  Now again the burst time of P4 is largest, thus it will execute further. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 13- 14ms P1 1ms P1, P2, P3 0ms 1ms 1ms P2 2ms 0ms 2ms 2ms P3 3ms 0ms 2ms 2ms P4 4ms 1ms 3ms 2ms At time = 14,  Available processes: P1, P2, P3, P4  Now, the process P2 will again begin to execute first among all Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 14- 15ms P1 1ms P1, P3, P4 0ms 1ms 1ms P2 2ms 1ms 2ms 1ms P3 3ms 0ms 2ms 2ms P4 4ms 0ms 2ms 2ms At time = 15,  Available processes: P1, P2, P3, P4, now P3 will execute
  • 43. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 15- 16ms P1 1ms P1, P2, P4 0ms 1ms 1ms P2 2ms 0ms 1ms 1ms P3 3ms 1ms 2ms 1ms P4 4ms 0ms 2ms 2ms At time = 16,  Available processes: P1, P2, P3, P4,  here, P4 will execute as it has the largest Burst time among all Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 16- 17ms P1 1ms P1, P2, P3 0ms 1ms 1ms P2 2ms 0ms 1ms 1ms P3 3ms 0ms 1ms 1ms P4 4ms 1ms 2ms 1ms At time = 17,  Available processes: P1, P2, P3, P4,  Process P1 will execute here on the basis of above explanation
  • 44. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 17- 18ms P1 1ms P2, P3, P4 1ms 1ms 0ms P2 2ms 0ms 1ms 1ms P3 3ms 0ms 1ms 1ms P4 4ms 0ms 1ms 1ms At time = 18,  Available processes: P2, P3, P4,  Process P2 will execute. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 18- 19ms P2 2ms P3, P4 1ms 1ms 0ms P3 3ms 0ms 1ms 1ms P4 4ms 0ms 1ms 1ms At time = 19,  Available processes: P3, P4,  Process P3 will execute.
  • 45. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 19- 20ms P3 3ms P4 1ms 1ms 0ms P4 4ms 0ms 1ms 1ms At time = 20,  Process P4 will execute at the end. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 20-21ms P4 4ms 1ms 1ms 0ms At time = 22,  Process P4 will finish its execution. The overall execution of the processes will be as shown below Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time 1-2ms P1 1ms 1ms 2ms 1ms 2-3ms P1 1ms P1 0ms 1ms 1ms P2 2ms 1ms 4ms 3ms 3-4ms P1 1ms P1, P2 0ms 1ms 1ms
  • 46. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time P2 2ms 0ms 3ms 3ms P3 3ms 1ms 6ms 5ms 4-5ms P1 1ms P1, P2, P3 0ms 1ms 1ms P2 2ms 0ms 3ms 3ms P3 3ms 0ms 5ms 5ms P4 4ms 1ms 8ms 7ms 5-7ms P1 1ms P1, P2, P3 0ms 1ms 1ms P2 2ms 0ms 3ms 3ms P3 3ms 0ms 5ms 5ms P4 4ms 2ms 7ms 5ms 7-8ms P1 1ms P1, P2, P4 0ms 1ms 1ms P2 2ms 0ms 3ms 3ms
  • 47. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time P3 3ms 1ms 5ms 4ms P4 4ms 0ms 7ms 5ms 8-9ms P1 1ms P1, P2, P3 0ms 1ms 1ms P2 2ms 0ms 3ms 3ms P3 3ms 0ms 4ms 4ms P4 4ms 1ms 5ms 4ms 9-10ms P1 1ms P1, P2, P4 0ms 1ms 1ms P2 2ms 0ms 3ms 3ms P3 3ms 1ms 4ms 3ms P4 4ms 0ms 4ms 4ms 10- 11ms P1 1ms P1, P2, P3 0ms 1ms 1ms P2 2ms 0ms 3ms 3ms
  • 48. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time P3 3ms 0ms 3ms 3ms P4 4ms 1ms 4ms 3ms 11- 12ms P1 1ms P1, P3, P4 0ms 1ms 1ms P2 2ms 1ms 3ms 2ms P3 3ms 0ms 3ms 3ms P4 4ms 0ms 3ms 3ms 12- 13ms P1 1ms P1, P2, P4 0ms 1ms 1ms P2 2ms 0ms 2ms 2ms P3 3ms 1ms 3ms 2ms P4 4ms 0ms 3ms 3ms 13- 14ms P1 1ms P1, P2, P3 0ms 1ms 1ms P2 2ms 0ms 2ms 2ms
  • 49. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time P3 3ms 0ms 2ms 2ms P4 4ms 1ms 3ms 2ms 14- 15ms P1 1ms P1, P3, P4 0ms 1ms 1ms P2 2ms 1ms 2ms 1ms P3 3ms 0ms 2ms 2ms P4 4ms 0ms 2ms 2ms 15- 16ms P1 1ms P1, P2, P4 0ms 1ms 1ms P2 2ms 0ms 1ms 1ms P3 3ms 1ms 2ms 1ms P4 4ms 0ms 2ms 2ms 16- 17ms P1 1ms P1, P2, P3 0ms 1ms 1ms P2 2ms 0ms 1ms 1ms
  • 50. Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst Time P3 3ms 0ms 1ms 1ms P4 4ms 1ms 2ms 1ms 17- 18ms P1 1ms P2, P3, P4 1ms 1ms 0ms P2 2ms 0ms 1ms 1ms P3 3ms 0ms 1ms 1ms P4 4ms 0ms 1ms 1ms 18- 19ms P2 2ms P3, P4 1ms 1ms 0ms P3 3ms 0ms 1ms 1ms P4 4ms 0ms 1ms 1ms 19- 20ms P3 3ms P4 1ms 1ms 0ms P4 4ms 0ms 1ms 1ms 20- 21ms P4 4ms 1ms 1ms 0ms
  • 51. Note: CPU will be idle for 0 to 1 unit time since there is no process available in the given interval. Gantt chart will be as following below: Since, completion time (C.T) can be directly determined by Gantt chart, and Turn Around Time (TAT) = (Completion Time) – (Arrival Time) Also, Waiting Time (WT) = (Turn Around Time) – (Burst Time) Therefore, final table look like, Total Turn Around Time = 68 ms So, Average Turn Around Time = 68/4 = 17.00 ms And, Total Waiting Time = 48 ms So Average Waiting Time = 48/4 = 12.00 ms Example-2: Consider the following table of arrival time and burst time for four processes P1, P2, P3,P4 and P5. Processes Arrival Time Burst Time P1 0ms 2ms P2 0ms 3ms P3 2ms 2ms
  • 52. Processes Arrival Time Burst Time P4 3ms 5ms P5 4ms 4ms Similarly example-1, Gantt chart for this example, Since, completion time (CT) can be directly determined by Gantt chart, and Turn Around Time (TAT) = (Completion Time) – (Arrival Time) Also, Waiting Time (WT) = (Turn Around Time) – (Burst Time) Therefore, final table look like, Total Turn Around Time = 61 ms So, Average Turn Around Time = 61/5 = 12.20 ms And, Total Waiting Time = 45 ms So, Average Waiting Time = 45/5 = 9.00 ms "GeeksforGeeks helped me ace the GATE exam! Whenever I had any doubt regarding any topic, GFG always helped me and made my concepts quiet clear." - Anshika Modi | AIR 21 Choose GeeksforGeeks as your perfect GATE 2025 Preparation partner with
  • 53. these newly launched programs GATE CS & IT GATE DS & AI GATE Offline (Delhi/NCR) Over 125,000+ students already trust us to be their GATE Exam guide. Join them & let us help you in opening the GATE to top-tech IITs & NITs!
  • 54. Program for Round Robin Scheduling for the same Arrival time Last Updated : 06 Dec, 2023   Round Robin is a CPU scheduling algorithm where each process is cyclically assigned a fixed time slot. It is the preemptive version of the First come First Serve CPU Scheduling algorithm.  Round Robin CPU Algorithm generally focuses on Time Sharing technique.  The period of time for which a process or job is allowed to run in a pre- emptive method is called time quantum.  Each process or job present in the ready queue is assigned the CPU for that time quantum, if the execution of the process is completed during that time then the process will end else the process will go back to the waiting table and wait for its next turn to complete the execution. Characteristics of Round Robin CPU Scheduling Algorithm  It is simple, easy to implement, and starvation-free as all processes get a fair share of CPU.  One of the most commonly used techniques in CPU scheduling is a core.  It is preemptive as processes are assigned CPU only for a fixed slice of time at most.  The disadvantage of it is more overhead of context switching. Advantages of Round Robin CPU Scheduling Algorithm  There is fairness since every process gets an equal share of the CPU.  The newly created process is added to the end of the ready queue.  A round-robin scheduler generally employs time-sharing, giving each job a time slot or quantum.  While performing a round-robin scheduling, a particular time quantum is allotted to different jobs.  Each process get a chance to reschedule after a particular quantum time in this scheduling. Disadvantages of Round Robin CPU Scheduling Algorithm  There is Larger waiting time and Response time.  There is Low throughput.  There is Context Switches.  Gantt chart seems to come too big (if quantum time is less for scheduling. For Example:1 ms for big scheduling.)  Time consuming scheduling for small quantum.
  • 55. Examples to show working of Round Robin Scheduling Algorithm Example-1: Consider the following table of arrival time and burst time for four processes P1, P2, P3, and P4 and given Time Quantum = 2 Process Burst Time Arrival Time P1 5 ms 0 ms P2 4 ms 1 ms P3 2 ms 2 ms P4 1 ms 4 ms The Round Robin CPU Scheduling Algorithm will work on the basis of steps as mentioned below: At time = 0,  The execution begins with process P1, which has burst time 5.  Here, every process executes for 2 milliseconds (Time Quantum Period). P2 and P3 are still in the waiting queue. Time Instanc e Proces s Arriva l Time Ready Queu e Runnin g Queue Executio n Time Initia l Burst Time Remainin g Burst Time 0-2ms P1 0ms P2, P3 P1 2ms 5ms 3ms At time = 2,  The processes P1 and P3 arrives in the ready queue and P2 starts executing for TQ period
  • 56. Time Instanc e Proces s Arriva l Time Ready Queu e Runnin g Queue Executio n Time Initia l Burst Time Remainin g Burst Time 2-4ms P1 0ms P3, P1 P2 0ms 3ms 3ms P2 1ms 2ms 4ms 2ms At time = 4,  The process P4 arrives in the ready queue,  Then P3 executes for TQ period. Time Instanc e Proces s Arriva l Time Ready Queu e Runnin g Queue Executio n Time Initia l Burst Time Remainin g Burst Time 4-6ms P1 0ms P1, P4, P2 P3 0ms 3ms 3ms P2 1ms 0ms 2ms 2ms P3 2ms 2ms 2ms 0ms At time = 6,  Process P3 completes its execution  Process P1 starts executing for TQ period as it is next in the b.
  • 57. Time Instanc e Proces s Arriva l Time Ready Queu e Runnin g Queue Executio n Time Initia l Burst Time Remainin g Burst Time 6-8ms P1 0ms P4, P2 P1 2ms 3ms 1ms P2 1ms 0ms 2ms 2ms At time = 8,  Process P4 starts executing, it will not execute for Time Quantum period as it has burst time = 1  Hence, it will execute for only 1ms. Time Instanc e Proces s Arriva l Time Ready Queu e Runnin g Queue Executio n Time Initia l Burst Time Remainin g Burst Time 8-9ms P1 0ms P2, P1 P4 0ms 3ms 1ms P2 1ms 0ms 2ms 2ms P4 4ms 1ms 1ms 0ms At time = 9,  Process P4 completes its execution  Process P2 starts executing for TQ period as it is next in the ready queue
  • 58. Time Instanc e Proces s Arriva l Time Ready Queu e Runnin g Queue Executio n Time Initia l Burst Time Remainin g Burst Time 9-11ms P1 0ms P1 P2 0ms 3ms 1ms P2 1ms 2ms 2ms 0ms At time = 11,  Process P2 completes its execution.  Process P1 starts executing, it will execute for 1ms only Time Instanc e Proces s Arriva l Time Ready Queu e Runnin g Queue Executio n Time Initia l Burst Time Remainin g Burst Time 11- 12ms P1 0ms P1 1ms 1ms 0ms At time = 12,  Process P1 completes its execution.  The overall execution of the processes will be as shown below: Time Instanc e Proces s Arriva l Time Ready Queu e Runnin g Queue Executio n Time Initia l Burst Time Remainin g Burst Time 0-2ms P1 0ms P2, P3 P1 2ms 5ms 3ms 2-4ms P1 0ms P3, P1 P2 0ms 3ms 3ms
  • 59. Time Instanc e Proces s Arriva l Time Ready Queu e Runnin g Queue Executio n Time Initia l Burst Time Remainin g Burst Time P2 1ms 2ms 4ms 2ms 4-6ms P1 0ms P1, P4, P2 P3 0ms 3ms 3ms P2 1ms 0ms 2ms 2ms P3 2ms 2ms 2ms 0ms 6-8ms P1 0ms P4, P2 P1 2ms 3ms 1ms P2 1ms 0ms 2ms 2ms 8-9ms P1 0ms P2, P1 P4 0ms 3ms 1ms P2 1ms 0ms 2ms 2ms P4 4ms 1ms 1ms 0ms 9-11ms P1 0ms P1 P2 0ms 3ms 1ms P2 1ms 2ms 2ms 0ms
  • 60. Time Instanc e Proces s Arriva l Time Ready Queu e Runnin g Queue Executio n Time Initia l Burst Time Remainin g Burst Time 11- 12ms P1 0ms P1 1ms 1ms 0ms Gantt chart will be as following below: Gantt chart for Round Robin Scheduling Algorithm How to compute below times in Round Robin using a program?  Completion Time: Time at which process completes its execution.  Turn Around Time: Time Difference between completion time and arrival time. Turn Around Time = Completion Time – Arrival Time  Waiting Time(W.T): Time Difference between turn around time and burst time. Waiting Time = Turn Around Time – Burst Time Now, lets calculate average waiting time and turn around time: Processes AT BT CT TAT WT P1 0 5 12 12-0 = 12 12-5 = 7 P2 1 4 11 11-1 = 10 10-4 = 6 P3 2 2 6 6-2 = 4 4-2 = 2 P4 4 1 9 9-4 = 5 5-1 = 4 Now,  Average Turn around time = (12 + 10 + 4 + 5)/4 = 31/4 = 7.7
  • 61.  Average waiting time = (7 + 6 + 2 + 4)/4 = 19/4 = 4.7 Example 2: Consider the following table of arrival time and burst time for three processes P1, P2 and P3 and given Time Quantum = 2 Process Burst Time Arrival Time P1 10 ms 0 ms P2 5 ms 0 ms P3 8 ms 0 ms Similarly, Gantt chart for this example: Gantt chart for example 2 Now, lets calculate average waiting time and turn around time: Processes AT BT CT TAT WT P1 0 10 23 23-0 = 23 23-10 = 13 P2 0 5 15 15-0 = 15 15-5 = 10 P3 0 8 21 21-0 = 21 21-8 = 13 Total Turn Around Time = 59 ms So, Average Turn Around Time = 59/3 = 19.667 ms And, Total Waiting Time = 36 ms So, Average Waiting Time = 36/3 = 12.00 ms Program for Round Robin Scheduling with arrival time as 0 for all processes Steps to find waiting times of all processes  Create an array rem_bt[] to keep track of remaining burst time of processes. This array is initially a copy of bt[] (burst times array)
  • 62.  Create another array wt[] to store waiting times of processes. Initialize this array as 0.  Initialize time : t = 0  Keep traversing all the processes while they are not done. Do following for i’th process if it is not done yet.  If rem_bt[i] > quantum  t = t + quantum  rem_bt[i] -= quantum;  Else // Last cycle for this process  t = t + rem_bt[i];  wt[i] = t – bt[i]  rem_bt[i] = 0; // This process is over Once we have waiting times, we can compute turn around time tat[i] of a process as sum of waiting and burst times, i.e., wt[i] + bt[i]. Below is implementation of above steps.