Question: C++ code! Please help fix my code. My code is fully functional but I made the mistake of making my processes numbered 0-7, when they
C++ code! Please help fix my code. My code is fully functional but I made the mistake of making my processes numbered 0-7, when they needed to be numbered 1-8, and I'm not sure how to change the numbers without breaking it. The output should be reading Processes as P1, P2, P3, P4, P5, P6, P7 and P8 instead of P0, P1, P2, P3, P4, P5, P6, P7
#include
using namespace std;
void fcfs(vector
void printCompleteProcesses(vector
int numProcesses = 8; vector
for (int i = 0; i < processes.size(); i++) { readyQueueFCFS.push(i); }
cout << " " << "----FCFS----" << " "; fcfs(processes, readyQueueFCFS, processBurstIndex); }
void fcfs(vector
vector
for (int i = 0; i < processes.size(); i++) { ioreturn.push_back(make_pair(INT_MAX, i)); } int time = 0; int idle_Cpu_Time = 0; while (!readyQueue.empty()) {
int process_index = readyQueue.front(); readyQueue.pop();
//waiting time in ready queue waitingTime[process_index] += time - arrivalTime[process_index];
//takes value of first waiting time and then resets flag so the if condition never executes again if (responseTime[process_index] == -1) { responseTime[process_index] = time - arrivalTime[process_index]; }
printContextSwitchData(processes, process_index, readyQueue, processBurstIndex, time);
time += processes[process_index][processBurstIndex[process_index]]; // process getting cpu burst time
if (processBurstIndex[process_index] <= processes[process_index].size() - 3) { for (int i = 0; i < processes.size(); i++) { if (ioreturn[i].second == process_index) { ioreturn[i].first = processes[process_index][processBurstIndex[process_index] + 1] + time; break; } } } else { complete[process_index] = 1; } printCompleteProcesses(complete); processBurstIndex[process_index] += 2; sort(ioreturn.begin(), ioreturn.end());
if (readyQueue.empty()) { while (ioreturn[0].first != INT_MAX && ioreturn[0].first > time) { time++; idle_Cpu_Time++; } }
for (int i = 0; i < processes.size(); i++) { if (ioreturn[i].first <= time) { readyQueue.push(ioreturn[i].second); //pushing process id into readyQueue arrivalTime[ioreturn[i].second] = ioreturn[i].first; ioreturn[i].first = INT_MAX; } } } int burst_time = time - idle_Cpu_Time;
for (int i = 0; i < processes.size(); i++) { int total_burst_time = 0; for (int j = 0; j < processes[i].size(); j++) { total_burst_time += processes[i][j]; } tat[i] += total_burst_time + waitingTime[i]; }
//printing results cout << "Total time required to complete all the 8 processes: " << time << " "; cout << "CPU Utilization: " << (float)burst_time / time * 100 << " " << " "; cout << "******************************************************** " << " "; cout << "******************************************************** " << " ";
cout << "WAITING TIME" << " "; int twt = 0; for (int i = 0; i < processes.size(); i++) { cout << "Process: " << i << ", Waiting Time: " << waitingTime[i] << " "; twt += waitingTime[i]; } cout << "Average Waiting Time: " << (float)twt / processes.size() << " " << " ";
cout << "******************************************************** " << " "; cout << "******************************************************** " << " "; cout << "TURNAROUND TIME" << " "; int ttat = 0; for (int i = 0; i < processes.size(); i++) { cout << "Process: " << i << ", Turnaround Time: " << tat[i] << " "; ttat += tat[i]; } cout << "Average Turnaround Time: " << (float)ttat / processes.size() << " " << " ";
cout << "******************************************************** " << " "; cout << "******************************************************** " << " "; cout << "RESPONSE TIME" << " "; int trt = 0; for (int i = 0; i < processes.size(); i++) { cout << "Process: " << i << ", Response Time: " << responseTime[i] << " "; trt += responseTime[i]; } cout << "Average Response Time: " << (float)trt / processes.size() << " "; }
//function to print the queue void printQueue(queue
void printCompleteProcesses(vector
void printContextSwitchData(vector
cout << setfill('-') << setw(40) << "-" << " "; cout << "Current Execution Time: " << time << " " << " "; cout << "Next Process on the CPU: P" << process_index << " "; cout << setfill('-') << setw(40) << "-" << " " << " "; cout << "Ready queue:" << " "; printQueue(readyQueue, processes, processBurstIndex); cout << setfill('-') << setw(40) << "-" << " "; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
