Update lab3/scheduler.cpp

This commit is contained in:
Jai Sharma Sharma 2024-09-17 17:37:01 +05:30
parent 485d24557f
commit 805d1bb949
1 changed files with 149 additions and 67 deletions

View File

@ -1,67 +1,149 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <cstring> #include <cstring>
#include <unistd.h> #include <unistd.h>
#include <chrono> #include <chrono>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <bits/stdc++.h> #include <bits/stdc++.h>
using namespace std; using namespace std;
void fifo() { struct process_detail {
cout << "fifo" << endl; //cpu_burst_times[0] is arrival time
} int pid;
vector<int> cpu_burst_times;
struct process_detail { vector<int> io_burst_times;
vector<int> burst_times; int in_cpu;
}; int ptr = 0;
};
int main(int argc, char **argv) {
struct clock{
if(argc != 3) int push_signal; //boolean
{ int timer;
cout <<"usage: ./scheduler.out <path-to-workload-file> <scheduler_algorithm>\nprovided arguments:\n";
for(int i = 0; i < argc; i++) };
cout << argv[i] << "\n";
return -1; //// operator overloading
} //struct CompareHeight {
// bool operator()(struct process_detail p1, struct process_detail p2)
char *file_to_search_in = argv[1]; // {
char *scheduler_algorithm = argv[2]; // // return "true" if "p1" is ordered
// // before "p2", for example:
vector<struct process_detail> processes_detail; // return p1.height < p2.height;
ifstream file(file_to_search_in, ios::binary); // }
string buffer; //};
while(getline(file, buffer)) { vector<struct process_detail> processes;
if(buffer[0] == '<'){ vector<struct process_detail> ready_queue;
continue; queue<struct process_detail> ready_queue_fifo;
} vector<struct process_detail> waiting;
istringstream iss(buffer); struct process_detail* CPU = NULL;
string word; int clock = 0;
struct process_detail pd;
memset(&pd,0,sizeof(struct process_detail)); void fifo() {
while(iss>>word){ //clock initialized to 0
pd.burst_times.push_back(stoi(word)); struct clock time;
cout << stoi(word) << endl; memset(&time, 0, sizeof(struct clock));
} int process_count = processes.size();
processes_detail.push_back(pd);
} //ready queue initialized as process 1 will arrive at time 0
ready_queue_fifo.push(processes[0]);
map<string, int> temp; processes[0].i++;
temp["fifo"] = 1;
string temp1 = scheduler_algorithm; while(true){
//managing arrival times
switch(temp[temp1]){ for(int i = 0; i < process_count; ++i) {
case 1: //if process not in cpu
fifo(); if(proccesses[i].in_cpu != 1) {
break; if(time.timer == processes[i].cpu_birst_times[0]) {
default: ready_queue_fifo.push(processes[ptr]);
cout << "enter fifo" << endl; processes[i].i++;
} }
return 0; }
} }
//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 <path-to-workload-file> <scheduler_algorithm>\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<string, int> temp;
temp["fifo"] = 1;
string temp1 = scheduler_algorithm;
switch(temp[temp1]){
case 1:
fifo();
break;
default:
cout << "enter fifo" << endl;
}
return 0;
}