Each time a process terminates the program should output: time elapsed, state of
ID: 3647186 • Letter: E
Question
Each time a process terminates the program should output: time elapsed, state of CPU,each process in main memory one line with: the sequence number of the process, its start time, READY, RUNNING, WAITING or TERMINATED#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
class CPU
{
public:
int endtime;
void isbusy(int &end)
{
end = end-1;
}
CPU(int _endtime)
{
endtime=_endtime;
}
};
class Command //create command class which holds the variables and the string of the user input
{
public:
int k;
string input; //.txt file used to input information into the program made into a string
void output ()
{
cout << input << " " << k << endl;
}
Command(int _k, string _input)
{
k = _k;
input = _input;
}
};
class Process
{
public:
int startTime;
queue <Command> commandString;
void output()
{
queue <Command> temp;
cout << startTime << endl;
while(!commandString.empty())
{
commandString.front().output();
temp.push(commandString.front());
commandString.pop();
}
while(!temp.empty())
{
commandString.push(temp.front());
temp.pop();
}
}
Process (int _startTime)//the "time" to start
{
startTime = _startTime;
}
void addCommand (Command _sp)
{
commandString.push(_sp);
}
};
void BubbleSort(vector <Process> &num)
{
int i, j, flag = 1;
Process temp = 0; // holding variable
int numLength = num.size( );
for(i = 1; (i <= numLength) && flag; i++)
{
flag = 0;
for (j=0; j < (numLength -1); j++)
{
if (num[j+1].startTime < num[j].startTime)
{
temp = num[j]; // swap elements
num[j] = num[j+1];
num[j+1] = temp;
flag = 1; // indicates that a swap occurred.
}
}
}
}
int main()
{
string temp, task;
int NDISKS = 1, DISKTIME = 10, val, elapsed; //number of disks is 1
int num;
Command running = Command(0," ");
Command lastRun = Command(0," ");
vector <Process> Allprocesses;
Process counter = 0;
CPU unit = CPU(0);
elapsed = 0;
//temp reads in the string that we dont need.
//Reading in constant variables
while (!cin.eof())//while loop to read all txt file into queue
{
if (temp == "START") //if we come across start in the .txt file
{
cin >> val; //read in the value after "Start"
counter = Process(val);
temp = "";
while (temp != "START"&&!cin.eof())
{
cin >> temp; //Read the input from the .txt file
if (temp != "END") //While it does not read in a "END"
{
if(temp!="START"&&!cin.eof())
{
cin >> val; //read in the value of the command
running = Command(val,temp);
counter.addCommand(running); //add command to command queu
}
}
else
{
//if Display is read
running = Command(val,temp);
counter.addCommand(running);
}
}
Allprocesses.push_back(counter);
}
}
while(!Allprocesses.empty())
{
if((unit.endtime==0) && (Allprocesses.front().startTime<elapsed)) //checks to see if cpu is busy
{
Command temp = Command(0," ");
temp = Allprocesses.front().commandString.front();//stores the next command to be run
Allprocesses.front().commandString.pop();//pops the command from queue
if(temp.input=="CPU")
{
Allprocesses.front().startTime = Allprocesses.front().startTime + temp.k;
unit.endtime=temp.k;
lastRun = Command(temp.k,"CPU");
}
if(temp.input=="TTY") //tty represent the "Display"
{
elapsed = elapsed + temp.k;
lastRun = Command(temp.k,"TTY");
}
if(temp.input=="END"){
cout << "==========Utilization==========" << endl;
cout << "Total time elapsed: " << elapsed << endl;
if(unit.endtime>0)
cout << "CPU Utilization: 100%" << endl;
else
cout << "CPU Utilization: 0%" << endl;
cout << "============================" << endl;
cout << "Processes in queue: " << endl;
for (int x = 0; x < Allprocesses.size(); x++) //a for loop to go throught the "queue" reading the values in
{
cout << "Process " << x+1 << ": " << endl;
Allprocesses[x].output(); //array of the proccesses
cout << endl;
}
lastRun = Command(0,"END");
}
if(Allprocesses.front().commandString.empty())
Allprocesses.erase(Allprocesses.begin());
BubbleSort(Allprocesses); //Sorts to find the next process to be run
}
else if(!unit.endtime==0){
unit.isbusy(unit.endtime);
}
if(Allprocesses.front().commandString.front().input=="END"){
cout << "==========Time Elapsed==========" << endl;
cout << "Total time elapsed: " << elapsed << endl;
cout << "Last command ran: " << lastRun.input << " " << lastRun.k << endl;
if(unit.endtime>0)
cout << "CPU Utilization: 100%" << endl;
else
cout << "CPU Utilization: 0%" << endl;
cout << "============================" << endl;
cout << "Processes in queue: " << endl;
for (int x = 0; x < Allprocesses.size(); x++) //a for loop to go throught the "queue" readint he values in
{
cout << "Process " << x+1 << ": " << endl;
Allprocesses[x].output(); //array of the proccesses
cout << endl;
}
lastRun = Command(0,"END");
Allprocesses.front().commandString.pop();
}
elapsed++;
}
return 0;
}
Explanation / Answer
In function 'int main()': Line 193: warning: comparison between signed and unsigned integer expressions a for loop to go throught the "queue" reading the values in Line 224: warning: comparison between signed and unsigned integer expressions a for loop to go throught the "queue" readint he values in
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.