I’m building a Multilevel Feedback Queue Scheduling(MLFQ) sorting Algorithm simulation and I’m having the results of each process print out to the terminal and they each have a loading bar and I need them to update accordingly. Right now I can get the loading bar to update correctly
This is what it looks like when it is printing:
Timestamp Process ID
=============== ] ======== ] [8] Process 9 [ [5] Process 6 [ 4 [
It is not having each process on its own separate line and its not updating correctly. Is the problem that I’m using threads? Any help would be appreciated
//.h file
#pragma once
#ifndef MLFQ_H
#define MLFQ_H
#include <vector>
struct Process {
int processID;
int arrivalTime;
int burstTime;
int priority;
Process(int id, int arrival, int burst, int prio) :
processID(id), arrivalTime(arrival), burstTime(burst), priority(prio) {}
};
class MLFQ {
private:
std::vector<Process> processes;
std::vector<Process> queue0; // Highest priority
std::vector<Process> queue1; // Medium priority
std::vector<Process> queue2; // Lowest priority
public:
MLFQ(std::vector<Process>& procs);
void runMLFQ();
};
#endif
//.cpp file
#include "MLFQ.h"
#include <iostream>
#include <algorithm>
#include <thread>
#include <mutex>
#include <chrono>
#include <iomanip>
// Mutex for ensuring thread-safe access to cout
std::mutex coutMutex;
// Named constants
const int MAX_PROCESS_ID_WIDTH = 10;
const int MAX_TIMESTAMP_LENGTH = 10;
const int LOADING_BAR_WIDTH = 20;
// Helper function to print column headers
void printColumnHeaders() {
std::lock_guard<std::mutex> lock(coutMutex);
std::cout << "33[1;34m" << std::right << std::setw(MAX_TIMESTAMP_LENGTH) << "Timestamp "
<< std::left << std::setw(MAX_PROCESS_ID_WIDTH) << "Process ID" << std::endl;
}
// Helper function to print process message
void printProcessMessage(const Process& process, int currentTime) {
std::lock_guard<std::mutex> lock(coutMutex);
std::cout << "33[1;33m" << std::right << std::setw(MAX_TIMESTAMP_LENGTH) << "[" << currentTime << "] "
<< std::right << std::setw(MAX_PROCESS_ID_WIDTH) << "Process " << std::setw(2) << process.processID << " [";
}
// Helper function to print loading bar
void printLoadingBar(int progress) {
std::lock_guard<std::mutex> lock(coutMutex);
for (int i = 0; i < LOADING_BAR_WIDTH; ++i) {
if (i < progress) {
std::cout << "=";
}
else {
std::cout << " ";
}
}
std::cout << "] ";
std::cout << std::flush;
}
// Helper function to execute process
void executeProcess(const Process& process, int& currentTime) {
// Print process ID
printProcessMessage(process, currentTime);
// Simulate processing time
for (int i = 0; i < process.burstTime; ++i) {
std::this_thread::sleep_for(std::chrono::seconds(1));
++currentTime;
// Print execution progress
int progress = LOADING_BAR_WIDTH * (i + 1) / process.burstTime;
std::cout << "r"; // Move cursor to the beginning of the line
printLoadingBar(progress);
std::cout << std::flush; // Flush the output buffer
}
std::cout << "33[K33[0m" << std::endl; // Clear the line and reset color
}
MLFQ::MLFQ(std::vector<Process>& procs) : processes(procs) {}
void MLFQ::runMLFQ() {
// Print column headers
printColumnHeaders();
// Sorting processes by arrival time
std::sort(processes.begin(), processes.end(), [](const Process& p1, const Process& p2) {
return p1.arrivalTime < p2.arrivalTime;
});
int currentTime = processes.empty() ? 0 : processes[0].arrivalTime;
// Create a thread for each process
std::vector<std::thread> threads;
for (const auto& process : processes) {
threads.emplace_back([&]() {
try {
// Wait until the arrival time
std::this_thread::sleep_for(std::chrono::seconds(process.arrivalTime - currentTime));
// Update current time
currentTime = process.arrivalTime;
// Execute process
executeProcess(process, currentTime);
}
catch (const std::exception& e) {
// Print error message in red
std::lock_guard<std::mutex> lock(coutMutex);
std::cerr << "33[1;31mError: " << e.what() << "33[0m" << std::endl;
}
});
}
// Wait for all threads to finish
for (auto& thread : threads) {
thread.join();
}
}
//main file
#include <iostream>
#include "MLFQ.h"
int main()
{
std::vector<Process> processes = {
Process(1, 0, 10, 1),
Process(2, 1, 15, 2),
Process(3, 2, 8, 0),
Process(4, 3, 12, 1),
Process(5, 4, 7, 2),
Process(6, 5, 18, 0),
Process(7, 6, 5, 1),
Process(8, 7, 20, 2),
Process(9, 8, 12, 0),
Process(10, 9, 6, 1),
Process(11, 10, 14, 2),
Process(12, 11, 9, 0),
Process(13, 12, 16, 1),
Process(14, 13, 4, 2),
Process(15, 14, 20, 0),
Process(16, 15, 8, 1),
Process(17, 16, 10, 2),
Process(18, 17, 6, 0),
Process(19, 18, 18, 1),
Process(20, 19, 5, 2),
Process(21, 20, 22, 0),
Process(22, 21, 9, 1),
Process(23, 22, 16, 2),
Process(24, 23, 7, 0),
Process(25, 24, 12, 1),
Process(26, 25, 14, 2),
Process(27, 26, 6, 0),
Process(28, 27, 20, 1),
Process(29, 28, 10, 2),
Process(30, 29, 8, 0)
};
MLFQ mlfq(processes);
mlfq.runMLFQ();
return 0;
}
1