In: Computer Science
In C++, assuming you have the following incomplete code:
#include<iostream>
#include <unistd.h>
using namespace std;
// Structure for storing the process data
struct procData
{
char pname[5]; // Name of a process
int arrivt; //Arrival time of a process
int pburst; // Burst time of a process
int endtime; // Exit time/ Leaving time of a process
int remburst; // Remaining burst time of a process
int readyFlag; // boolean, Flag for maintaining the process
status
};
// Global variable
int qTime; // Time quantum
// Function prototypes
procData* procIPData(int ); // Input the processes data in the
global array
void calculate(procData*, int); // Calculate the average waiting
and turn around time
void display_waittime(procData*, int); // Display the average and
waiting and turn around time
int main()
{
procData *q;
int noProcess; // Number of processes
cout << "Enter Total no. of processes" << endl;
cin >> noProcess;
q = procIPData(noProcess);
calculate(q, noProcess);
display_waittime(q, noProcess);
return 0;
}
procData* procIPData(int noProc)
{
procData *a = new procData[noProc];
for (int i = 0; i < noProc; i++)
{
cout << "Enter process name:" << endl;
cin >> a[i].pname;
cout << "Enter process burst time:" << endl;
cin >> a[i].pburst;
cout << "Enter process arrival time:" << endl;
cin >> a[i].arrivt;
a[i].remburst = a[i].pburst;
a[i].endtime = 0;
a[i].readyFlag = 0;
}
cout << "Enter the time quantum/Time Slice:" <<
endl;
cin >> qTime;
return a;
}
void calculate(procData* a, int noProc)
{
// To be completed
}
void display_waittime(procData* a, int noProc)
{
int totalTA = 0, totalWait = 0, ta = 0, tw = 0;
for (int i = 0; i < noProc; i++)
{
ta = a[i].endtime - a[i].arrivt;
tw = ta - a[i].pburst;
cout << endl << "Waiting time for Process " <<
a[i].pname << " is " << tw << endl;
totalTA = totalTA + ta;
totalWait = totalWait + tw;
}
cout << "Average waiting time = " << ((float) totalWait
/ (float) noProc) << endl;
cout << "Average turnaround time= " << ((float) totalTA
/ (float) noProc) << endl;
}
Complete the C++ program so that it displays the Gantt chart, average waiting time and average turnaround time for the list of process using pre-emptive Round-robin CPU scheduling strategy. The program takes the CPU burst times and arrival times for the given list of processes as input.
The solution for above problem is as follows:-
Code-
#include<iostream>
#include <unistd.h>
#include<algorithm>
using namespace std;
// Structure for storing the process data
struct procData
{
char pname[5]; // Name of a process
int arrivt; //Arrival time of a process
int pburst; // Burst time of a process
int endtime; // Exit time/ Leaving time of a
process
int remburst; // Remaining burst time of a
process
int readyFlag; // boolean, Flag for maintaining the
process status
};
// Global variable
int qTime; // Time quantum
// Function prototypes
procData* procIPData(int noProc); // Input the processes data in
the global array
void calculate(procData*, int); // Calculate the average waiting
and turn around time
void display_waittime(procData*, int); // Display the average and
waiting and turn around time
int main()
{
procData *q;
int noProcess; // Number of processes
cout << "Enter Total no. of processes" <<
endl;
cin >> noProcess;
q = procIPData(noProcess);
calculate(q, noProcess);
display_waittime(q, noProcess);
return 0;
}
procData* procIPData(int noProc)
{
procData *a = new procData[noProc];
for (int i = 0; i < noProc; i++)
{
cout << "Enter process name:"
<< endl;
cin >> a[i].pname;
cout << "Enter process burst
time:" << endl;
cin >> a[i].pburst;
cout << "Enter process
arrival time:" << endl;
cin >> a[i].arrivt;
a[i].remburst = a[i].pburst;
a[i].endtime = 0;
a[i].readyFlag = 0;
}
cout << "Enter the time quantum/Time Slice:"
<< endl;
cin >> qTime;
return a;
}
bool compare(procData a,procData b){
return a.arrivt<b.arrivt;
}
void calculate(procData* a, int noProc)
{
sort(a,a+noProc,compare);//Sorting processes accoridng
to arrival time;
int time,count,flag=0,remain=noProc;
for(time=0,count=0;remain!=0;)
{
if(a[count].remburst<=qTime
&& a[count].remburst>0)
{
time+=a[count].remburst;
a[count].remburst=0;
flag=1;
}
else
if(a[count].remburst>0)
{
a[count].remburst-=qTime;
time+=qTime;
}
if(a[count].remburst==0 &&
flag==1)
{
remain--;
a[count].endtime=time;
flag=0;
}
if(count==noProc-1)
count=0;
else
if(a[count+1].arrivt<=time)
count++;
else
count=0;
}
}
void display_waittime(procData* a, int noProc)
{
int totalTA = 0, totalWait = 0, ta = 0, tw = 0;
for (int i = 0; i < noProc; i++)
{
ta = a[i].endtime -
a[i].arrivt;
tw = ta - a[i].pburst;
cout << endl <<
"Waiting time for Process " << a[i].pname << " is "
<< tw << endl;
totalTA = totalTA + ta;
totalWait = totalWait + tw;
}
cout << "Average waiting time = " <<
((float) totalWait / (float) noProc) << endl;
cout << "Average turnaround time= " <<
((float) totalTA / (float) noProc) << endl;
}
Code screenshots-
Outputs-
Feel free to comment for any issues, do rate the answer positively