I have written this code to simulate a Load Balancer now i want to deploy this in XV6…
- User
I have written this code to simulate a Load Balancer now i want to deploy this in XV6 Here, are the assumption Each core have there own queue When a process come it is scheduled to any core (In my code to calculate Load i have used different approach but for XV6 you can think it as ratio time which core is idle and in which it execute) There is load balancer which check for load on each core and remove a process from higher load core queue and move it to lower load core queue. And for each core it schedule process in round robin manner from its own queue
#include #include #include #include #include #include #include #include <condition_variable> #include #include using namespace std;
// Constant for load type. Can also create enum const int LOW_LOAD = 1; const int MEDIUM_LOAD = 2; const int HIGH_LOAD = 3;
const int TIME_QUANTUM = 15; // in ms
struct Process { int id; int arrival_time; int total_execution_time; int remaining_time; int load; int priority; Process(int pid, int arrival, int execution_time, int lo, int pri = 0){ id = pid; arrival_time = arrival; total_execution_time = execution_time; load = lo; priority = pri; remaining_time = execution_time; }
Process(){}};
// Comparator for priority queue (higher priority first). Used when we implement "priority_queue". struct ComparePriority { bool operator()(const Process& a, const Process& b) { return a.priority < b.priority; } };
class Core { public: // When we will be using this "mtx" to lock the std::lock_guardstd::mutex lock(mtx); it will lock all the data of this class // (so it is better to make this private. So, no random access is given by mistake). int id; queue process_queue; mutex mtx; int current_load; // See getCoreLoad() thread core_thread; // I will be using this to simulate a process running on Core. bool stop;
Core(int core_id) : id(core_id), current_load(0), stop(false){} // Function executed by the core thread void run(){ while(!stop) { Process current_process; bool has_process = false; // Fetch process from its own queue // Writing in Separate block so the "lock" will finish as soon as i leave this block { lock_guard<mutex> lock(mtx); if(!process_queue.empty()) { current_process = process_queue.front(); process_queue.pop(); current_load -= current_process.load; has_process = true; } } if(has_process) { // Simulate execution for TIME_QUANTUM or remaining_time, whichever is smaller int exec_time = std::min(TIME_QUANTUM, current_process.remaining_time); std::cout << "Core " << id << " executing Process P" << current_process.id << " for " << exec_time << "ms\n"; std::this_thread::sleep_for(std::chrono::milliseconds(exec_time)); current_process.remaining_time -= exec_time; if(current_process.remaining_time > 0) { { // Re-add the process to the ready queue lock_guard<mutex> lock(mtx); process_queue.push(current_process); current_load += current_process.load; } } else { cout << "Process P" << current_process.id << " completed on Core " << id << "\n"; } } else { // No process to execute, wait for a while std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } } // Start the core thread void start(); // Starting from Scheduler class itself // Instead of this we can also update core_load when we are adding a process and when a process execution complete int getCoreLoad(){ // So, how do we calculate a Load on a core. Note, that in Process structure we have a field name Load. // So, to get a core load what we do is sum all the Process load present in queue of this particular core. // This is somewhat the Research Paper Do (https://ieeexplore.ieee.org/document/9626411) return current_load; } // Stop the core thread void stop_core() { stop = true; // Notify in case the core is waiting { lock_guard<mutex> lock(mtx); } } - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.