Round Robin Scheduling Algorithm with Different Arrival Time
Last Updated :
11 Jul, 2025
Round Robin Scheduling is one of the most popular CPU scheduling algorithms used in operating systems. This algorithm is designed to handle processes efficiently by assigning a fixed time slice or quantum to each process.
However, when processes arrive at different times, the scheduling becomes slightly more complex but remains true to its principles. In this article, we'll explore how the Round Robin Scheduling Algorithm effectively handles this situation. Also, we'll see its program implementation.
Characteristics of RR Scheduling Algo with Different Arrival Time
- Handles Dynamic Arrival: Processes can arrive at different times, and the scheduler dynamically adds them to the ready queue as they arrive, ensuring no process is overlooked.
- Time Quantum Distribution: Each process is allocated a fixed time slice (time quantum) for execution, regardless of its arrival time.
- Preemptive Execution: If a process doesn’t complete within its time quantum, it is preempted and added back to the queue for its next turn, even if new processes have arrived.
- Fair Scheduling: Even with different arrival times, all processes are treated fairly, and no process is given priority over others by default.
- Minimizes Starvation: New processes are regularly added to the queue, and the algorithm ensures that all processes get CPU time, reducing the risk of starvation.
- Efficient Queue Updates: The ready queue is updated dynamically as new processes arrive, maintaining a circular queue structure for smooth execution.
- Impact on Turnaround and Waiting Time: Processes arriving later may experience slightly increased waiting times, but the algorithm works to minimize delays by rotating through all tasks.
Example of Round Robin Scheduling Algorithm for the Different Arrival Time:
After all these we get the three times which are:
- Completion Time: the time taken for a process to complete.
- Turn Around Time: total time the process exists in the system. (completion time - arrival time).
- Waiting Time: total time waiting for their complete execution. (turn around time - burst time ).
- Turnaround Time (TAT) = Completion Time - Arrival Time.
- Waiting Time (WT) = TAT - Burst Time.
Process | Arrival Time | Burst Time | Completion Time | Turnaround Time (TAT) | Waiting Time (WT) |
---|
P1 | 0 | 5 | 14 | 14 | 9 |
P2 | 1 | 7 | 22 | 21 | 14 |
P3 | 3 | 4 | 18 | 15 | 11 |
P4 | 5 | 6 | 21 | 16 | 10 |
How to implement in a programming language:
- Declare arrival[], burst[], wait[], turn[] arrays and initialize them. Also declare a timer variable and initialize it to zero. To sustain the original burst array create another array (temp_burst[]) and copy all the values of burst array in it.
- To keep a check we create another array of bool type which keeps the record of whether a process is completed or not. we also need to maintain a queue array which contains the process indices (initially the array is filled with 0).
- Now we increment the timer variable until the first process arrives and when it does, we add the process index to the queue array.
- Now we execute the first process until the time quanta and during that time quanta, we check whether any other process has arrived or not and if it has then we add the index in the queue (by calling the fxn. queueUpdation()).
- Now, after doing the above steps if a process has finished, we store its exit time and execute the next process in the queue array. Else, we move the currently executed process at the end of the queue (by calling another fxn. queueMaintainence()) when the time slice expires.
- The above steps are then repeated until all the processes have been completely executed. If a scenario arises where there are some processes left but they have not arrived yet, then we shall wait and the CPU will remain idle during this interval.
Below is the implementation of the above approach:
(For the sake of simplicity, we assume that the arrival times are entered in a sorted way)
C++
//C++ Program for implementing
//Round Robin Algorithm
//code by sparsh_cbs
#include <iostream>
using namespace std;
void queueUpdation(int queue[],int timer,int arrival[],int n, int maxProccessIndex){
int zeroIndex;
for(int i = 0; i < n; i++){
if(queue[i] == 0){
zeroIndex = i;
break;
}
}
queue[zeroIndex] = maxProccessIndex + 1;
}
void queueMaintainence(int queue[], int n){
for(int i = 0; (i < n-1) && (queue[i+1] != 0) ; i++){
int temp = queue[i];
queue[i] = queue[i+1];
queue[i+1] = temp;
}
}
void checkNewArrival(int timer, int arrival[], int n, int maxProccessIndex,int queue[]){
if(timer <= arrival[n-1]){
bool newArrival = false;
for(int j = (maxProccessIndex+1); j < n; j++){
if(arrival[j] <= timer){
if(maxProccessIndex < j){
maxProccessIndex = j;
newArrival = true;
}
}
}
//adds the incoming process to the ready queue
//(if any arrives)
if(newArrival)
queueUpdation(queue,timer,arrival,n, maxProccessIndex);
}
}
//Driver Code
int main(){
int n,tq, timer = 0, maxProccessIndex = 0;
float avgWait = 0, avgTT = 0;
cout << "\nEnter the time quanta : ";
cin>>tq;
cout << "\nEnter the number of processes : ";
cin>>n;
int arrival[n], burst[n], wait[n], turn[n], queue[n], temp_burst[n];
bool complete[n];
cout << "\nEnter the arrival time of the processes : ";
for(int i = 0; i < n; i++)
cin>>arrival[i];
cout << "\nEnter the burst time of the processes : ";
for(int i = 0; i < n; i++){
cin>>burst[i];
temp_burst[i] = burst[i];
}
for(int i = 0; i < n; i++){ //Initializing the queue and complete array
complete[i] = false;
queue[i] = 0;
}
while(timer < arrival[0]) //Incrementing Timer until the first process arrives
timer++;
queue[0] = 1;
while(true){
bool flag = true;
for(int i = 0; i < n; i++){
if(temp_burst[i] != 0){
flag = false;
break;
}
}
if(flag)
break;
for(int i = 0; (i < n) && (queue[i] != 0); i++){
int ctr = 0;
while((ctr < tq) && (temp_burst[queue[0]-1] > 0)){
temp_burst[queue[0]-1] -= 1;
timer += 1;
ctr++;
//Checking and Updating the ready queue until all the processes arrive
checkNewArrival(timer, arrival, n, maxProccessIndex, queue);
}
//If a process is completed then store its exit time
//and mark it as completed
if((temp_burst[queue[0]-1] == 0) && (complete[queue[0]-1] == false)){
//turn array currently stores the completion time
turn[queue[0]-1] = timer;
complete[queue[0]-1] = true;
}
//checks whether or not CPU is idle
bool idle = true;
if(queue[n-1] == 0){
for(int i = 0; i < n && queue[i] != 0; i++){
if(complete[queue[i]-1] == false){
idle = false;
}
}
}
else
idle = false;
if(idle){
timer++;
checkNewArrival(timer, arrival, n, maxProccessIndex, queue);
}
//Maintaining the entries of processes
//after each premption in the ready Queue
queueMaintainence(queue,n);
}
}
for(int i = 0; i < n; i++){
turn[i] = turn[i] - arrival[i];
wait[i] = turn[i] - burst[i];
}
cout << "\nProgram No.\tArrival Time\tBurst Time\tWait Time\tTurnAround Time"
<< endl;
for(int i = 0; i < n; i++){
cout<<i+1<<"\t\t"<<arrival[i]<<"\t\t"
<<burst[i]<<"\t\t"<<wait[i]<<"\t\t"<<turn[i]<<endl;
}
for(int i =0; i< n; i++){
avgWait += wait[i];
avgTT += turn[i];
}
cout<<"\nAverage wait time : "<<(avgWait/n)
<<"\nAverage Turn Around Time : "<<(avgTT/n);
return 0;
}
Java
//JAVA Program for implementing
//Round Robin Algorithm
// code by Sparsh_cbs
import java.util.*;
public class RoundRobin{
private static Scanner inp = new Scanner(System.in);
//Driver Code
public static void main(String[] args){
int n,tq, timer = 0, maxProccessIndex = 0;
float avgWait = 0, avgTT = 0;
System.out.print("\nEnter the time quanta : ");
tq = inp.nextInt();
System.out.print("\nEnter the number of processes : ");
n = inp.nextInt();
int arrival[] = new int[n];
int burst[] = new int[n];
int wait[] = new int[n];
int turn[] = new int[n];
int queue[] = new int[n];
int temp_burst[] = new int[n];
boolean complete[] = new boolean[n];
System.out.print("\nEnter the arrival time of the processes : ");
for(int i = 0; i < n; i++)
arrival[i] = inp.nextInt();
System.out.print("\nEnter the burst time of the processes : ");
for(int i = 0; i < n; i++){
burst[i] = inp.nextInt();
temp_burst[i] = burst[i];
}
for(int i = 0; i < n; i++){ //Initializing the queue and complete array
complete[i] = false;
queue[i] = 0;
}
while(timer < arrival[0]) //Incrementing Timer until the first process arrives
timer++;
queue[0] = 1;
while(true){
boolean flag = true;
for(int i = 0; i < n; i++){
if(temp_burst[i] != 0){
flag = false;
break;
}
}
if(flag)
break;
for(int i = 0; (i < n) && (queue[i] != 0); i++){
int ctr = 0;
while((ctr < tq) && (temp_burst[queue[0]-1] > 0)){
temp_burst[queue[0]-1] -= 1;
timer += 1;
ctr++;
//Updating the ready queue until all the processes arrive
checkNewArrival(timer, arrival, n, maxProccessIndex, queue);
}
if((temp_burst[queue[0]-1] == 0) && (complete[queue[0]-1] == false)){
turn[queue[0]-1] = timer; //turn currently stores exit times
complete[queue[0]-1] = true;
}
//checks whether or not CPU is idle
boolean idle = true;
if(queue[n-1] == 0){
for(int k = 0; k < n && queue[k] != 0; k++){
if(complete[queue[k]-1] == false){
idle = false;
}
}
}
else
idle = false;
if(idle){
timer++;
checkNewArrival(timer, arrival, n, maxProccessIndex, queue);
}
//Maintaining the entries of processes after each premption in the ready Queue
queueMaintainence(queue,n);
}
}
for(int i = 0; i < n; i++){
turn[i] = turn[i] - arrival[i];
wait[i] = turn[i] - burst[i];
}
System.out.print("\nProgram No.\tArrival Time\tBurst Time\tWait Time\tTurnAround Time"
+ "\n");
for(int i = 0; i < n; i++){
System.out.print(i+1+"\t\t"+arrival[i]+"\t\t"+burst[i]
+"\t\t"+wait[i]+"\t\t"+turn[i]+ "\n");
}
for(int i =0; i< n; i++){
avgWait += wait[i];
avgTT += turn[i];
}
System.out.print("\nAverage wait time : "+(avgWait/n)
+"\nAverage Turn Around Time : "+(avgTT/n));
}
public static void queueUpdation(int queue[],int timer,int arrival[],int n, int maxProccessIndex){
int zeroIndex = -1;
for(int i = 0; i < n; i++){
if(queue[i] == 0){
zeroIndex = i;
break;
}
}
if(zeroIndex == -1)
return;
queue[zeroIndex] = maxProccessIndex + 1;
}
public static void checkNewArrival(int timer, int arrival[], int n, int maxProccessIndex,int queue[]){
if(timer <= arrival[n-1]){
boolean newArrival = false;
for(int j = (maxProccessIndex+1); j < n; j++){
if(arrival[j] <= timer){
if(maxProccessIndex < j){
maxProccessIndex = j;
newArrival = true;
}
}
}
if(newArrival) //adds the index of the arriving process(if any)
queueUpdation(queue,timer,arrival,n, maxProccessIndex);
}
}
public static void queueMaintainence(int queue[], int n){
for(int i = 0; (i < n-1) && (queue[i+1] != 0) ; i++){
int temp = queue[i];
queue[i] = queue[i+1];
queue[i+1] = temp;
}
}
}
Python
# Python program for implementing Round Robin Algorithm
def queueUpdation(queue, timer, arrival, n, maxProccessIndex):
zeroIndex = -1
for i in range(n):
if(queue[i] == 0):
zeroIndex = i
break
if(zeroIndex == -1):
return
queue[zeroIndex] = maxProccessIndex + 1
def checkNewArrival(timer, arrival, n, maxProccessIndex, queue):
if(timer <= arrival[n-1]):
newArrival = False
for j in range(maxProccessIndex+1, n):
if(arrival[j] <= timer):
if(maxProccessIndex < j):
maxProccessIndex = j
newArrival = True
# adds the index of the arriving process(if any)
if(newArrival):
queueUpdation(queue, timer, arrival, n, maxProccessIndex)
def queueMaintainence(queue, n):
for i in range(n-1):
if(queue[i+1] != 0):
queue[i], queue[i+1] = queue[i+1], queue[i]
timer, maxProccessIndex = 0, 0
avgWait, avgTT = 0, 0
print("\nEnter the time quanta :", end=" ")
tq = int(input())
print("\nEnter the number of processes :", end=" ")
n = int(input())
arrival = [0]*n
burst = [0]*n
wait = [0]*n
turn = [0]*n
queue = [0]*n
temp_burst = [0]*n
complete = [False]*n
print("\nEnter the arrival time of the processes :", end=" ")
for i in range(n):
arrival[i] = int(input())
print("\nEnter the burst time of the processes :", end=" ")
for i in range(n):
burst[i] = int(input())
temp_burst[i] = burst[i]
for i in range(n):
# Initializing the queue and complete array
complete[i] = False
queue[i] = 0
while(timer < arrival[0]):
# Incrementing Timer until the first process arrives
timer += 1
queue[0] = 1
while(True):
flag = True
for i in range(n):
if(temp_burst[i] != 0):
flag = False
break
if(flag):
break
for i in range(n and queue[i] != 0):
ctr = 0
while((ctr < tq) and (temp_burst[queue[0]-1] > 0)):
temp_burst[queue[0]-1] -= 1
timer += 1
ctr += 1
# Updating the ready queue until all the processes arrive
checkNewArrival(timer, arrival, n, maxProccessIndex, queue)
if((temp_burst[queue[0]-1] == 0) and (complete[queue[0]-1] == False)):
# turn currently stores exit times
turn[queue[0]-1] = timer
complete[queue[0]-1] = True
# checks whether or not CPU is idle
idle = True
if(queue[n-1] == 0):
for k in range(n):
if(queue[k] != 0):
if(complete[queue[k]-1] == False):
idle = False
else:
idle = False
if(idle):
timer += 1
checkNewArrival(timer, arrival, n, maxProccessIndex, queue)
# Maintaining the entries of processes aftereach premption in the ready Queue
queueMaintainence(queue, n)
for i in range(n):
turn[i] = turn[i] - arrival[i]
wait[i] = turn[i] - burst[i]
print("\nProgram No.\tArrival Time\tBurst Time\tWait Time\tTurnAround Time\n")
for i in range(n):
print(i+1, "\t\t", arrival[i], "\t\t", burst[i],
"\t\t", wait[i], "\t\t", turn[i], "\n")
for i in range(n):
avgWait += wait[i]
avgTT += turn[i]
print("\nAverage wait time : ", (avgWait//n))
print("\nAverage Turn Around Time : ", (avgTT//n))
# This code is contributed by lokeshmvs21.
C#
// C# program to implement Round Robin
// Scheduling with different arrival time
using System;
class GFG {
public static void roundRobin(String[] p, int[] a,
int[] b, int n)
{
// result of average times
int res = 0;
int resc = 0;
// for sequence storage
String seq = "";
// copy the burst array and arrival array
// for not effecting the actual array
int[] res_b = new int[b.Length];
int[] res_a = new int[a.Length];
for (int i = 0; i < res_b.Length; i++) {
res_b[i] = b[i];
res_a[i] = a[i];
}
// critical time of system
int t = 0;
// for store the waiting time
int[] w = new int[p.Length];
// for store the Completion time
int[] comp = new int[p.Length];
while (true) {
Boolean flag = true;
for (int i = 0; i < p.Length; i++) {
// these condition for if
// arrival is not on zero
// check that if there come before qtime
if (res_a[i] <= t) {
if (res_a[i] <= n) {
if (res_b[i] > 0) {
flag = false;
if (res_b[i] > n) {
// make decrease the b time
t = t + n;
res_b[i] = res_b[i] - n;
res_a[i] = res_a[i] + n;
seq += "->" + p[i];
}
else {
// for last time
t = t + res_b[i];
// store comp time
comp[i] = t - a[i];
// store wait time
w[i] = t - b[i] - a[i];
res_b[i] = 0;
// add sequence
seq += "->" + p[i];
}
}
}
else if (res_a[i] > n) {
// is any have less arrival time
// the coming process then execute
// them
for (int j = 0; j < p.Length; j++) {
// compare
if (res_a[j] < res_a[i]) {
if (res_b[j] > 0) {
flag = false;
if (res_b[j] > n) {
t = t + n;
res_b[j]
= res_b[j] - n;
res_a[j]
= res_a[j] + n;
seq += "->" + p[j];
}
else {
t = t + res_b[j];
comp[j] = t - a[j];
w[j] = t - b[j]
- a[j];
res_b[j] = 0;
seq += "->" + p[j];
}
}
}
}
// now the previous process
// according to ith is process
if (res_b[i] > 0) {
flag = false;
// Check for greaters
if (res_b[i] > n) {
t = t + n;
res_b[i] = res_b[i] - n;
res_a[i] = res_a[i] + n;
seq += "->" + p[i];
}
else {
t = t + res_b[i];
comp[i] = t - a[i];
w[i] = t - b[i] - a[i];
res_b[i] = 0;
seq += "->" + p[i];
}
}
}
}
// if no process is come on the critical
else if (res_a[i] > t) {
t++;
i--;
}
}
// for exit the while loop
if (flag) {
break;
}
}
Console.WriteLine("name ctime wtime");
for (int i = 0; i < p.Length; i++) {
Console.WriteLine(" " + p[i] + "\t" + comp[i]
+ "\t" + w[i]);
res = res + w[i];
resc = resc + comp[i];
}
Console.WriteLine("Average waiting time is "
+ (float)res / p.Length);
Console.WriteLine("Average compilation time is "
+ (float)resc / p.Length);
Console.WriteLine("Sequence is like that " + seq);
}
// Driver Code
public static void Main(String[] args)
{
// name of the process
String[] name = { "p1", "p2", "p3", "p4" };
// arrival for every process
int[] arrivaltime = { 0, 1, 2, 3 };
// burst time for every process
int[] bursttime = { 10, 4, 5, 3 };
// quantum time of each process
int q = 3;
// cal the function for output
roundRobin(name, arrivaltime, bursttime, q);
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
const queueUpdation = (queue, timer, arrival, n, maxProccessIndex) => {
let zeroIndex;
for (let i = 0; i < n; i++) {
if (queue[i] == 0) {
zeroIndex = i;
break;
}
}
queue[zeroIndex] = maxProccessIndex + 1;
}
const queueMaintainence = (queue, n) => {
for (let i = 0; (i < n - 1) && (queue[i + 1] != 0); i++) {
let temp = queue[i];
queue[i] = queue[i + 1];
queue[i + 1] = temp;
}
}
const checkNewArrival = (timer, arrival, n, maxProccessIndex, queue) => {
if (timer <= arrival[n - 1]) {
let newArrival = false;
for (let j = (maxProccessIndex + 1); j < n; j++) {
if (arrival[j] <= timer) {
if (maxProccessIndex < j) {
maxProccessIndex = j;
newArrival = true;
}
}
}
//adds the incoming process to the ready queue
//(if any arrives)
if (newArrival)
queueUpdation(queue, timer, arrival, n, maxProccessIndex);
}
}
//Driver Code
let n = 4;
let tq = 2;
let timer = 0;
let maxProccessIndex = 0;
let avgWait = 0;
let avgTT = 0;
const wait = [];
const turn = [];
const queue = [];
const temp_burst = [];
const complete = [];
const arrival = [0, 1, 2, 3];
const burst = [5, 4, 2, 1];
for (let i = 0; i < n; i++) {
temp_burst[i] = burst[i];
}
for (let i = 0; i < n; i++) { //Initializing the queue and complete array
complete[i] = false;
queue[i] = 0;
}
while (timer < arrival[0]) //Incrementing Timer until the first process arrives
timer++;
queue[0] = 1;
while (true) {
let flag = true;
for (let i = 0; i < n; i++) {
if (temp_burst[i] != 0) {
flag = false;
break;
}
}
if (flag)
break;
for (let i = 0; (i < n) && (queue[i] != 0); i++) {
let ctr = 0;
while ((ctr < tq) && (temp_burst[queue[0] - 1] > 0)) {
temp_burst[queue[0] - 1] -= 1;
timer += 1;
ctr++;
// Checking and Updating the ready queue until all the processes arrive
checkNewArrival(timer, arrival, n, maxProccessIndex, queue);
}
// If a process is completed then store its exit time
// and mark it as completed
if ((temp_burst[queue[0] - 1] == 0) && (complete[queue[0] - 1] == false)) {
//turn array currently stores the completion time
turn[queue[0] - 1] = timer;
complete[queue[0] - 1] = true;
}
// checks whether or not CPU is idle
let idle = true;
if (queue[n - 1] == 0) {
for (let i = 0; i < n && queue[i] != 0; i++) {
if (complete[queue[i] - 1] == false) {
idle = false;
}
}
}
else
idle = false;
if (idle) {
timer++;
checkNewArrival(timer, arrival, n, maxProccessIndex, queue);
}
//Maintaining the entries of processes
//after each premption in the ready Queue
queueMaintainence(queue, n);
}
}
for (let i = 0; i < n; i++) {
turn[i] = turn[i] - arrival[i];
wait[i] = turn[i] - burst[i];
}
console.log(`Time Quanta : ${tq}`);
console.log(`Number of Processes : ${n}`);
console.log(`Arrival Time of Processes : ${arrival}`);
console.log(`Burst Time of Processes : ${burst}`);
console.log("\nProgram No.\tArrival Time\tBurst Time\tWait Time\tTurnAround Time\n");
for (let i = 0; i < n; i++) {
console.log(`${i + 1}\t\t\t ${arrival[i]}\t\t\t ${burst[i]}\t\t\t\t ${wait[i]} \t\t\t\t ${turn[i]} \n`);
}
for (let i = 0; i < n; i++) {
avgWait += wait[i];
avgTT += turn[i];
}
console.log(`\nAverage wait time : ${avgWait / n}`);
console.log(`\nAverage Turn Around Time : ${avgTT / n}`);
// This code is contributed by akashish_.
</script>
Output:
Enter the time quanta : 2
Enter the number of processes : 4
Enter the arrival time of the processes : 0 1 2 3
Enter the burst time of the processes : 5 4 2 1
Program No. Arrival Time Burst Time Wait Time TurnAround Time
1 0 5 7 12
2 1 4 6 10
3 2 2 2 4
4 3 1 5 6
Average wait time : 5
Average Turn Around Time : 8
In case of any queries or a problem with the code, please write it in the comment section.
Note: A slightly optimized version of the above-implemented code could be done by using Queue data structure as follows:
C++
#include <bits/stdc++.h>
using namespace std;
struct Process
{
int pid;
int arrivalTime;
int burstTime;
int burstTimeRemaining; // the amount of CPU time remaining after each execution
int completionTime;
int turnaroundTime;
int waitingTime;
bool isComplete;
bool inQueue;
};
/*
* At every time quantum or when a process has been executed before the time quantum,
* check for any new arrivals and push them into the queue
*/
void checkForNewArrivals(Process processes[], const int n, const int currentTime, queue<int> &readyQueue)
{
for (int i = 0; i < n; i++)
{
Process p = processes[i];
// checking if any processes has arrived
// if so, push them in the ready Queue.
if (p.arrivalTime <= currentTime && !p.inQueue && !p.isComplete)
{
processes[i].inQueue = true;
readyQueue.push(i);
}
}
}
/*
* Context switching takes place at every time quantum
* At every iteration, the burst time of the processes in the queue are handled using this method
*/
void updateQueue(Process processes[], const int n, const int quantum, queue<int> &readyQueue, int ¤tTime, int &programsExecuted)
{
int i = readyQueue.front();
readyQueue.pop();
// if the process is going to be finished executing,
// ie, when it's remaining burst time is less than time quantum
// mark it completed and increment the current time
// and calculate its waiting time and turnaround time
if (processes[i].burstTimeRemaining <= quantum)
{
processes[i].isComplete = true;
currentTime += processes[i].burstTimeRemaining;
processes[i].completionTime = currentTime;
processes[i].waitingTime = processes[i].completionTime - processes[i].arrivalTime - processes[i].burstTime;
processes[i].turnaroundTime = processes[i].waitingTime + processes[i].burstTime;
if (processes[i].waitingTime < 0)
processes[i].waitingTime = 0;
processes[i].burstTimeRemaining = 0;
// if all the processes are not yet inserted in the queue,
// then check for new arrivals
if (programsExecuted != n)
{
checkForNewArrivals(processes, n, currentTime, readyQueue);
}
}
else
{
// the process is not done yet. But it's going to be pre-empted
// since one quantum is used
// but first subtract the time the process used so far
processes[i].burstTimeRemaining -= quantum;
currentTime += quantum;
// if all the processes are not yet inserted in the queue,
// then check for new arrivals
if (programsExecuted != n)
{
checkForNewArrivals(processes, n, currentTime, readyQueue);
}
// insert the incomplete process back into the queue
readyQueue.push(i);
}
}
/*
* Just a function that outputs the result in terms of their PID.
*/
void output(Process processes[], const int n)
{
double avgWaitingTime = 0;
double avgTurntaroundTime = 0;
// sort the processes array by processes.PID
sort(processes, processes + n, [](const Process &p1, const Process &p2)
{ return p1.pid < p2.pid; });
for (int i = 0; i < n; i++)
{
cout << "Process " << processes[i].pid << ": Waiting Time: " << processes[i].waitingTime << " Turnaround Time: " << processes[i].turnaroundTime << endl;
avgWaitingTime += processes[i].waitingTime;
avgTurntaroundTime += processes[i].turnaroundTime;
}
cout << "Average Waiting Time: " << avgWaitingTime / n << endl;
cout << "Average Turnaround Time: " << avgTurntaroundTime / n << endl;
}
/*
* This function assumes that the processes are already sorted according to their arrival time
*/
void roundRobin(Process processes[], int n, int quantum)
{
queue<int> readyQueue;
readyQueue.push(0); // initially, pushing the first process which arrived first
processes[0].inQueue = true;
int currentTime = 0; // holds the current time after each process has been executed
int programsExecuted = 0; // holds the number of programs executed so far
while (!readyQueue.empty())
{
updateQueue(processes, n, quantum, readyQueue, currentTime, programsExecuted);
}
}
int main()
{
int n, quantum;
cout << "Enter the number of processes: ";
cin >> n;
cout << "Enter time quantum: ";
cin >> quantum;
Process processes[n + 1];
for (int i = 0; i < n; i++)
{
cout << "Enter arrival time and burst time of each process " << i + 1 << ": ";
cin >> processes[i].arrivalTime;
cin >> processes[i].burstTime;
processes[i].burstTimeRemaining = processes[i].burstTime;
processes[i].pid = i + 1;
cout << endl;
}
// stl sort in terms of arrival time
sort(processes, processes + n, [](const Process &p1, const Process &p2)
{ return p1.arrivalTime < p2.arrivalTime; });
roundRobin(processes, n, quantum);
output(processes, n);
return 0;
}
Java
// Java Code
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
class GFG {
// At every time quantum or when a process has been
// executed before the time quantum, check for any new
// arrivals and push them into the queue
public static void
checkForNewArrivals(Process[] processes, int n,
int currentTime,
Queue<Integer> readyQueue)
{
for (int i = 0; i < n; i++) {
Process p = processes[i];
// checking if any processes has arrived
// if so, push them in the ready Queue.
if (p.arrivalTime <= currentTime && !p.inQueue
&& !p.isComplete) {
processes[i].inQueue = true;
readyQueue.add(i);
}
}
}
// Context switching takes place at every time quantum
// At every iteration, the burst time of the processes
// in the queue are handled using this method
public static void
updateQueue(Process[] processes, int n, int quantum,
Queue<Integer> readyQueue, int currentTime,
int programsExecuted)
{
int i = readyQueue.remove();
// if the process is going to be finished executing,
// ie, when it's remaining burst time is less than
// time quantum mark it completed and increment the
// current time and calculate its waiting time and
// turnaround time
if (processes[i].burstTimeRemaining <= quantum) {
processes[i].isComplete = true;
currentTime += processes[i].burstTimeRemaining;
processes[i].completionTime = currentTime;
processes[i].waitingTime
= processes[i].completionTime
- processes[i].arrivalTime
- processes[i].burstTime;
processes[i].turnaroundTime
= processes[i].waitingTime
+ processes[i].burstTime;
if (processes[i].waitingTime < 0)
processes[i].waitingTime = 0;
processes[i].burstTimeRemaining = 0;
// if all the processes are not yet inserted in
// the queue, then check for new arrivals
if (programsExecuted != n) {
checkForNewArrivals(
processes, n, currentTime, readyQueue);
}
}
else {
// the process is not done yet. But it's going
// to be pre-empted since one quantum is used
// but first subtract the time the process used
// so far
processes[i].burstTimeRemaining -= quantum;
currentTime += quantum;
// if all the processes are not yet inserted in
// the queue, then check for new arrivals
if (programsExecuted != n) {
checkForNewArrivals(
processes, n, currentTime, readyQueue);
}
// insert the incomplete process back into the
// queue
readyQueue.add(i);
}
}
// Just a function that outputs the result in terms of
// their PID.
public static void output(Process[] processes, int n)
{
double avgWaitingTime = 0;
double avgTurntaroundTime = 0;
// sort the processes array by processes.PID
Arrays.sort(processes, (Process p1, Process p2) -> {
return p1.pid - p2.pid;
});
for (int i = 0; i < n; i++) {
System.out.println(
"Process " + processes[i].pid
+ ": Waiting Time: "
+ processes[i].waitingTime
+ " Turnaround Time: "
+ processes[i].turnaroundTime);
avgWaitingTime += processes[i].waitingTime;
avgTurntaroundTime
+= processes[i].turnaroundTime;
}
System.out.println("Average Waiting Time: "
+ avgWaitingTime / n);
System.out.println("Average Turnaround Time: "
+ avgTurntaroundTime / n);
}
/*
* This function assumes that the processes are already
* sorted according to their arrival time
*/
public static void roundRobin(Process[] processes,
int n, int quantum)
{
Queue<Integer> readyQueue
= new LinkedList<Integer>();
readyQueue.add(0); // initially, pushing the first
// process which arrived first
processes[0].inQueue = true;
int currentTime
= 0; // holds the current time after each
// process has been executed
int programsExecuted
= 0; // holds the number of programs executed so
// far
while (!readyQueue.isEmpty()) {
updateQueue(processes, n, quantum, readyQueue,
currentTime, programsExecuted);
}
}
public static class Process {
int pid;
int arrivalTime;
int burstTime;
int burstTimeRemaining; // the amount of CPU time
// remaining after each
// execution
int completionTime;
int turnaroundTime;
int waitingTime;
boolean isComplete;
boolean inQueue;
}
public static void main(String[] args)
{
int n, quantum;
System.out.println(
"Enter the number of processes: ");
n = Integer.parseInt(System.console().readLine());
System.out.println("Enter time quantum: ");
quantum
= Integer.parseInt(System.console().readLine());
Process[] processes = new Process[n + 1];
for (int i = 0; i < n; i++) {
System.out.println(
"Enter arrival time and burst time of each process "
+ (i + 1) + ": ");
processes[i].arrivalTime = Integer.parseInt(
System.console().readLine());
processes[i].burstTime = Integer.parseInt(
System.console().readLine());
processes[i].burstTimeRemaining
= processes[i].burstTime;
processes[i].pid = i + 1;
System.out.println();
}
// stl sort in terms of arrival time
Arrays.sort(processes, (Process p1, Process p2) -> {
return p1.arrivalTime - p2.arrivalTime;
});
roundRobin(processes, n, quantum);
output(processes, n);
}
}
// This code is contributed by akashish__
Python
# Python Code
class Process:
def __init__(self):
self.pid = 0
self.arrivalTime = 0
self.burstTime = 0
self.burstTimeRemaining = 0
self.completionTime = 0
self.turnaroundTime = 0
self.waitingTime = 0
self.isComplete = False
self.inQueue = False
# At every time quantum or when a process has been executed before the time quantum,
# check for any new arrivals and push them into the queue
def check_for_new_arrivals(processes, n, current_time, ready_queue):
for i in range(n):
p = processes[i]
# checking if any processes has arrived
# if so, push them in the ready Queue.
if p.arrivalTime <= current_time and not p.inQueue and not p.isComplete:
processes[i].inQueue = True
ready_queue.append(i)
# Context switching takes place at every time quantum
# At every iteration, the burst time of the processes in the queue are handled using this method
def update_queue(processes, n, quantum, ready_queue, current_time, programs_executed):
i = ready_queue[0]
ready_queue.pop(0)
# if the process is going to be finished executing,
# ie, when it's remaining burst time is less than time quantum
# mark it completed and increment the current time
# and calculate its waiting time and turnaround time
if processes[i].burstTimeRemaining <= quantum:
processes[i].isComplete = True
current_time += processes[i].burstTimeRemaining
processes[i].completionTime = current_time
processes[i].waitingTime = processes[i].completionTime - processes[i].arrivalTime - processes[i].burstTime
processes[i].turnaroundTime = processes[i].waitingTime + processes[i].burstTime
if processes[i].waitingTime < 0:
processes[i].waitingTime = 0
processes[i].burstTimeRemaining = 0
# if all the processes are not yet inserted in the queue,
# then check for new arrivals
if programs_executed != n:
check_for_new_arrivals(processes, n, current_time, ready_queue)
else:
# the process is not done yet. But it's going to be pre-empted
# since one quantum is used
# but first subtract the time the process used so far
processes[i].burstTimeRemaining -= quantum
current_time += quantum
# if all the processes are not yet inserted in the queue,
# then check for new arrivals
if programs_executed != n:
check_for_new_arrivals(processes, n, current_time, ready_queue)
# insert the incomplete process back into the queue
ready_queue.append(i)
# Just a function that outputs the result in terms of their PID.
def output(processes, n):
avg_waiting_time = 0
avg_turntaround_time = 0
# sort the processes array by processes.PID
processes.sort(key=lambda p: p.pid)
for i in range(n):
print("Process ", processes[i].pid, ": Waiting Time: ", processes[i].waitingTime,
" Turnaround Time: ", processes[i].turnaroundTime, sep="")
avg_waiting_time += processes[i].waitingTime
avg_turntaround_time += processes[i].turnaroundTime
print("Average Waiting Time: ", avg_waiting_time / n)
print("Average Turnaround Time: ", avg_turntaround_time / n)
# This function assumes that the processes are already sorted according to their arrival time
def round_robin(processes, n, quantum):
ready_queue = []
ready_queue.append(0) # initially, pushing the first process which arrived first
processes[0].inQueue = True
current_time = 0 # holds the current time after each process has been executed
programs_executed = 0 # holds the number of programs executed so far
while len(ready_queue) != 0:
update_queue(processes, n, quantum, ready_queue, current_time, programs_executed)
def main():
n = int(input("Enter the number of processes: "))
quantum = int(input("Enter time quantum: "))
processes = []
for i in range(n):
print("Enter arrival time and burst time of each process ", i + 1, ": ", sep="", end="")
arrival_time = int(input())
burst_time = int(input())
proc = Process()
proc.arrivalTime = arrival_time
proc.burstTime = burst_time
proc.burstTimeRemaining = burst_time
proc.pid = i + 1
processes.append(proc)
print("")
# stl sort in terms of arrival time
processes.sort(key=lambda p: p.arrivalTime)
round_robin(processes, n, quantum)
output(processes, n)
main()
# This code is contributed by akashish__
C#
// C# Code
using System;
using System.Collections.Generic;
using System.Linq;
class GFG
{
// At every time quantum or when a process has been
// executed before the time quantum, check for any new
// arrivals and push them into the queue
public static void
checkForNewArrivals(Process[] processes, int n,
int currentTime,
Queue<int> readyQueue)
{
for (int i = 0; i < n; i++)
{
Process p = processes[i];
// checking if any processes has arrived
// if so, push them in the ready Queue.
if (p.arrivalTime <= currentTime && !p.inQueue
&& !p.isComplete)
{
processes[i].inQueue = true;
readyQueue.Enqueue(i);
}
}
}
// Context switching takes place at every time quantum
// At every iteration, the burst time of the processes
// in the queue are handled using this method
public static void
updateQueue(Process[] processes, int n, int quantum,
Queue<int> readyQueue, int currentTime,
int programsExecuted)
{
int i = readyQueue.Dequeue();
// if the process is going to be finished executing,
// ie, when it's remaining burst time is less than
// time quantum mark it completed and increment the
// current time and calculate its waiting time and
// turnaround time
if (processes[i].burstTimeRemaining <= quantum)
{
processes[i].isComplete = true;
currentTime += processes[i].burstTimeRemaining;
processes[i].completionTime = currentTime;
processes[i].waitingTime
= processes[i].completionTime
- processes[i].arrivalTime
- processes[i].burstTime;
processes[i].turnaroundTime
= processes[i].waitingTime
+ processes[i].burstTime;
if (processes[i].waitingTime < 0)
processes[i].waitingTime = 0;
processes[i].burstTimeRemaining = 0;
// if all the processes are not yet inserted in
// the queue, then check for new arrivals
if (programsExecuted != n)
{
checkForNewArrivals(
processes, n, currentTime, readyQueue);
}
}
else
{
// the process is not done yet. But it's going
// to be pre-empted since one quantum is used
// but first subtract the time the process used
// so far
processes[i].burstTimeRemaining -= quantum;
currentTime += quantum;
// if all the processes are not yet inserted in
// the queue, then check for new arrivals
if (programsExecuted != n)
{
checkForNewArrivals(
processes, n, currentTime, readyQueue);
}
// insert the incomplete process back into the
// queue
readyQueue.Enqueue(i);
}
}
// Just a function that outputs the result in terms of
// their PID.
public static void output(Process[] processes, int n)
{
double avgWaitingTime = 0;
double avgTurntaroundTime = 0;
// sort the processes array by processes.PID
processes = processes.OrderBy(p => p.pid).ToArray();
for (int i = 0; i < n; i++)
{
Console.WriteLine("Process " + processes[i].pid
+ ": Waiting Time: "
+ processes[i].waitingTime
+ " Turnaround Time: "
+ processes[i].turnaroundTime);
avgWaitingTime += processes[i].waitingTime;
avgTurntaroundTime
+= processes[i].turnaroundTime;
}
Console.WriteLine("Average Waiting Time: "
+ avgWaitingTime / n);
Console.WriteLine("Average Turnaround Time: "
+ avgTurntaroundTime / n);
}
/*
* This function assumes that the processes are already
* sorted according to their arrival time
*/
public static void roundRobin(Process[] processes,
int n, int quantum)
{
Queue<int> readyQueue
= new Queue<int>();
readyQueue.Enqueue(0); // initially, pushing the first
// process which arrived first
processes[0].inQueue = true;
int currentTime
= 0; // holds the current time after each
// process has been executed
int programsExecuted
= 0; // holds the number of programs executed so
// far
while (readyQueue.Count() > 0)
{
updateQueue(processes, n, quantum, readyQueue,
currentTime, programsExecuted);
}
}
public class Process
{
public int pid;
public int arrivalTime;
public int burstTime;
public int burstTimeRemaining; // the amount of CPU time
// remaining after each
// execution
public int completionTime;
public int turnaroundTime;
public int waitingTime;
public bool isComplete;
public bool inQueue;
}
public static void Main(String[] args)
{
int n, quantum;
Console.WriteLine("Enter the number of processes: ");
n = int.Parse(Console.ReadLine());
Console.WriteLine("Enter time quantum: ");
quantum
= int.Parse(Console.ReadLine());
Process[] processes = new Process[n + 1];
for (int i = 0; i < n; i++)
{
Console.WriteLine("Enter arrival time and burst time of each process "
+ (i + 1) + ": ");
processes[i].arrivalTime = int.Parse(
Console.ReadLine());
processes[i].burstTime = int.Parse(
Console.ReadLine());
processes[i].burstTimeRemaining
= processes[i].burstTime;
processes[i].pid = i + 1;
Console.WriteLine();
}
// stl sort in terms of
processes.OrderBy(p => p.arrivalTime).ToArray();
roundRobin(processes, n, quantum);
output(processes, n);
}
}
// This code is contributed by akashish__
JavaScript
const readline = require('readline-sync');
class Process {
constructor(pid, arrivalTime, burstTime) {
this.pid = pid;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
this.burstTimeRemaining = burstTime;
this.completionTime = 0;
this.turnaroundTime = 0;
this.waitingTime = 0;
this.isComplete = false;
this.inQueue = false;
}
}
function checkForNewArrivals(processes, currentTime, readyQueue) {
processes.forEach((p, i) => {
if (p.arrivalTime <= currentTime && !p.inQueue && !p.isComplete) {
processes[i].inQueue = true;
readyQueue.push(i);
}
});
}
function updateQueue(processes, quantum, readyQueue, currentTime, programsExecuted) {
const i = readyQueue.shift();
if (processes[i].burstTimeRemaining <= quantum) {
processes[i].isComplete = true;
currentTime += processes[i].burstTimeRemaining;
processes[i].completionTime = currentTime;
processes[i].waitingTime =
processes[i].completionTime - processes[i].arrivalTime - processes[i].burstTime;
processes[i].turnaroundTime =
processes[i].waitingTime + processes[i].burstTime;
if (processes[i].waitingTime < 0)
processes[i].waitingTime = 0;
processes[i].burstTimeRemaining = 0;
if (programsExecuted !== processes.length - 1) {
checkForNewArrivals(processes, currentTime, readyQueue);
}
} else {
processes[i].burstTimeRemaining -= quantum;
currentTime += quantum;
if (programsExecuted !== processes.length - 1) {
checkForNewArrivals(processes, currentTime, readyQueue);
}
readyQueue.push(i);
}
}
function output(processes) {
let avgWaitingTime = 0;
let avgTurnaroundTime = 0;
processes.sort((p1, p2) => p1.pid - p2.pid);
processes.forEach(p => {
console.log(`Process ${p.pid}: Waiting Time: ${p.waitingTime} Turnaround Time: ${p.turnaroundTime}`);
avgWaitingTime += p.waitingTime;
avgTurnaroundTime += p.turnaroundTime;
});
console.log(`Average Waiting Time: ${avgWaitingTime / processes.length}`);
console.log(`Average Turnaround Time: ${avgTurnaroundTime / processes.length}`);
}
function roundRobin(processes, quantum) {
const readyQueue = [0];
processes[0].inQueue = true;
let currentTime = 0;
let programsExecuted = 0;
while (readyQueue.length > 0) {
updateQueue(processes, quantum, readyQueue, currentTime, programsExecuted);
}
}
function main() {
const n = parseInt(readline.question("Enter the number of processes: "));
const quantum = parseInt(readline.question("Enter time quantum: "));
const processes = Array.from({ length: n }, (_, i) => {
console.log(`Enter arrival time and burst time of each process ${i + 1}: `);
const arrivalTime = parseInt(readline.question("Arrival Time: "));
const burstTime = parseInt(readline.question("Burst Time: "));
return new Process(i + 1, arrivalTime, burstTime);
});
processes.sort((p1, p2) => p1.arrivalTime - p2.arrivalTime);
roundRobin(processes, quantum);
output(processes);
}
main();
Enter the arrival time and burst time of each process:
0 5
1 4
2 2
3 1Enter the number of processes: 4
Enter time quantum: 2
Process 1: Waiting Time: 7 Turnaround Time: 12
Process 2: Waiting Time: 6 Turnaround Time: 10
Process 3: Waiting Time: 2 Turnaround Time: 4
Process 4: Waiting Time: 5 Turnaround Time: 6
Average Waiting Time: 5
Average Turnaround Time: 8
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem