Can you fix the output of the Gantt chart in the code and the output of the gantt chart should be the same in the image below? Badly need to fix it :( Thank you! Code: #include #include #include #include struct Process { int processId; int burstTime; int priority; }; void print_gantt_chart(const std::vector>& gantt_chart) { std::cout << "Gantt Chart:" << std::endl; std::cout << "----------------------------------------------------------------------------" << std::endl; std::cout << "| "; for (const auto& process : gantt_chart) { std::cout << "P" << process.first << " | "; } std::cout << std::endl; std::cout << "----------------------------------------------------------------------------" << std::endl; std::cout << "0 "; int currentTime = 0; for (const auto& process : gantt_chart) { currentTime += process.second; if (std::to_string(currentTime).length() == 1) std::cout << " " << currentTime << " "; else std::cout << " " << currentTime << " "; } std::cout << std::endl; } void multiLevelQueueScheduling(const std::vector& processes, int quantumTime) { std::queue fcfs; std::queue rr; std::queue priority; // Splitting the processes into different queues based on priority for (const auto& process : processes) { if (process.priority == 1) fcfs.push(process); else if (process.priority == 2) rr.push(process); else if (process.priority == 3 || process.priority == 4) priority.push(process); } std::vector> gantt_chart; while (!fcfs.empty() || !rr.empty() || !priority.empty()) { // FCFS Scheduling if (!fcfs.empty()) { Process process = fcfs.front(); fcfs.pop(); int executionTime = std::min(quantumTime, process.burstTime); process.burstTime -= executionTime; gantt_chart.emplace_back(process.processId, executionTime); if (process.burstTime > 0) fcfs.push(process); } // RR Scheduling if (!rr.empty()) { Process process = rr.front();
Can you fix the output of the Gantt chart in the code and the output of the gantt chart should be the same in the image below? Badly need to fix it :( Thank you!
Code:
#include <iostream>
#include <queue>
#include <string>
#include <
struct Process {
int processId;
int burstTime;
int priority;
};
void print_gantt_chart(const std::vector<std::pair<int, int>>& gantt_chart) {
std::cout << "Gantt Chart:" << std::endl;
std::cout << "----------------------------------------------------------------------------" << std::endl;
std::cout << "| ";
for (const auto& process : gantt_chart) {
std::cout << "P" << process.first << " | ";
}
std::cout << std::endl;
std::cout << "----------------------------------------------------------------------------" << std::endl;
std::cout << "0 ";
int currentTime = 0;
for (const auto& process : gantt_chart) {
currentTime += process.second;
if (std::to_string(currentTime).length() == 1)
std::cout << " " << currentTime << " ";
else
std::cout << " " << currentTime << " ";
}
std::cout << std::endl;
}
void multiLevelQueueScheduling(const std::vector<Process>& processes, int quantumTime) {
std::queue<Process> fcfs;
std::queue<Process> rr;
std::queue<Process> priority;
// Splitting the processes into different queues based on priority
for (const auto& process : processes) {
if (process.priority == 1)
fcfs.push(process);
else if (process.priority == 2)
rr.push(process);
else if (process.priority == 3 || process.priority == 4)
priority.push(process);
}
std::vector<std::pair<int, int>> gantt_chart;
while (!fcfs.empty() || !rr.empty() || !priority.empty()) {
// FCFS Scheduling
if (!fcfs.empty()) {
Process process = fcfs.front();
fcfs.pop();
int executionTime = std::min(quantumTime, process.burstTime);
process.burstTime -= executionTime;
gantt_chart.emplace_back(process.processId, executionTime);
if (process.burstTime > 0)
fcfs.push(process);
}
// RR Scheduling
if (!rr.empty()) {
Process process = rr.front();
rr.pop();
int executionTime = std::min(quantumTime, process.burstTime);
process.burstTime -= executionTime;
gantt_chart.emplace_back(process.processId, executionTime);
if (process.burstTime > 0)
rr.push(process);
}
// Priority Scheduling
if (!priority.empty()) {
Process process = priority.front();
priority.pop();
int executionTime = std::min(quantumTime, process.burstTime);
process.burstTime -= executionTime;
gantt_chart.emplace_back(process.processId, executionTime);
if (process.burstTime > 0)
priority.push(process);
}
}
print_gantt_chart(gantt_chart);
std::vector<int> completionTime(processes.size());
std::vector<int> turnaroundTime(processes.size());
std::vector<int> waitingTime(processes.size());
int currentEndTime = 0;
for (const auto& process : gantt_chart) {
int processIndex = process.first - 1;
currentEndTime += process.second;
completionTime[processIndex] = currentEndTime;
turnaroundTime[processIndex] = completionTime[processIndex];
waitingTime[processIndex] = turnaroundTime[processIndex] - processes[processIndex].burstTime;
}
std::cout << "\nProcess\t\tBurst Time\t\tPriority\t\tCompletion Time\t\tTurnaround Time\t\tWaiting Time" << std::endl;
for (const auto& process : processes) {
int processIndex = process.processId - 1;
std::cout << "P" << process.processId << "\t\t" << process.burstTime << "\t\t\t" << process.priority << "\t\t\t"
<< completionTime[processIndex] << "\t\t\t" << turnaroundTime[processIndex] << "\t\t\t" << waitingTime[processIndex] << std::endl;
}
float avgWaitingTime = 0;
float avgTurnaroundTime = 0;
for (const auto& process : processes) {
avgWaitingTime += waitingTime[process.processId - 1];
avgTurnaroundTime += turnaroundTime[process.processId - 1];
}
avgWaitingTime /= processes.size();
avgTurnaroundTime /= processes.size();
std::cout << std::endl;
std::cout << "Average Waiting Time: " << avgWaitingTime << std::endl;
std::cout << "Average Turnaround Time: " << avgTurnaroundTime << std::endl;
}
int main() {
std::vector<Process> processes = {
{1, 8, 4},
{2, 6, 1},
{3, 1, 2},
{4, 9, 2},
{5, 3, 3}
};
int quantumTime = 2;
multiLevelQueueScheduling(processes, quantumTime);
return 0;
}
Step by step
Solved in 3 steps
Can you align the gantt chart properly like the image below? Thank you!
#include <iostream>
#include <
#include <algorithm>
#include <limits>
struct Process {
int processId;
int burstTime;
int arrivalTime;
};
void print_gantt_chart(const std::vector<int>& processIds, const std::vector<int>& completionTimes) {
std::cout << "Gantt Chart:" << std::endl;
std::cout << "--------------------------------------------------" << std::endl;
std::cout << "| ";
for (int i = 0; i < processIds.size(); i++) {
std::cout << "P" << processIds[i] << " | ";
}
std::cout << std::endl;
std::cout << "--------------------------------------------------" << std::endl;
std::cout << "0 ";
for (int i = 0; i < completionTimes.size(); i++) {
std::cout << " " << completionTimes[i] << " ";
}
std::cout << std::endl;
}
void calculate_completion_times(const std::vector<Process>& processes, std::vector<int>& completionTimes) {
std::vector<int> remainingBurstTimes(processes.size());
for (int i = 0; i < processes.size(); i++) {
remainingBurstTimes[i] = processes[i].burstTime;
}
int currentTime = 0;
int completedProcesses = 0;
while (completedProcesses < processes.size()) {
int shortestJob = -1;
int shortestBurstTime = std::numeric_limits<int>::max();
for (int i = 0; i < processes.size(); i++) {
if (remainingBurstTimes[i] > 0 && processes[i].arrivalTime <= currentTime && remainingBurstTimes[i] < shortestBurstTime) {
shortestJob = i;
shortestBurstTime = remainingBurstTimes[i];
}
}
if (shortestJob == -1) {
currentTime++;
continue;
}
completionTimes[shortestJob] = currentTime + remainingBurstTimes[shortestJob];
remainingBurstTimes[shortestJob] = 0;
completedProcesses++;
currentTime = completionTimes[shortestJob];
}
}
void calculate_turnaround_times(const std::vector<Process>& processes, const std::vector<int>& completionTimes, std::vector<int>& turnaroundTimes) {
for (int i = 0; i < processes.size(); i++) {
turnaroundTimes[i] = completionTimes[i] - processes[i].arrivalTime;
}
}
void calculate_waiting_times(const std::vector<Process>& processes, const std::vector<int>& turnaroundTimes, std::vector<int>& waitingTimes) {
for (int i = 0; i < processes.size(); i++) {
waitingTimes[i] = turnaroundTimes[i] - processes[i].burstTime;
}
}
void print_process_table(const std::vector<Process>& processes, const std::vector<int>& completionTimes, const std::vector<int>& turnaroundTimes, const std::vector<int>& waitingTimes) {
std::cout << "\nProcess\tBurst Time\tArrival Time\tTurnaround Time\tWaiting Time" << std::endl;
for (int i = 0; i < processes.size(); i++) {
std::cout << "P" << processes[i].processId << "\t" << processes[i].burstTime << "\t\t" << processes[i].arrivalTime << "\t\t"
<< turnaroundTimes[i] << "\t\t\t" << waitingTimes[i] << std::endl;
}
}
void print_average_times(const std::vector<int>& turnaroundTimes, const std::vector<int>& waitingTimes) {
float avgTurnaroundTime = 0;
float avgWaitingTime = 0;
for (int i = 0; i < turnaroundTimes.size(); i++) {
avgTurnaroundTime += turnaroundTimes[i];
avgWaitingTime += waitingTimes[i];
}
avgTurnaroundTime /= turnaroundTimes.size();
avgWaitingTime /= waitingTimes.size();
std::cout << std::endl;
std::cout << "Average Turnaround Time: " << avgTurnaroundTime << std::endl;
std::cout << "Average Waiting Time: " << avgWaitingTime << std::endl;
}
int main() {
std::vector<Process> processes = {
{1, 8, 0},
{2, 6, 2},
{3, 1, 3},
{4, 9, 4},
{5, 3, 5}
};
std::vector<int> completionTimes(processes.size());
std::vector<int> turnaroundTimes(processes.size());
std::vector<int> waitingTimes(processes.size());
calculate_completion_times(processes, completionTimes);
calculate_turnaround_times(processes, completionTimes, turnaroundTimes);
calculate_waiting_times(processes, turnaroundTimes, waitingTimes);
print_gantt_chart(std::vector<int>{2, 3, 4, 4, 4, 4, 4, 5, 1}, std::vector<int>{6, 7, 9, 11, 13, 15, 16, 19, 27});
print_process_table(processes, completionTimes, turnaroundTimes, waitingTimes);
print_average_times(turnaroundTimes, waitingTimes);
return 0;
}