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 <fstream>
#include <cstring>
#include <unistd.h>
#include <chrono>
#include <sstream>
#include <string>
#include <bits/stdc++.h>
using namespace std;
void fifo() {
cout << "fifo" << endl;
}
struct process_detail {
vector<int> burst_times;
};
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];
vector<struct process_detail> 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<string, int> temp;
temp["fifo"] = 1;
string temp1 = scheduler_algorithm;
switch(temp[temp1]){
case 1:
fifo();
break;
default:
cout << "enter fifo" << endl;
}
return 0;
}
#include <iostream>
#include <fstream>
#include <cstring>
#include <unistd.h>
#include <chrono>
#include <sstream>
#include <string>
#include <bits/stdc++.h>
using namespace std;
struct process_detail {
//cpu_burst_times[0] is arrival time
int pid;
vector<int> cpu_burst_times;
vector<int> 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<struct process_detail> processes;
vector<struct process_detail> ready_queue;
queue<struct process_detail> ready_queue_fifo;
vector<struct process_detail> 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 <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;
}