From 805d1bb94956cf1edc89240d05cea0a7c93195b3 Mon Sep 17 00:00:00 2001 From: Jai Sharma Sharma <220120009@iitdh.ac.in> Date: Tue, 17 Sep 2024 17:37:01 +0530 Subject: [PATCH] Update lab3/scheduler.cpp --- lab3/scheduler.cpp | 216 +++++++++++++++++++++++++++++++-------------- 1 file changed, 149 insertions(+), 67 deletions(-) diff --git a/lab3/scheduler.cpp b/lab3/scheduler.cpp index 6b7fc76..37f874d 100644 --- a/lab3/scheduler.cpp +++ b/lab3/scheduler.cpp @@ -1,67 +1,149 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace std; - -void fifo() { - cout << "fifo" << endl; -} - -struct process_detail { - vector burst_times; -}; - -int main(int argc, char **argv) { - - if(argc != 3) - { - cout <<"usage: ./scheduler.out \nprovided arguments:\n"; - for(int i = 0; i < argc; i++) - cout << argv[i] << "\n"; - return -1; - } - - char *file_to_search_in = argv[1]; - char *scheduler_algorithm = argv[2]; - - vector processes_detail; - ifstream file(file_to_search_in, ios::binary); - string buffer; - - - while(getline(file, buffer)) { - if(buffer[0] == '<'){ - continue; - } - istringstream iss(buffer); - string word; - struct process_detail pd; - memset(&pd,0,sizeof(struct process_detail)); - - while(iss>>word){ - pd.burst_times.push_back(stoi(word)); - cout << stoi(word) << endl; - } - processes_detail.push_back(pd); - } - - map temp; - temp["fifo"] = 1; - string temp1 = scheduler_algorithm; - - - switch(temp[temp1]){ - case 1: - fifo(); - break; - default: - cout << "enter fifo" << endl; - } - return 0; -} \ No newline at end of file +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +struct process_detail { + //cpu_burst_times[0] is arrival time + int pid; + vector cpu_burst_times; + vector io_burst_times; + int in_cpu; + int ptr = 0; +}; + +struct clock{ + int push_signal; //boolean + int timer; + +}; + +//// operator overloading +//struct CompareHeight { +// bool operator()(struct process_detail p1, struct process_detail p2) +// { +// // return "true" if "p1" is ordered +// // before "p2", for example: +// return p1.height < p2.height; +// } +//}; + + +vector processes; +vector ready_queue; +queue ready_queue_fifo; +vector waiting; +struct process_detail* CPU = NULL; +int clock = 0; + +void fifo() { + + //clock initialized to 0 + struct clock time; + memset(&time, 0, sizeof(struct clock)); + int process_count = processes.size(); + + //ready queue initialized as process 1 will arrive at time 0 + ready_queue_fifo.push(processes[0]); + processes[0].i++; + + while(true){ + + //managing arrival times + for(int i = 0; i < process_count; ++i) { + //if process not in cpu + if(proccesses[i].in_cpu != 1) { + if(time.timer == processes[i].cpu_birst_times[0]) { + ready_queue_fifo.push(processes[ptr]); + processes[i].i++; + } + } + } + + //THE FIFO RULE + if(CPU == NULL) { + CPU = ready_queue_fifo.front(); + CPU->in_cpu = 1; + ready_queue_fifo.pop(); + } + else{ + //check cpu_burst complete + for(int i = 0; i < process_count; ++i) { + if(proccesses[i].in_cpu == 1) { + if(timer.push_signal + CPU->cpu_birst_times[1] == time.timer){ + waiting.push_back(CPU); + } + } + } + } + time.timer++; + + } + + cout << "fifo" << endl; + return; +} + +int main(int argc, char **argv) { + + if(argc != 3) + { + cout <<"usage: ./scheduler.out \nprovided arguments:\n"; + for(int i = 0; i < argc; i++) + cout << argv[i] << "\n"; + return -1; + } + + char *file_to_search_in = argv[1]; + char *scheduler_algorithm = argv[2]; + + ifstream file(file_to_search_in, ios::binary); + string buffer; + int pid = 1; + + while(getline(file, buffer)) { + if(buffer[0] == '<'){ + continue; + } + istringstream iss(buffer); + string word; + struct process_detail pd; + memset(&pd,0,sizeof(struct process_detail)); + + while(iss>>word){ + if(i == 0){ + pd.cpu_burst_times.push_back(stoi(word)); + } + else if(i % 2 == 0){ + pd.io_burst_times.push_back(stoi(word)); + } + else if(i % 2 == 1){ + pd.cpu_burst_times.push_back(stoi(word)); + } + i++; +// cout << stoi(word) << endl; + } + pd.pid = pid; + processes.push_back(pd); + } + + map temp; + temp["fifo"] = 1; + string temp1 = scheduler_algorithm; + + + switch(temp[temp1]){ + case 1: + fifo(); + break; + default: + cout << "enter fifo" << endl; + } + return 0; +}