2024-09-17 17:37:01 +05:30
|
|
|
#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;
|
2024-09-17 17:51:50 +05:30
|
|
|
vector<int> burst_times;
|
|
|
|
// vector<int> io_burst_times;
|
2024-09-17 17:37:01 +05:30
|
|
|
int in_cpu;
|
|
|
|
int ptr = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct clock{
|
|
|
|
int push_signal; //boolean
|
|
|
|
int timer;
|
2024-09-17 17:51:50 +05:30
|
|
|
|
2024-09-17 17:37:01 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
//// 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() {
|
2024-09-17 17:51:50 +05:30
|
|
|
|
2024-09-17 17:37:01 +05:30
|
|
|
//clock initialized to 0
|
|
|
|
struct clock time;
|
|
|
|
memset(&time, 0, sizeof(struct clock));
|
|
|
|
int process_count = processes.size();
|
2024-09-17 17:51:50 +05:30
|
|
|
|
2024-09-17 17:37:01 +05:30
|
|
|
//ready queue initialized as process 1 will arrive at time 0
|
|
|
|
ready_queue_fifo.push(processes[0]);
|
|
|
|
processes[0].i++;
|
2024-09-17 17:51:50 +05:30
|
|
|
|
2024-09-17 17:37:01 +05:30
|
|
|
while(true){
|
2024-09-17 17:51:50 +05:30
|
|
|
|
2024-09-17 17:37:01 +05:30
|
|
|
//managing arrival times
|
|
|
|
for(int i = 0; i < process_count; ++i) {
|
|
|
|
//if process not in cpu
|
|
|
|
if(proccesses[i].in_cpu != 1) {
|
2024-09-17 17:51:50 +05:30
|
|
|
if(time.timer == processes[i].burst_times[0]) {
|
2024-09-17 17:37:01 +05:30
|
|
|
ready_queue_fifo.push(processes[ptr]);
|
|
|
|
processes[i].i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-17 17:51:50 +05:30
|
|
|
|
2024-09-17 17:37:01 +05:30
|
|
|
//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) {
|
2024-09-17 17:51:50 +05:30
|
|
|
if(timer.push_signal + CPU->burst_times[ptr] == time.timer){
|
|
|
|
waiting.push_back(CPU); // process added to waiting queue
|
|
|
|
CPU->in_cpu = 0;
|
|
|
|
CPU = ready_queue_fifo.front(); // process added to CPU
|
|
|
|
CPU->in_cpu = 1;
|
|
|
|
ready_queue_fifo.pop();
|
|
|
|
timer.push_signal = timer.push_signal + CPU->burst_times[ptr] ;
|
2024-09-17 17:37:01 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-17 17:51:50 +05:30
|
|
|
|
|
|
|
// removing form wait
|
|
|
|
|
|
|
|
|
2024-09-17 17:37:01 +05:30
|
|
|
}
|
|
|
|
time.timer++;
|
2024-09-17 17:51:50 +05:30
|
|
|
|
2024-09-17 17:37:01 +05:30
|
|
|
}
|
2024-09-17 17:51:50 +05:30
|
|
|
|
2024-09-17 17:37:01 +05:30
|
|
|
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){
|
2024-09-17 17:51:50 +05:30
|
|
|
// 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.burst_times.push_back(stoi(word));
|
|
|
|
// i++;
|
2024-09-17 17:37:01 +05:30
|
|
|
// 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;
|
|
|
|
}
|