16. Write a program that will simulate FCFS, SJN, SRT, and Round Robin heduling
ID: 3738278 • Letter: 1
Question
16. Write a program that will simulate FCFS, SJN, SRT, and Round Robin heduling algorithms. For each algorithm, the program should compute waiting time and turnaround time of every job as well as the average waiting time and average turnaround time. The average values should be consolidated in a table for easy comparison. You may use the following data to test your program. The time quantum for Round Robin is 4 ms. (Assume that the context switching time is 0). Job Arrival Times CPU Cycle (in milliseconds) 10 12Explanation / Answer
ScreenShot
------------------------------------------------------------------------------------------------------
ProgramCode
//Header file
#include<iostream>
#include<string>
#include <algorithm>
using namespace std;
//Main Method
int main()
{
//Variable declaration
int n = 0, i, j, bt[20],bt1[20], wt[20], tat[20], avwt = 0, avtat = 0, arr[20], cput[20],t=0,pos,temp;
char p[20],p1[20],c;
int count,time, remain, flag = 0,quantum;
int wait_time = 0, turnaround_time = 0;
//Ask user the count of processess
cout << "Enter total number of processes(maximum 20):";
cin >> n;
//Set values
p[n];
bt[n], wt[n], tat[n];
remain = n;
//Enter arriving ang cpu time calculate burst time
cout << " Enter Process and Burst Time ";
for (i = 0; i<n; i++)
{
cout << "Enter Process name :";
cin >> c;
p[i] = c;
p1[i] = c;
cout << "Enter cpu time time :";
cin >> cput[i];
cout << "Enter arrival time :";
cin >> arr[i];
t+= cput[i] - arr[i];
bt[i] = t;
bt1[i] = t;
}
//Calculate waiting time
wt[0] = 0; //waiting time for first process is 0
//calculating waiting time
for (i = 1; i<n; i++)
{
wt[i] = 0;
for (j = 0; j<i; j++)
wt[i] += bt[j];
}
cout << " Process Execution using FCFS ";
cout << " Process Burst Time Waiting Time Turnaround Time";
//calculating turnaround time
for (i = 0; i<n; i++)
{
tat[i] = bt[i] + wt[i];
avwt += wt[i];
avtat += tat[i];
cout << " "<<p[i] << " " << bt[i] << " " << wt[i] << " " << tat[i];
}
avwt /= i;
avtat /= i;
cout << " Average Waiting Time:" << avwt;
cout << " Average Turnaround Time:" << avtat<<endl;//End of FCFS
//Start of SJN scheduling
for (i = 0; i<n; i++) //Sorting loop
{
pos = i;
for (j = i + 1; j<n; j++)
{
if (bt1[j]<bt1[pos])
pos = j;
}
temp = bt1[i];
bt1[i] = bt1[pos];
bt1[pos] = temp;
temp = p1[i];
p1[i] = p1[pos];
p1[pos] = temp;
}
wt[0] = 0; //waiting time for first process is 0
//calculating waiting time
for (i = 1; i<n; i++)
{
wt[i] = 0;
for (j = 0; j<i; j++)
wt[i] += bt1[j];
}
cout << " Process Execution using SJF ";
cout << " Process Burst Time Waiting Time Turnaround Time";
//calculating turnaround time
for (i = 0; i<n; i++)
{
tat[i] = bt1[i] + wt[i];
avwt += wt[i];
avtat += tat[i];
cout << " " << p1[i] << " " << bt1[i] << " " << wt[i] << " " << tat[i];
}
avwt /= i;
avtat /= i;
cout << " Average Waiting Time:" << avwt;
cout << " Average Turnaround Time:" << avtat << endl;//end of sjn
//Round robin scheduling
cout<<endl<<"Enter Time Quantum: ";
cin>>quantum;
cout << " Roun robin scheduling" << endl;
printf(" Process |Turnaround Time|Waiting Time ");
for (time = 0, count = 0; remain != 0;)
{
if (tat[count] <= quantum && tat[count]>0)
{
time += tat[count];
tat[count] = 0;
flag = 1;
}
else if (tat[count]>0)
{
tat[count] -= quantum;
time += quantum;
}
if (tat[count] == 0 && flag == 1)
{
remain--;
printf("P[%d] | %d | %d ", count + 1, time - arr[count], time - arr[count] - bt[count]);
wait_time += time - arr[count] - bt[count];
turnaround_time += time - arr[count];
flag = 0;
}
if (count == n - 1)
count = 0;
else if (arr[count + 1] <= time)
count++;
else
count = 0;
}
printf(" Average Waiting Time= %f ", wait_time*1.0 / n);
printf("Avg Turnaround Time = %f", turnaround_time*1.0 / n);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.