Hello i want to integrate a combined CPU scheduling gantt chart, Here is my code without a gantt chart (this works perfectly, but still no gantt chart):
#include <iostream>
#include <fstream>
#include <vector>
#include <limits>
#include <iomanip>
using namespace std;
class Process {
public:
int process_n;
int a_time;
int b_time;
int prio;
int rem_time;
int res_time;
int com_time;
int w_time;
int t_time;
void initialize() {
rem_time = b_time;
}
};
int main() {
ifstream infile("input.txt"); // Open input file
if (!infile) {
cerr << "Error opening input file." << '\n';
return 1;
}
int proc;
infile >> proc; // Read number of processes from file
vector<Process> processes(proc);
//processes
cout << "Number of process: " << proc << '\n';
cout << '\n';
//arrival time
cout << "Arrival time:\n";
for (int i = 0; i < proc; ++i) {
infile >> processes[i].a_time;
cout << "P" << i + 1 << ": " << processes[i].a_time << '\n';
}
cout << '\n';
//burst time
cout << "Burst time:\n";
for (int i = 0; i < proc; ++i) {
infile >> processes[i].b_time;
cout << "P" << i + 1 << ": " << processes[i].b_time << '\n';
}
cout << '\n';
//priority
cout << "Priority:\n";
for (int i = 0; i < proc; ++i) {
infile >> processes[i].prio;
cout << "p" << i + 1 << ": " << processes[i].prio << '\n';
processes[i].initialize();
}
//pid
for (int i = 0; i < proc; i++){
processes[i].process_n = i + 1;
}
cout << '\n';
infile.close(); // Close the input file
int curr_time = 0;
while (true) {
int curr_high_priority_index = -1;
int curr_high_priority = numeric_limits<int>::max();
bool all_completed = true;
for (int i = 0; i < proc; ++i) {
if (processes[i].rem_time > 0) {
all_completed = false;
if (processes[i].a_time <= curr_time) {
if (processes[i].prio < curr_high_priority) {
curr_high_priority = processes[i].prio;
curr_high_priority_index = i;
}
}
}
}
if (all_completed) {
break;
}
if (processes[curr_high_priority_index].rem_time == processes[curr_high_priority_index].b_time) {
processes[curr_high_priority_index].res_time = curr_time;
}
processes[curr_high_priority_index].rem_time--;
curr_time++;
if (processes[curr_high_priority_index].rem_time == 0) {
processes[curr_high_priority_index].com_time = curr_time;
}
}
// Output results
int sum_res_time = 0;
int sum_com_time = 0;
int sum_w_time = 0;
int sum_t_time = 0;
//table
cout << "Process Table:\n";
cout << "+-----------+-----------------+--------------+" << '\n';
cout << "| Process | Turnaround Time | Waiting Time |" << '\n';
cout << "+-----------+-----------------+--------------+" << '\n';
for (int n = 0; n < proc; ++n) {
processes[n].t_time = processes[n].com_time - processes[n].a_time;
processes[n].w_time = processes[n].t_time - processes[n].b_time;
cout << "| P" << processes[n].process_n << setw(9) << "|";
cout << setw(10) << processes[n].t_time << setw(8) << "|";
cout << setw(8) << processes[n].w_time << setw(7) << "|" << '\n';
sum_res_time += processes[n].res_time;
sum_com_time += processes[n].com_time;
sum_w_time += processes[n].w_time;
sum_t_time += processes[n].t_time;
}
int ave_t_time = (sum_t_time / proc);
int ave_w_time = (sum_w_time / proc);
//total and aevrage
cout << "+-----------+-----------------+--------------+" << '\n';
cout << "| Total |" << setw(10) << sum_t_time << setw(8) << "|" << setw(8) << sum_w_time << setw(7) << "|" << '\n';
cout << "+-----------+-----------------+--------------+" << '\n';
cout << "| Average |" << setw(10) << ave_t_time << setw(8) << "|" << setw(8) << ave_w_time << setw(7) << "|" << '\n';
cout << "+-----------+-----------------+--------------+" << '\n';
return 0;
}
i got lots of incorrect outputs this is the example
<= 2(priority) background process (FCFS)
>= 1(priority) foreground process (RR Q=2)
example input:
number of process = 4
arrival time
p1: 0
p2: 2
p3: 4
p4: 5
burst time
p1: 7
p2: 4
p3: 1
p4: 4
priority
p1: 2
p2: 1
p3: 2
p4: 1
example gantt chart:
<-FCFS-> <----------RR Q=2-------------> <-----FCFS----->
|p1 |p2 |p2 |p4 |p4 |p1 |p3 |
0 2 4 6 8 10 15 16