Question

In: Computer Science

In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; //...

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.

Solutions

Expert Solution

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


Related Solutions

I have a error code, for my C++ class, using Putty include <iostream> using namespace std;...
I have a error code, for my C++ class, using Putty include <iostream> using namespace std; int main() { char answer; char grade; cout << "Are you taking a class (enter y, Y, N or N): " << endl; cin >> answer; if (answer == 'N' || 'n'); { cout << "Thanks for using the system" << endl; } else if (answer == 'Y' || 'y'); { cout << "Enter a letter grade (A, B, C, D, or F): "...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to store user input bool cont = true; //implement a loop so that it will continue asking until the user provides a positive integer // the following provides ONLY part of the loop body, which you should complete { cout <<"How many words are in your message? \n"; cout <<"Enter value: "; // get user input integer here    cout <<"\nInvalid value. Please Re-enter a...
Plz convert this C++ code into JAVA code thanks #include<iostream> using namespace std; //function for calculating...
Plz convert this C++ code into JAVA code thanks #include<iostream> using namespace std; //function for calculating the average sightings from the Total Sightings array float calcAverage(float totalSightings[],int n) {    int i;    float sum=0.0;    for(i=0;i<n;i++)    sum=sum+totalSightings[i];    return sum/n; } int main() {    // n is no. of bird watchers    //flag , flag2 and flag3 are for validating using while loops    int n,i,flag,flag2,flag3;       //ch also helps in validating    char ch;   ...
I need the following code completed: #include <iostream> #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================...
I need the following code completed: #include <iostream> #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() { amount = 0.0; } }; // FIXME (1): Internal structure for tree node struct Node {    Bid data; Node *left; Node...
Question 3 A. Show the output of the following code. #include <iostream> using namespace std; void...
Question 3 A. Show the output of the following code. #include <iostream> using namespace std; void magic (int &a, int b, int& c) {   a *= 2;    b = b+2;    c = c-2; } int main () { int x=1, y=3, z=7;    magic (x, y, z); cout << "x=" << x << ", y=" << y << ", z=" << z;    magic (z, y, x); cout << "x=" << x << ", y=" << y <<...
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate...
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate number non white space characters int GetNumOfNonWSCharacters(string str) { int i = 0; int count = 0; while(str[i] != '\0') { if(str[i] != ' ') { count += 1; } i++; } return count; } // function to calculate numbers of words int GetNumOfWords(string str) { int i = 0; int count = 1; while(str[i] != '\0') { if(str[i] == ' ' && str[i-1]...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function name: main Purpose:                   main function In parameters: b,r,i Out paramters: trun,error,total,value Version:                   1.0 Author: ********************************************************************************/ void main() {    int i;//declaring this variable to get value for quitting or calaculating series    do {//do while loop to calaculate series until user quits        cout << "Enter 1 to evaluate the series." << endl;       ...
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned...
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned char c1; unsigned char c2; unsigned char c3; unsigned char c4; unsigned long i1 (0xaabbccee); _asm { } cout.flags (ios::hex); cout << "results are " << (unsigned int) c1 << ", " << (unsigned int) c2 << ", " << (unsigned int) c3 << ", " << (unsigned int) c4 << endl; } Inside the block denoted by the _asm keyword, add code to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT