Answer to Question #327096 in C++ for naqibullah

Question #327096

write the code for round robin scheduling algorithm?


1
Expert's answer
2022-04-13T07:33:09-0400
#include<iostream>
using namespace std;

void findWaitingTime(int processes[], int n,
	int bt[], int wt[], int quantum)
{
	int* rem_bt = new int[n];
	for (int i = 0; i < n; i++)
		rem_bt[i] = bt[i];


	int t = 0; 
	while (1)
	{
		bool done = true;
		for (int i = 0; i < n; i++)
		{
			if (rem_bt[i] > 0)
			{
				done = false; // There is a pending process
				if (rem_bt[i] > quantum)
				{
					t += quantum;
					rem_bt[i] -= quantum;
				}
				else
				{
					t = t + rem_bt[i];
					wt[i] = t - bt[i];
					rem_bt[i] = 0;
				}
			}
		}
		if (done == true)
			break;
	}
}

void findTurnAroundTime(int processes[], int n,
	int bt[], int wt[], int tat[])
{
	for (int i = 0; i < n; i++)
		tat[i] = bt[i] + wt[i];
}

void findavgTime(int processes[], int n, int bt[],
	int quantum)
{
	int total_wt = 0, total_tat = 0;
	int* wt = new int[n];
	int* tat = new int[n];
	findWaitingTime(processes, n, bt, wt, quantum);
	findTurnAroundTime(processes, n, bt, wt, tat);
	cout << "Processes " << " Burst time "
		<< " Waiting time " << " Turn around time\n";


	for (int i = 0; i<n; i++)
	{
		total_wt = total_wt + wt[i];
		total_tat = total_tat + tat[i];
		cout << " " << i + 1 << "\t\t" << bt[i] << "\t "
			<< wt[i] << "\t\t " << tat[i] << endl;
	}


	cout << "Average waiting time = "
		<< (float)total_wt / (float)n;
	cout << "\nAverage turn around time = "
		<< (float)total_tat / (float)n;
}


int main()
{
	// process id's
	int processes[] = { 1, 2, 3 };
	int n = sizeof processes / sizeof processes[0];


	// Burst time of all processes
	int burst_time[] = { 10, 5, 8 };


	// Time quantum
	int quantum = 2;
	findavgTime(processes, n, burst_time, quantum);
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS