-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
95 lines (79 loc) · 4 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <fstream>
#include <algorithm>
#include <boost/algorithm/string.hpp>
#include "sOS-Sim.h"
#include "ProcessSchedulingAlgorithms.h"
#include "FileManager.h"
#include "PageReplacementAlgorithms.h"
std::vector<std::tuple<int, double, int, double, double, std::vector<Page>>> process;
std::vector<Page> getPages(std::string _pages);
bool step_by_step = false, debug_mode = true;
/**
* Reads the file, takes the tuples from the processes and places them in a vector;
* Each process is a tuple with PID, Submission time, Priority, Execution time and Block time, respectively.
* @param filename
*/
void filetoVectorofTuples(std::string filename) {
using namespace FileManager;
std::ifstream file(filename.c_str(), std::ifstream::in);
double submissionTime, executionTime, blockTime;
std::string _pages;
int PID, priority;
std::istringstream pagesFile(readFile(filename+"_pages.txt").str());
std::stringstream processFile = readFile(filename+"_process.txt");
for (auto i = 0; i < FileManager::numberofLines; i++) {
processFile >> PID >> submissionTime >> priority >> executionTime >> blockTime;
executionTime -= blockTime;
if (debug_mode)
std::cout << PID << " " << submissionTime << " " << priority << " " << executionTime << " " << blockTime << "\n";
std::getline(pagesFile, _pages);
process.push_back(std::tuple<int, double, int, double, double, std::vector<Page>>(PID, submissionTime, priority,
executionTime, blockTime,getPages(_pages)));
}
}
std::vector<Page> getPages(std::string _pages) {
std::vector<Page> pages;
std::stringstream aux_pages(_pages);
int pid, time, value;
char colon;
aux_pages >> pid;
while (aux_pages >> time) {
aux_pages >> colon >> value;
pages.push_back(Page(time, 0, value, pid));
}
// sort pages by time
std::sort(pages.begin(), pages.end());
// Update life time in all pages
for (int i = 0; i < pages.size(); i++)
pages[i].setLifeTime(pages[i+1].getFirstUse() - pages[i].getFirstUse());
return pages;
}
int main() {
// Short Time Scheduling Algorithms
bool (*RR)(std::vector <Process>*, std::vector<Process>*, int*, double) = ProcessSchedulingAlgorithms::RR;
bool (*HRRN)(std::vector <Process>*, std::vector<Process>*, int*, double) = ProcessSchedulingAlgorithms::HRRN;
bool (*PRIORITY)(std::vector <Process>*, std::vector<Process>*, int*, double) = ProcessSchedulingAlgorithms::PRIORITY;
bool (*LOTTERY)(std::vector <Process>*, std::vector<Process>*, int*, double) = ProcessSchedulingAlgorithms::LOTTERY;
bool (*SD) (std::vector <Process>*, std::vector<Process>*, int*, double) = ProcessSchedulingAlgorithms::SD;
bool (*FEEDBACK)(std::vector <Process>*, std::vector<Process>*, int*, double) = ProcessSchedulingAlgorithms::FEEDBACK;
// Page Replacement Algorithms
bool (*FIFO)(std::vector<Page>*, std::vector<Page>*, Page, double) = PageReplacementAlgorithms::FIFO;
bool (*CLOCK)(std::vector<Page>*, std::vector<Page>*, Page, double) = PageReplacementAlgorithms::CLOCK;
bool (*LRU)(std::vector<Page>*, std::vector<Page>*, Page, double) = PageReplacementAlgorithms::LRU;
bool (*OPTIMAL)(std::vector<Page>*, std::vector<Page>*, Page, double) = NULL;
std::stringstream out;
Simulator sim(20, step_by_step, debug_mode);
ProcessSchedulingAlgorithms::maxProcessMultiprogramming = 20;
// get process from file
process.clear();
filetoVectorofTuples("/home/ariel/ClionProjects/sOS-Sim/Files4Test/cenario1");
out << "Cenário 1:\n" << "\tShort Time Scheduling Algorithm: Round Robbin - Quantum = 2\n";
std::cout << "Cenário 1:\n" << "\tShort Time Scheduling Algorithm: Round Robbin - Quantum = 2\n";
out << "\rPage replacement Algorithm: FIFO \n";
std::cout << "\tPage replacement Algorithm: FIFO \n";
sim.StartSimulation(RR,LRU, process);
out << sim.getResults() << "\n\n";
FileManager::writeFile("/home/ariel/ClionProjects/sOS-Sim/Files4Test/simulation.out", out.str());
return 0;
}