c++ - Implementation of scheduling algorithm -
i have assignment need develop simulation of process scheduler. got of setup stuck @ figuring out logic schedule processes. right have struct created holds processes pid, cycle count , memory requirement. , have used vector hold 100 processes can sort them if want , remove processes schedule them.
for 5 processors created int array guessing processors should have cycle count , memory limits? in case struct maybe?
and how assign processes these 5 processors? can me figure out? need implement simple fcfs algorithm.
void init_process_list(vector<process> &p_list) { generator generate; // random number generator class process p; for(int = 0; < process_count; i++) { p.process_id = i; p.cpu_cycles = generate.rand_num_between(cycle_lbound, cycle_ubound); p.mem_footprint = generate.rand_num_between(mem_lbound, mem_ubound); p_list.push_back(p); } } // initialize processor array void init_processor_list(int *processor_list) { for(int = 0; < processor_count; i++) processor_list[i] = 0; } void schedule_processes(vector<process> &p_list, int *processor) { } int main() { vector<process> process_list; int cpu[processor_count]; init_process_list(process_list); init_processor_list(cpu); schedule_processes(process_list, cpu); }
check if understand below.
#include <iostream> #include <vector> #include<thread> #include <future> int global_processorid =0; int global_processid =0; struct process { int cycles; int memory; int pid; process() { pid = global_processid++; cycles = rand()%1000; memory = rand()%100; } }; class processor { public: processor():processing(false) { this->processorid = global_processorid++; } bool processsing() { return processing; } void process(process& process) { processing= true; std::async(std::launch::async,[&] { std::cout<<"processor id: "<<this->processorid<<" ,started processing cycles"<<process.cycles<<" ,process id :"<<process.pid<<std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(process.cycles*10)); std::cout<<"done processing: "<<process.pid<<std::endl; processing=false; }); } int processorid; private: bool processing; }; class scheduler { public: void addprcessor(processor& aprocessor) { processor.push_back(&aprocessor); } void prcess(process& process) { getnextavilableprocessor()->process(process); } processor* getnextavilableprocessor() { for(auto& : processor) { if(!a->processsing()) return a; } std::cout<<"all processors busy, waiting next avilable processor\n"; while(true) { for(auto& : processor) { if(!a->processsing()) return a; } } } private: std::vector<processor*> processor; }; int main() { int =257; int*ptr = &i; std::vector<process> processes(100); scheduler scheduler; processor one; processor two; processor three; processor four; scheduler.addprcessor(one); scheduler.addprcessor(two); scheduler.addprcessor(three); scheduler.addprcessor(four); for(auto& process : processes) { scheduler.prcess(process); }; }
Comments
Post a Comment