Posted on by Kalkicode
Code Operating System

Round Robin Scheduling

In computing, round-robin (RR) is one of the algorithms used by process and network schedulers. Time slices (also known as time quanta) are allotted to each process in equal chunks and in a circular sequence, managing all processes without priority, as the word is often used (also known as cyclic executive). Round-robin scheduling is simple, straightforward, and free of famine. Other scheduling issues, such as data packet scheduling in computer networks, can benefit from round-robin scheduling. It's a notion for an operating system.

Key Point

Waiting time : The entire time spent by the process in the ready state waiting for the CPU is known as waiting time.

Burst time : Every computer system operation takes some time to accomplish. Both CPU and I/O time are included in this calculation. The CPU time is the amount of time it takes for the CPU to finish a task. The amount of time it takes for a process to do an I/O operation is known as the I/O time. When analysing a process, we usually ignore I/O time and just consider CPU time. As a result, burst time is the total time it takes for the process to run on the CPU.

Turnaround time : The time interval between when a process is submitted and when it is completed is known as turnaround time (TAT). It can also be thought of as the sum of the time spent waiting to get into memory or the ready queue, as well as the time spent executing code on the CPU and executing input/output. Turnaround time is an important parameter for analysing an operating system's scheduling algorithms.

Program implementation

Here given code implementation process.

// C program 
// Implementation of Round Robin scheduling
#include <stdio.h>

// Calculate waiting time of given process by using quantum time
void find_waiting_time(int processes[], int bt[], int wt[], int quantum, int n)
{
	// Auxiliary space which is used to find waiting time
	int pending_bt[n];
	// Loop controlling variable
	int i = 0;
	// Get burst time
	for (i = 0; i < n; i++)
	{
		pending_bt[i] = bt[i];
	}
	// Current time 
	int process_time = 0;
	// work process indicator
	int work = 1;
	// Execute round robin process 
	// until work are not complete
	while (work == 1)
	{
		// Set that initial no work at this time
		work = 0;
		// Execute process one by one repeatedly
		for (i = 0; i < n; i++)
		{
			if (pending_bt[i] > 0)
			{
				// When pending process are exists
				// Active work
				work = 1;
				if (pending_bt[i] > quantum)
				{
					// Update the process time
					process_time += quantum;
					// Reduce padding burst time of current process
					pending_bt[i] -= quantum;
				}
				else
				{
					// Add the remains padding BT (burst time)
					process_time = process_time + pending_bt[i];
					// Get waiting time of i process
					wt[i] = process_time - bt[i];
					//Set that no remaining pending time
					pending_bt[i] = 0;
				}
			}
		}
	}
}
//This is calculating the average time by using given processes id,burst time and quantum  
void find_avg_time(int processes[], int burst_time[], int quantum, int n)
{
	// Auxiliary space to store waiting time and turnaround time
	int turnaround_time[n];
	int waiting_time[n];
	//Resultant variable
	double total_waiting_time = 0;
	double total_turnaround_time = 0;
	//Loop control variable
	int i = 0;
	find_waiting_time(processes, burst_time, waiting_time, quantum, n);
	// Calculate turnaround time 
	for (i = 0; i < n; ++i)
	{
		// Get turn around time for ith processes
		turnaround_time[i] = burst_time[i] + waiting_time[i];
	}
	printf(" (Process) (Burst Time) (Waiting Time) (Turn Around Time)");
	// Display process ,burst time, waiting time, turn around time ,
	// And calculate the average waiting time and average turn around time
	for (i = 0; i < n; i++)
	{
		// Calculate waiting time
		total_waiting_time += waiting_time[i];
		// Calculate turnaround time 
		total_turnaround_time += turnaround_time[i];
		printf("\n  p%d \t\t%d \t\t%d \t\t%d ", processes[i], burst_time[i], waiting_time[i], turnaround_time[i]);
	}
	//Display Result 
	printf("\n Average Waiting Time  : %lf", (total_waiting_time / n));
	printf("\n Average Turn Around Time  : %lf\n", (total_turnaround_time / n));
}
int main()
{
	//Process set
	int processes[] = {
		1,
		2,
		3,
		4
	};
	//Burst time of process set  
	int burst_time[] = {
		4,
		3,
		5,
		9
	};
	// Assume that size of process and burst time is equal
	// Get size
	int n = sizeof processes / sizeof processes[0];
	int quantum = 2;
	find_avg_time(processes, burst_time, quantum, n);
	return 0;
}

Output

 (Process) (Burst Time) (Waiting Time) (Turn Around Time)
  p1 		4 		6 		10
  p2 		3 		8 		11
  p3 		5 		11 		16
  p4 		9 		12 		21
 Average Waiting Time  : 9.250000
 Average Turn Around Time  : 14.500000
// Java program 
// Implementation of Round Robin scheduling
class RoundRobin
{
	// Calculate waiting time of given process by using quantum time
	public void find_waiting_time(int[] processes, int[] bt, int[] wt, int quantum, int n)
	{
		// Auxiliary space which is used to find waiting time
		int[] pending_bt = new int[n];
		// Loop controlling variable
		int i = 0;
		// Get burst time
		for (i = 0; i < n; i++)
		{
			pending_bt[i] = bt[i];
		}
		// Current time 
		int process_time = 0;
		// work process indicator
		boolean work = true;
		// Execute round robin process 
		// until work are not complete
		while (work == true)
		{
			// Set that initial no work at this time
			work = false;
			// Execute process one by one repeatedly
			for (i = 0; i < n; i++)
			{
				if (pending_bt[i] > 0)
				{
					// When pending process are exists
					// Active work
					work = true;
					if (pending_bt[i] > quantum)
					{
						// Update the process time
						process_time += quantum;
						// Reduce padding burst time of current process
						pending_bt[i] -= quantum;
					}
					else
					{
						// Add the remains padding BT (burst time)
						process_time = process_time + pending_bt[i];
						// Get waiting time of i process
						wt[i] = process_time - bt[i];
						//Set that no remaining pending time
						pending_bt[i] = 0;
					}
				}
			}
		}
	}
	//This is calculating the average time by using given processes id,burst time and quantum  
	public void find_avg_time(int[] processes, int[] burst_time, int quantum, int n)
	{
		// Auxiliary space to store waiting time and turnaround time
		int[] turnaround_time = new int[n];
		int[] waiting_time = new int[n];
		//Resultant variable
		double total_waiting_time = 0;
		double total_turnaround_time = 0;
		//Loop control variable
		int i = 0;
		find_waiting_time(processes, burst_time, waiting_time, quantum, n);
		// Calculate turnaround time 
		for (i = 0; i < n; ++i)
		{
			// Get turn around time for ith processes
			turnaround_time[i] = burst_time[i] + waiting_time[i];
		}
		System.out.print(" (Process) (Burst Time) (Waiting Time) (Turn Around Time)");
		// Display process ,burst time, waiting time, turn around time  , // And calculate the average waiting time and average turn around time
		for (i = 0; i < n; i++)
		{
			// Calculate waiting time
			total_waiting_time += waiting_time[i];
			// Calculate turnaround time 
			total_turnaround_time += turnaround_time[i];
			System.out.print("\n p" + processes[i] + " \t\t" + burst_time[i] + " \t\t" + waiting_time[i] + " \t\t" + turnaround_time[i] );
		}
		//Display Result 
		System.out.print("\n Average Waiting Time : " + (total_waiting_time / n) + "");
		System.out.print("\n Average Turn Around Time : " + (total_turnaround_time / n) + "\n");
	}
	public static void main(String args[])
	{
      	RoundRobin obj = new RoundRobin();
		//Process set
		int[] processes = {
			1,
			2,
			3,
			4
		};
		//Burst time of process set  
		int[] burst_time = {
			4,
			3,
			5,
			9
		};
		// Assume that size of process and burst time is equal
		// Get size
		int n = processes.length;
		int quantum = 2;
		obj.find_avg_time(processes, burst_time, quantum, n);
	}
}

Output

 (Process) (Burst Time) (Waiting Time) (Turn Around Time)
 p1 		4 		6 		10
 p2 		3 		8 		11
 p3 		5 		11 		16
 p4 		9 		12 		21
 Average Waiting Time : 9.25
 Average Turn Around Time : 14.5
//Include header file
#include <iostream>

using namespace std;
// C++ program 
// Implementation of Round Robin scheduling
class RoundRobin
{
	public:
		// Calculate waiting time of given process by using quantum time
		void find_waiting_time(int processes[], int bt[], int wt[], int quantum, int n)
		{
			// Auxiliary space which is used to find waiting time
			int pending_bt[n];
			// Loop controlling variable
			int i = 0;
			// Get burst time
			for (i = 0; i < n; i++)
			{
				pending_bt[i] = bt[i];
			}
			// Current time 
			int process_time = 0;
			// work process indicator
			bool work = true;
			// Execute round robin process 
			// until work are not complete
			while (work == true)
			{
				// Set that initial no work at this time
				work = false;
				// Execute process one by one repeatedly
				for (i = 0; i < n; i++)
				{
					if (pending_bt[i] > 0)
					{
						// When pending process are exists
						// Active work
						work = true;
						if (pending_bt[i] > quantum)
						{
							// Update the process time
							process_time += quantum;
							// Reduce padding burst time of current process
							pending_bt[i] -= quantum;
						}
						else
						{
							// Add the remains padding BT (burst time)
							process_time = process_time + pending_bt[i];
							// Get waiting time of i process
							wt[i] = process_time - bt[i];
							//Set that no remaining pending time
							pending_bt[i] = 0;
						}
					}
				}
			}
		}
	//This is calculating the average time by using given processes id,burst time and quantum  
	void find_avg_time(int processes[], int burst_time[], int quantum, int n)
	{
		// Auxiliary space to store waiting time and turnaround time
		int turnaround_time[n];
		int waiting_time[n];
		//Resultant variable
		double total_waiting_time = 0;
		double total_turnaround_time = 0;
		//Loop control variable
		int i = 0;
		this->find_waiting_time(processes, burst_time, waiting_time, quantum, n);
		// Calculate turnaround time 
		for (i = 0; i < n; ++i)
		{
			// Get turn around time for ith processes
			turnaround_time[i] = burst_time[i] + waiting_time[i];
		}
		cout << " (Process) (Burst Time) (Waiting Time) (Turn Around Time)";
		// Display process ,burst time, waiting time, turn around time  , // And calculate the average waiting time and average turn around time
		for (i = 0; i < n; i++)
		{
			// Calculate waiting time
			total_waiting_time += waiting_time[i];
			// Calculate turnaround time 
			total_turnaround_time += turnaround_time[i];
			cout << "\n p" << processes[i] << " \t\t" << burst_time[i] << " \t\t" << waiting_time[i] << " \t\t" << turnaround_time[i];
		}
		//Display Result 
		cout << "\n Average Waiting Time : " << (total_waiting_time / n) << "";
		cout << "\n Average Turn Around Time : " << (total_turnaround_time / n) << "\n";
	}
};
int main()
{
	RoundRobin obj = RoundRobin();
	//Process set
	int processes[] = {
		1 , 2 , 3 , 4
	};
	//Burst time of process set  
	int burst_time[] = {
		4 , 3 , 5 , 9
	};
	// Assume that size of process and burst time is equal
	// Get size
	int n = sizeof(processes) / sizeof(processes[0]);
	int quantum = 2;
	obj.find_avg_time(processes, burst_time, quantum, n);
	return 0;
}

Output

 (Process) (Burst Time) (Waiting Time) (Turn Around Time)
 p1 		4 		6 		10
 p2 		3 		8 		11
 p3 		5 		11 		16
 p4 		9 		12 		21
 Average Waiting Time : 9.25
 Average Turn Around Time : 14.5
//Include namespace system
using System;
// C# program 
// Implementation of Round Robin scheduling
class RoundRobin
{
	// Calculate waiting time of given process by using quantum time
	public void find_waiting_time(int[] processes, int[] bt, int[] wt, int quantum, int n)
	{
		// Auxiliary space which is used to find waiting time
		int[] pending_bt = new int[n];
		// Loop controlling variable
		int i = 0;
		// Get burst time
		for (i = 0; i < n; i++)
		{
			pending_bt[i] = bt[i];
		}
		// Current time 
		int process_time = 0;
		// work process indicator
		Boolean work = true;
		// Execute round robin process 
		// until work are not complete
		while (work == true)
		{
			// Set that initial no work at this time
			work = false;
			// Execute process one by one repeatedly
			for (i = 0; i < n; i++)
			{
				if (pending_bt[i] > 0)
				{
					// When pending process are exists
					// Active work
					work = true;
					if (pending_bt[i] > quantum)
					{
						// Update the process time
						process_time += quantum;
						// Reduce padding burst time of current process
						pending_bt[i] -= quantum;
					}
					else
					{
						// Add the remains padding BT (burst time)
						process_time = process_time + pending_bt[i];
						// Get waiting time of i process
						wt[i] = process_time - bt[i];
						//Set that no remaining pending time
						pending_bt[i] = 0;
					}
				}
			}
		}
	}
	//This is calculating the average time by using given processes id,burst time and quantum  
	public void find_avg_time(int[] processes, int[] burst_time, int quantum, int n)
	{
		// Auxiliary space to store waiting time and turnaround time
		int[] turnaround_time = new int[n];
		int[] waiting_time = new int[n];
		//Resultant variable
		double total_waiting_time = 0;
		double total_turnaround_time = 0;
		//Loop control variable
		int i = 0;
		find_waiting_time(processes, burst_time, waiting_time, quantum, n);
		// Calculate turnaround time 
		for (i = 0; i < n; ++i)
		{
			// Get turn around time for ith processes
			turnaround_time[i] = burst_time[i] + waiting_time[i];
		}
		Console.Write(" (Process) (Burst Time) (Waiting Time) (Turn Around Time)");
		// Display process ,burst time, waiting time, turn around time  , // And calculate the average waiting time and average turn around time
		for (i = 0; i < n; i++)
		{
			// Calculate waiting time
			total_waiting_time += waiting_time[i];
			// Calculate turnaround time 
			total_turnaround_time += turnaround_time[i];
			Console.Write("\n p" + processes[i] + " \t\t" + burst_time[i] + " \t\t" + waiting_time[i] + " \t\t" + turnaround_time[i]);
		}
		//Display Result 
		Console.Write("\n Average Waiting Time : " + (total_waiting_time / n) + "");
		Console.Write("\n Average Turn Around Time : " + (total_turnaround_time / n) + "\n");
	}
	public static void Main(String[] args)
	{
		RoundRobin obj = new RoundRobin();
		//Process set
		int[] processes = {
			1 , 2 , 3 , 4
		};
		//Burst time of process set  
		int[] burst_time = {
			4 , 3 , 5 , 9
		};
		// Assume that size of process and burst time is equal
		// Get size
		int n = processes.Length;
		int quantum = 2;
		obj.find_avg_time(processes, burst_time, quantum, n);
	}
}

Output

 (Process) (Burst Time) (Waiting Time) (Turn Around Time)
 p1 		4 		6 		10
 p2 		3 		8 		11
 p3 		5 		11 		16
 p4 		9 		12 		21
 Average Waiting Time : 9.25
 Average Turn Around Time : 14.5
<?php
// Php program 
// Implementation of Round Robin scheduling
class RoundRobin
{
	// Calculate waiting time of given process by using quantum time
	public	function find_waiting_time( & $processes, & $bt, & $wt, $quantum, $n)
	{
		// Auxiliary space which is used to find waiting time
		$pending_bt = array_fill(0, $n, 0);
		// Loop controlling variable
		$i = 0;
		// Get burst time
		for ($i = 0; $i < $n; $i++)
		{
			$pending_bt[$i] = $bt[$i];
		}
		// Current time 
		$process_time = 0;
		// work process indicator
		$work = true;
		// Execute round robin process 
		// until work are not complete
		while ($work == true)
		{
			// Set that initial no work at this time
			$work = false;
			// Execute process one by one repeatedly
			for ($i = 0; $i < $n; $i++)
			{
				if ($pending_bt[$i] > 0)
				{
					// When pending process are exists
					// Active work
					$work = true;
					if ($pending_bt[$i] > $quantum)
					{
						// Update the process time
						$process_time += $quantum;
						// Reduce padding burst time of current process
						$pending_bt[$i] -= $quantum;
					}
					else
					{
						// Add the remains padding BT (burst time)
						$process_time = $process_time + $pending_bt[$i];
						// Get waiting time of i process
						$wt[$i] = $process_time - $bt[$i];
						//Set that no remaining pending time
						$pending_bt[$i] = 0;
					}
				}
			}
		}
	}
	//This is calculating the average time by using given processes id,burst time and quantum  
	public	function find_avg_time( & $processes, & $burst_time, $quantum, $n)
	{
		// Auxiliary space to store waiting time and turnaround time
		$turnaround_time = array_fill(0, $n, 0);
		$waiting_time = array_fill(0, $n, 0);
		//Resultant variable
		$total_waiting_time = 0;
		$total_turnaround_time = 0;
		//Loop control variable
		$i = 0;
		$this->find_waiting_time($processes, $burst_time, $waiting_time, $quantum, $n);
		// Calculate turnaround time 
		for ($i = 0; $i < $n; ++$i)
		{
			// Get turn around time for ith processes
			$turnaround_time[$i] = $burst_time[$i] + $waiting_time[$i];
		}
		echo " (Process) (Burst Time) (Waiting Time) (Turn Around Time)";
		// Display process ,burst time, waiting time, turn around time  , // And calculate the average waiting time and average turn around time
		for ($i = 0; $i < $n; $i++)
		{
			// Calculate waiting time
			$total_waiting_time += $waiting_time[$i];
			// Calculate turnaround time 
			$total_turnaround_time += $turnaround_time[$i];
			echo "\n p". $processes[$i] ." \t\t". $burst_time[$i] ." \t\t". $waiting_time[$i] ." \t\t". $turnaround_time[$i];
		}
		//Display Result 
		echo "\n Average Waiting Time : ". (($total_waiting_time / $n));
		echo "\n Average Turn Around Time : ". (($total_turnaround_time / $n)) ."\n";
	}
}

function main()
{
	$obj = new RoundRobin();
	//Process set
	$processes = array(1, 2, 3, 4);
	//Burst time of process set  
	$burst_time = array(4, 3, 5, 9);
	// Assume that size of process and burst time is equal
	// Get size
	$n = count($processes);
	$quantum = 2;
	$obj->find_avg_time($processes, $burst_time, $quantum, $n);
}
main();

Output

 (Process) (Burst Time) (Waiting Time) (Turn Around Time)
 p1 		4 		6 		10
 p2 		3 		8 		11
 p3 		5 		11 		16
 p4 		9 		12 		21
 Average Waiting Time : 9.25
 Average Turn Around Time : 14.5
// Node Js program 
// Implementation of Round Robin scheduling
class RoundRobin
{
	// Calculate waiting time of given process by using quantum time
	find_waiting_time(processes, bt, wt, quantum, n)
	{
		// Auxiliary space which is used to find waiting time
		var pending_bt = Array(n).fill(0);
		// Loop controlling variable
		var i = 0;
		// Get burst time
		for (i = 0; i < n; i++)
		{
			pending_bt[i] = bt[i];
		}
		// Current time 
		var process_time = 0;
		// work process indicator
		var work = true;
		// Execute round robin process 
		// until work are not complete
		while (work == true)
		{
			// Set that initial no work at this time
			work = false;
			// Execute process one by one repeatedly
			for (i = 0; i < n; i++)
			{
				if (pending_bt[i] > 0)
				{
					// When pending process are exists
					// Active work
					work = true;
					if (pending_bt[i] > quantum)
					{
						// Update the process time
						process_time += quantum;
						// Reduce padding burst time of current process
						pending_bt[i] -= quantum;
					}
					else
					{
						// Add the remains padding BT (burst time)
						process_time = process_time + pending_bt[i];
						// Get waiting time of i process
						wt[i] = process_time - bt[i];
						//Set that no remaining pending time
						pending_bt[i] = 0;
					}
				}
			}
		}
	}
	//This is calculating the average time by using given processes id,burst time and quantum  
	find_avg_time(processes, burst_time, quantum, n)
	{
		// Auxiliary space to store waiting time and turnaround time
		var turnaround_time = Array(n).fill(0);
		var waiting_time = Array(n).fill(0);
		//Resultant variable
		var total_waiting_time = 0;
		var total_turnaround_time = 0;
		//Loop control variable
		var i = 0;
		this.find_waiting_time(processes, burst_time, waiting_time, quantum, n);
		// Calculate turnaround time 
		for (i = 0; i < n; ++i)
		{
			// Get turn around time for ith processes
			turnaround_time[i] = burst_time[i] + waiting_time[i];
		}
		process.stdout.write(" (Process) (Burst Time) (Waiting Time) (Turn Around Time)");
		// Display process ,burst time, waiting time, turn around time  
        // And calculate the average waiting time and average turn around time
		for (i = 0; i < n; i++)
		{
			// Calculate waiting time
			total_waiting_time += waiting_time[i];
			// Calculate turnaround time 
			total_turnaround_time += turnaround_time[i];
			process.stdout.write("\n p" + processes[i] + " \t\t" + burst_time[i] + " \t\t" + waiting_time[i] + " \t\t" + turnaround_time[i]);
		}
		//Display Result 
		process.stdout.write("\n Average Waiting Time : " + (total_waiting_time / n));
		process.stdout.write("\n Average Turn Around Time : " + (total_turnaround_time / n) + "\n");
	}
}

function main()
{
	var obj = new RoundRobin();
	//Process set
	var processes = [1, 2, 3, 4];
	//Burst time of process set  
	var burst_time = [4, 3, 5, 9];
	// Assume that size of process and burst time is equal
	// Get size
	var n = processes.length;
	var quantum = 2;
	obj.find_avg_time(processes, burst_time, quantum, n);
}
main();

Output

 (Process) (Burst Time) (Waiting Time) (Turn Around Time)
 p1 		4 		6 		10
 p2 		3 		8 		11
 p3 		5 		11 		16
 p4 		9 		12 		21
 Average Waiting Time : 9.25
 Average Turn Around Time : 14.5
#  Python 3 program 
#  Implementation of Round Robin scheduling
class RoundRobin :
	#  Calculate waiting time of given process by using quantum time
	def find_waiting_time(self, processes, bt, wt, quantum, n) :
		#  Auxiliary space which is used to find waiting time
		pending_bt = [0] * n
		#  Loop controlling variable
		i = 0
		#  Get burst time
		while (i < n) :
			pending_bt[i] = bt[i]
			i += 1
		
		#  Current time 
		process_time = 0
		#  work process indicator
		work = True
		#  Execute round robin process 
		#  until work are not complete
		while (work == True) :
			#  Set that initial no work at this time
			work = False
			#  Execute process one by one repeatedly
			i = 0
			while (i < n) :
				if (pending_bt[i] > 0) :
					#  When pending process are exists
					#  Active work
					work = True
					if (pending_bt[i] > quantum) :
						#  Update the process time
						process_time += quantum
						#  Reduce padding burst time of current process
						pending_bt[i] -= quantum
					else :
						#  Add the remains padding BT (burst time)
						process_time = process_time + pending_bt[i]
						#  Get waiting time of i process
						wt[i] = process_time - bt[i]
						# Set that no remaining pending time
						pending_bt[i] = 0
					
				
				i += 1
			
		
	
	# This is calculating the average time by using given processes id,burst time and quantum  
	def find_avg_time(self, processes, burst_time, quantum, n) :
		#  Auxiliary space to store waiting time and turnaround time
		turnaround_time = [0] * n
		waiting_time = [0] * n
		# Resultant variable
		total_waiting_time = 0
		total_turnaround_time = 0
		# Loop control variable
		i = 0
		self.find_waiting_time(processes, burst_time, waiting_time, quantum, n)
		#  Calculate turnaround time 
		while (i < n) :
			#  Get turn around time for ith processes
			turnaround_time[i] = burst_time[i] + waiting_time[i]
			i += 1
		
		print(" (Process) (Burst Time) (Waiting Time) (Turn Around Time)", end = "")
		#  Display process ,burst time, waiting time, turn around time  
		#  And calculate the average waiting time and average turn around time
		i = 0
		while (i < n) :
			#  Calculate waiting time
			total_waiting_time += waiting_time[i]
			#  Calculate turnaround time 
			total_turnaround_time += turnaround_time[i]
			print("\n p", processes[i] ," \t\t", burst_time[i] ," \t\t", waiting_time[i] ," \t\t", turnaround_time[i], end = "")
			i += 1
		
		# Display Result 
		print("\n Average Waiting Time : ", (total_waiting_time / n))
		print(" Average Turn Around Time : ", (total_turnaround_time / n) )
	

def main() :
	obj = RoundRobin()
	# Process set
	processes = [1, 2, 3, 4]
	# Burst time of process set  
	burst_time = [4, 3, 5, 9]
	#  Assume that size of process and burst time is equal
	#  Get size
	n = len(processes)
	quantum = 2
	obj.find_avg_time(processes, burst_time, quantum, n)

if __name__ == "__main__": main()

Output

 (Process) (Burst Time) (Waiting Time) (Turn Around Time)
 p 1  		 4  		 6  		 10
 p 2  		 3  		 8  		 11
 p 3  		 5  		 11  		 16
 p 4  		 9  		 12  		 21
 Average Waiting Time :  9.25
 Average Turn Around Time :  14.5
#  Ruby program 
#  Implementation of Round Robin scheduling
class RoundRobin

	#  Calculate waiting time of given process by using quantum time
	def find_waiting_time(processes, bt, wt, quantum, n)
	
		#  Auxiliary space which is used to find waiting time
		pending_bt = Array.new(n) {0}
		#  Loop controlling variable
		i = 0
		#  Get burst time
		while (i < n)
		
			pending_bt[i] = bt[i]
			i += 1
		end
		#  Current time 
		process_time = 0
		#  work process indicator
		work = true
		#  Execute round robin process 
		#  until work are not complete
		while (work == true)
		
			#  Set that initial no work at this time
			work = false
			#  Execute process one by one repeatedly
			i = 0
			while (i < n)
			
				if (pending_bt[i] > 0)
				
					#  When pending process are exists
					#  Active work
					work = true
					if (pending_bt[i] > quantum)
					
						#  Update the process time
						process_time += quantum
						#  Reduce padding burst time of current process
						pending_bt[i] -= quantum
					else
					
						#  Add the remains padding BT (burst time)
						process_time = process_time + pending_bt[i]
						#  Get waiting time of i process
						wt[i] = process_time - bt[i]
						# Set that no remaining pending time
						pending_bt[i] = 0
					end
				end
				i += 1
			end
		end
	end
	# This is calculating the average time by using given processes id,burst time and quantum  
	def find_avg_time(processes, burst_time, quantum, n)
	
		#  Auxiliary space to store waiting time and turnaround time
		turnaround_time = Array.new(n) {0}
		waiting_time = Array.new(n) {0}
		# Resultant variable
		total_waiting_time = 0.0
		total_turnaround_time = 0.0
		# Loop control variable
		i = 0
		self.find_waiting_time(processes, burst_time, waiting_time, quantum, n)
		#  Calculate turnaround time 
		while (i < n)
		
			#  Get turn around time for ith processes
			turnaround_time[i] = burst_time[i] + waiting_time[i]
			i += 1
		end
		print(" (Process) (Burst Time) (Waiting Time) (Turn Around Time)")
		#  Display process ,burst time, waiting time, turn around time  
		#  And calculate the average waiting time and average turn around time
		i = 0
		while (i < n)
		
			#  Calculate waiting time
			total_waiting_time += waiting_time[i]
			#  Calculate turnaround time 
			total_turnaround_time += turnaround_time[i]
			print("\n p", processes[i] ," \t\t", burst_time[i] ," \t\t", waiting_time[i] ," \t\t", turnaround_time[i])
			i += 1
		end
		# Display Result 
		print("\n Average Waiting Time : ", (total_waiting_time / n))
		print("\n Average Turn Around Time : ", (total_turnaround_time / n) ,"\n")
	end
end
def main()

	obj = RoundRobin.new()
	# Process set
	processes = [1, 2, 3, 4]
	# Burst time of process set  
	burst_time = [4, 3, 5, 9]
	#  Assume that size of process and burst time is equal
	#  Get size
	n = processes.length
	quantum = 2
	obj.find_avg_time(processes, burst_time, quantum, n)
end
main()

Output

 (Process) (Burst Time) (Waiting Time) (Turn Around Time)
 p1 		4 		6 		10
 p2 		3 		8 		11
 p3 		5 		11 		16
 p4 		9 		12 		21
 Average Waiting Time : 9.25
 Average Turn Around Time : 14.5
// Scala program 
// Implementation of Round Robin scheduling
class RoundRobin
{
	// Calculate waiting time of given process by using quantum time
	def find_waiting_time(processes: Array[Int], bt: Array[Int], wt: Array[Int], quantum: Int, n: Int): Unit = {
		// Auxiliary space which is used to find waiting time
		var pending_bt: Array[Int] = Array.fill[Int](n)(0);
		// Loop controlling variable
		var i: Int = 0;
		// Get burst time
		while (i < n)
		{
			pending_bt(i) = bt(i);
			i += 1;
		}
		// Current time 
		var process_time: Int = 0;
		// work process indicator
		var work: Boolean = true;
		// Execute round robin process 
		// until work are not complete
		while (work == true)
		{
			// Set that initial no work at this time
			work = false;
			// Execute process one by one repeatedly
			i = 0;
			while (i < n)
			{
				if (pending_bt(i) > 0)
				{
					// When pending process are exists
					// Active work
					work = true;
					if (pending_bt(i) > quantum)
					{
						// Update the process time
						process_time += quantum;
						// Reduce padding burst time of current process
						pending_bt(i) -= quantum;
					}
					else
					{
						// Add the remains padding BT (burst time)
						process_time = process_time + pending_bt(i);
						// Get waiting time of i process
						wt(i) = process_time - bt(i);
						//Set that no remaining pending time
						pending_bt(i) = 0;
					}
				}
				i += 1;
			}
		}
	}
	//This is calculating the average time by using given processes id,burst time and quantum  
	def find_avg_time(processes: Array[Int], burst_time: Array[Int], quantum: Int, n: Int): Unit = {
		// Auxiliary space to store waiting time and turnaround time
		var turnaround_time: Array[Int] = Array.fill[Int](n)(0);
		var waiting_time: Array[Int] = Array.fill[Int](n)(0);
		//Resultant variable
		var total_waiting_time: Double = 0.0;
		var total_turnaround_time: Double = 0.0;
		//Loop control variable
		var i: Int = 0;
		find_waiting_time(processes, burst_time, waiting_time, quantum, n);
		// Calculate turnaround time 
		while (i < n)
		{
			// Get turn around time for ith processes
			turnaround_time(i) = burst_time(i) + waiting_time(i);
			i += 1;
		}
		print(" (Process) (Burst Time) (Waiting Time) (Turn Around Time)");
		// Display process ,burst time, waiting time, turn around time  
		// And calculate the average waiting time and average turn around time
		i = 0;
		while (i < n)
		{
			// Calculate waiting time
			total_waiting_time += waiting_time(i);
			// Calculate turnaround time 
			total_turnaround_time += turnaround_time(i);
			print("\n p" + processes(i) + " \t\t" + burst_time(i) + " \t\t" + waiting_time(i) + " \t\t" + turnaround_time(i));
			i += 1;
		}
		//Display Result 
		print("\n Average Waiting Time : " + (total_waiting_time / n));
		print("\n Average Turn Around Time : " + (total_turnaround_time / n) + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: RoundRobin = new RoundRobin();
		//Process set
		var processes: Array[Int] = Array(1, 2, 3, 4);
		//Burst time of process set  
		var burst_time: Array[Int] = Array(4, 3, 5, 9);
		// Assume that size of process and burst time is equal
		// Get size
		var n: Int = processes.length;
		var quantum: Int = 2;
		obj.find_avg_time(processes, burst_time, quantum, n);
	}
}

Output

 (Process) (Burst Time) (Waiting Time) (Turn Around Time)
 p1 		4 		6 		10
 p2 		3 		8 		11
 p3 		5 		11 		16
 p4 		9 		12 		21
 Average Waiting Time : 9.25
 Average Turn Around Time : 14.5
// Swift program 
// Implementation of Round Robin scheduling
class RoundRobin
{
	// Calculate waiting time of given process by using quantum time
	func find_waiting_time(_ processes: [Int], _ bt: [Int], _ wt: inout[Int], _ quantum: Int, _ n: Int)
	{
		// Auxiliary space which is used to find waiting time
		var pending_bt: [Int] = Array(repeating: 0, count: n);
		// Loop controlling variable
		var i: Int = 0;
		// Get burst time
		while (i < n)
		{
			pending_bt[i] = bt[i];
			i += 1;
		}
		// Current time 
		var process_time: Int = 0;
		// work process indicator
		var work: Bool = true;
		// Execute round robin process 
		// until work are not complete
		while (work == true)
		{
			// Set that initial no work at this time
			work = false;
			// Execute process one by one repeatedly
			i = 0;
			while (i < n)
			{
				if (pending_bt[i] > 0)
				{
					// When pending process are exists
					// Active work
					work = true;
					if (pending_bt[i] > quantum)
					{
						// Update the process time
						process_time += quantum;
						// Reduce padding burst time of current process
						pending_bt[i] -= quantum;
					}
					else
					{
						// Add the remains padding BT (burst time)
						process_time = process_time + pending_bt[i];
						// Get waiting time of i process
						wt[i] = process_time - bt[i];
						//Set that no remaining pending time
						pending_bt[i] = 0;
					}
				}
				i += 1;
			}
		}
	}
	//This is calculating the average time by using given processes id,burst time and quantum  
	func find_avg_time(_ processes: [Int], _ burst_time: [Int], _ quantum: Int, _ n: Int)
	{
		// Auxiliary space to store waiting time and turnaround time
		var turnaround_time: [Int] = Array(repeating: 0, count: n);
		var waiting_time: [Int] = Array(repeating: 0, count: n);
		//Resultant variable
		var total_waiting_time: Double = 0;
		var total_turnaround_time: Double = 0;
		//Loop control variable
		var i: Int = 0;
		self.find_waiting_time(processes, burst_time, &waiting_time, quantum, n);
		// Calculate turnaround time 
		while (i < n)
		{
			// Get turn around time for ith processes
			turnaround_time[i] = burst_time[i] + waiting_time[i];
			i += 1;
		}
		print(" (Process) (Burst Time) (Waiting Time) (Turn Around Time)", terminator: "");
		// Display process ,burst time, waiting time, turn around time  
		// And calculate the average waiting time and average turn around time
		i = 0;
		while (i < n)
		{
			// Calculate waiting time
			total_waiting_time += Double(waiting_time[i]);
			// Calculate turnaround time 
			total_turnaround_time += Double(turnaround_time[i]);
			print("\n  p\(processes[i])  \t\t", burst_time[i] ," \t\t", waiting_time[i] ," \t\t", turnaround_time[i], terminator: "");
			i += 1;
		}
		//Display Result 
		print("\n Average Waiting Time : ", (total_waiting_time / Double(n)), terminator: "");
		print("\n Average Turn Around Time : ", (total_turnaround_time / Double(n)) ,"\n", terminator: "");
	}
}
func main()
{
	let obj: RoundRobin = RoundRobin();
	//Process set
	let processes: [Int] = [1, 2, 3, 4];
	//Burst time of process set  
	let burst_time: [Int] = [4, 3, 5, 9];
	// Assume that size of process and burst time is equal
	// Get size
	let n: Int = processes.count;
	let quantum: Int = 2;
	obj.find_avg_time(processes, burst_time, quantum, n);
}
main();

Output

 (Process) (Burst Time) (Waiting Time) (Turn Around Time)
  p1  		 4  		 6  		 10
  p2  		 3  		 8  		 11
  p3  		 5  		 11  		 16
  p4  		 9  		 12  		 21
 Average Waiting Time :  9.25
 Average Turn Around Time :  14.5

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







Big dick     567 Day ago
Test