OS-Labs/lab3/scheduler.cpp

218 lines
5.6 KiB
C++

#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> burst_times;
int in_cpu;
int current_burst_index;
};
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<process_detail> processes;
queue<process_detail*> ready_queue_fifo;
vector<process_detail*> waiting;
struct process_detail* CPU = NULL;
ofstream output_file("cpu_times.txt");
void fifo() {
//clock initialized to 0
struct clock time;
memset(&time, 0, sizeof(struct clock));
time.timer = 0;
time.push_signal = 0;
int process_count = processes.size();
int completed_processes = 0;
while(completed_processes < process_count){
// breaking from the infinite loop
for(int i = 0; i < process_count; ++i) {
if(processes[i].burst_times[processes[i].current_burst_index] == -1) {
completed_processes++;
}
}
//managing arrival times
for(int i = 0; i < process_count; ++i) {
//if process not in cpu
if(processes[i].in_cpu != 1) {
if(time.timer == processes[i].burst_times[0]) {
ready_queue_fifo.push(&processes[i]);
processes[i].current_burst_index++;
}
}
}
//THE FIFO RULE
if(CPU == NULL && !ready_queue_fifo.empty()) {
CPU = ready_queue_fifo.front();
CPU->in_cpu = 1;
// Record in_time when the process enters the CPU
CPU->burst_times[CPU->current_burst_index]--;
output_file << "P" << CPU->pid+1 << " " << time.timer;
ready_queue_fifo.pop();
}
// else if(CPU == NULL && ready_queue_fifo.empty()) {
// // removing form waiting list
// for(int j = 0; j < waiting.size(); ++j) {
// if(waiting[j] != NULL) {
// if(waiting[j]->burst_times[waiting[j]->current_burst_index] == 0) {
// ready_queue_fifo.push(waiting[j]);
// waiting[j]->current_burst_index++;
// waiting[j] = NULL;
// }
// else waiting[j]->burst_times[waiting[j]->current_burst_index]--; // reducing the io burst till it reaches 0
// }
// }
// time.push_signal++;
// }
else {
// removing form waiting list
for(int j = 0; j < waiting.size(); ++j) {
if(waiting[j] != NULL) {
if(waiting[j]->burst_times[waiting[j]->current_burst_index] == 0) {
ready_queue_fifo.push(waiting[j]);
waiting[j]->current_burst_index++;
waiting[j] = NULL;
}
else waiting[j]->burst_times[waiting[j]->current_burst_index]--; // reducing the io burst till it reaches 0
}
}
//check cpu_burst complete
for(int i = 0; i < process_count; ++i) {
if(processes[i].in_cpu == 1 && CPU != NULL) {
if(CPU->burst_times[processes[i].current_burst_index] == 0){
// Record out_time when the process exits the CPU
output_file << " " << time.timer << endl;
// time.push_signal = time.push_signal + CPU->burst_times[processes[i].current_burst_index] ;
CPU->in_cpu = 0;
CPU->current_burst_index++;
// CPU->burst_times[CPU->current_burst_index]--;
waiting.push_back(CPU); // process added to waiting queue
if(!ready_queue_fifo.empty()) {
CPU = ready_queue_fifo.front(); // process added to CPU
CPU->in_cpu = 1;
// CPU->burst_times[CPU->current_burst_index]--;
output_file << "P" << CPU->pid+1 << " " << time.timer; // New entry time
ready_queue_fifo.pop();
}
else {
CPU = NULL;
}
}
else CPU->burst_times[CPU->current_burst_index]--;
}
}
}
time.timer++;
// completed_processes++;
}
output_file.close();
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);
// ifstream file("process1.dat", ios::binary);
string buffer;
int pid = 0;
while(getline(file, buffer)) {
if(buffer[0] == '<'){
continue;
}
istringstream iss(buffer);
string word;
struct process_detail pd;
memset(&pd,0,sizeof(struct process_detail));
pd.pid = pid++;
pd.current_burst_index = 0;
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.burst_times.push_back(stoi(word));
// i++;
// cout << stoi(word) << endl;
}
processes.push_back(pd);
}
map<string, int> temp;
temp["fifo"] = 1;
string temp1 = scheduler_algorithm;
// string temp1 = "fifo";
switch(temp[temp1]){
case 1:
fifo();
break;
default:
cout << "enter fifo" << endl;
}
// cout << processes[0].in_cpu << endl;
// cout << processes[0].current_burst_index << endl;
// cout << processes[1].in_cpu << endl;
// cout << processes[1].current_burst_index << endl;
// cout << ready_queue_fifo.front()->pid << endl;
// ready_queue_fifo.pop();
// cout << ready_queue_fifo.front()->pid << endl;
return 0;
}