Skip to content

Commit 99cd177

Browse files
add paging to simulator
1 parent 6c8cdb6 commit 99cd177

9 files changed

+397
-107
lines changed

FileManager.h

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace FileManager {
1515
*/
1616
static std::stringstream readFile(std::string filepath) {
1717
try {
18+
numberofLines = 0;
1819
std::stringstream out;
1920
std::ifstream infile(filepath);
2021
for (std::string line; getline(infile, line);

Page.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef SIMPLE_OS_SIMULATOR_PAGE_H
2+
#define SIMPLE_OS_SIMULATOR_PAGE_H
3+
4+
#include <cstdint>
5+
6+
class Page {
7+
public:
8+
Page(uint32_t firstUse, uint32_t lifeTime, uint32_t value, uint32_t pid) { this->firstUse = firstUse;
9+
this->lastTimeUsed = 0; this->pid = pid; this->lifeTime = lifeTime; this->value = value; }
10+
11+
uint32_t getPID() { return this->pid; }
12+
uint32_t getFirstUse() { return this->firstUse; }
13+
uint32_t getLifeTime() { return this->lifeTime; }
14+
uint32_t getValue() { lastTimeUsed = 0; return this->value; }
15+
double getLastTimeUsed() { return this->lastTimeUsed; }
16+
17+
void setLifeTime(uint32_t lifeTime) { this->lifeTime = lifeTime; }
18+
void setUsed(double _elapsedTime) { this->lastTimeUsed = _elapsedTime; }
19+
20+
void decrementLifeTime() { this->lifeTime--; }
21+
bool operator<(Page &a) const { return firstUse < a.getFirstUse(); }
22+
private:
23+
uint32_t firstUse, lifeTime, value, pid;
24+
double lastTimeUsed;
25+
};
26+
27+
#endif //SIMPLE_OS_SIMULATOR_PAGE_H

PageReplacementAlgorithms.h

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#ifndef SIMPLE_OS_SIMULATOR_PAGEREPLACEMENTALGORITHMS_H
2+
#define SIMPLE_OS_SIMULATOR_PAGEREPLACEMENTALGORITHMS_H
3+
4+
#include <cstdint>
5+
#include <vector>
6+
#include <iostream>
7+
#include "Page.h"
8+
#include "sOS-Sim.h"
9+
10+
namespace PageReplacementAlgorithms {
11+
12+
uint32_t maxFrames = Simulator::numberOfMemoryFrames;
13+
14+
15+
static bool FIFO(std::vector<Page>* pagesInMemory, std::vector<Page>* pagesInDisk, Page page, double _elapsedTime) {
16+
try {
17+
if (!pagesInDisk->empty() && page.getLifeTime() > 0) {
18+
int i = 0;
19+
// search page on disk
20+
for (; i < (*pagesInDisk).size() && !((*pagesInDisk)[i].getValue() == page.getValue() &&
21+
(*pagesInDisk)[i].getPID() == page.getPID()); i++);
22+
if (i > (*pagesInDisk).size()) { Simulator::DebugLog("ERRO. PÁGINA NÃO ENCONTRADA NO DISCO"); exit(1);}
23+
// if memory is full, move first page in memory to disk
24+
if ((*pagesInMemory).size() >= maxFrames) {
25+
(*pagesInDisk).push_back((*pagesInMemory)[0]);
26+
(*pagesInMemory).erase((*pagesInMemory).begin());
27+
Simulator::DebugLog(_elapsedTime,
28+
"Page " + std::to_string((*pagesInMemory)[0].getValue()) + " of pid " +
29+
std::to_string((*pagesInMemory)[0].getPID()) + " in pos 0 on memory " +
30+
"was moved to disk");
31+
}
32+
// move page from disk to memory
33+
(*pagesInMemory).push_back((*pagesInDisk)[i]);
34+
(*pagesInDisk).erase((*pagesInDisk).begin() + i);
35+
Simulator::DebugLog(_elapsedTime, "Page "+std::to_string(page.getValue())+" of pid "+
36+
std::to_string(page.getPID())+" moved from disk to memory");
37+
return true;
38+
} return false;
39+
} catch (...) {
40+
std::cout << "Erro ao substituir página." << std::endl;
41+
return false;
42+
}
43+
}
44+
45+
static bool LRU(std::vector<Page>* pagesInMemory, std::vector<Page>* pagesInDisk, Page page, double _elapsedTime) {
46+
try {
47+
if (!(*pagesInDisk).empty() && page.getLifeTime() > 0) {
48+
int lru = 0, i = 0;
49+
// search page on disk
50+
for (; i < (*pagesInDisk).size() && !((*pagesInDisk)[i].getValue() == page.getValue() &&
51+
(*pagesInDisk)[i].getPID() == page.getPID()); i++);
52+
if (i > (*pagesInDisk).size()) { Simulator::DebugLog("ERRO. PÁGINA NÃO ENCONTRADA NO DISCO"); exit(1);}
53+
if (!(*pagesInMemory).empty()) {
54+
// finds the less recently used page in memory
55+
for (auto j = (*pagesInMemory).size(); j-- > 0; )
56+
if ((*pagesInMemory)[j].getLastTimeUsed() < (*pagesInMemory)[lru].getLastTimeUsed()) lru = j;
57+
// move the page memory[lru] in disk
58+
(*pagesInDisk).push_back((*pagesInMemory)[lru]);
59+
// then send this page disk[i] to memory[lru]
60+
(*pagesInMemory)[lru] = (*pagesInDisk)[i];
61+
(*pagesInDisk).erase((*pagesInDisk).begin()+i);
62+
Simulator::DebugLog(_elapsedTime, "Page "+std::to_string(page.getValue())+" of pid "+
63+
std::to_string(page.getPID())+" moved from disk to pos "+std::to_string(lru)+" on memory");
64+
} else {
65+
// then send this page disk[i] to memory
66+
(*pagesInMemory).push_back((*pagesInDisk)[i]);
67+
(*pagesInDisk).erase((*pagesInDisk).begin()+i);
68+
Simulator::DebugLog(_elapsedTime, "Page "+std::to_string(page.getValue())+" of pid "+
69+
std::to_string(page.getPID())+" moved from disk to pos 0 on memory");
70+
}
71+
return true;
72+
}
73+
return false;
74+
} catch(...) {
75+
std::cout << "Erro ao substituir página." << std::endl;
76+
return false;
77+
}
78+
}
79+
80+
static bool CLOCK(std::vector<Page>* pagesInMemory, std::vector<Page>* pagesInDisk, Page page, double _elapsedTime) {
81+
try {
82+
//TO DO
83+
return true;
84+
} catch(...) {
85+
std::cout << "Erro ao substituir página." << std::endl;
86+
return false;
87+
}
88+
}
89+
90+
static bool OTIMO(std::vector<Page>* pagesInMemory, std::vector<Page>* pagesInDisk, Page page, double _elapsedTime) {
91+
try {
92+
//TO DO
93+
return true;
94+
} catch(...) {
95+
std::cout << "Erro ao substituir página." << std::endl;
96+
return false;
97+
}
98+
}
99+
}
100+
101+
#endif //SIMPLE_OS_SIMULATOR_PAGEREPLACEMENTALGORITHMS_H

Process.cpp

+25-8
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,28 @@ Process::Process() {
44
this->PID = 0;
55
this->submissionTime = 0;
66
this->priority = 0;
7-
this->executionTime = 0;
7+
this->remainsExecutionTime = 0;
88
this->blockTime = 0;
99
this->timesExecuted = 0;
1010

1111
this->waitingTime = 0;
1212
this->responseTime = 0;
1313

14-
executionTime__ = executionTime;
14+
totalExecutationTime = remainsExecutionTime;
1515
}
1616

17-
Process::Process(std::tuple<int, double, int, double, double> _process) {
17+
Process::Process(std::tuple<int, double, int, double, double, std::vector<Page>> _process) {
1818
this->PID = _getPID(_process);
1919
this->submissionTime = _getSubmissionTime(_process);
2020
this->priority = _getPriority(_process);
21-
this->executionTime = _getExecutionTime(_process);
21+
this->remainsExecutionTime = _getExecutionTime(_process);
2222
this->blockTime = _getBlockedTime(_process);
2323
this->waitingTime = -1;
2424
this->responseTime = -1;
2525
this->timesExecuted = 0;
26+
this->pages = _getPages(_process);
2627

27-
executionTime__ = executionTime;
28+
totalExecutationTime = remainsExecutionTime;
2829
}
2930

3031
int Process::getPID() {
@@ -44,7 +45,7 @@ int Process::getTimesExecuted() {
4445
}
4546

4647
double Process::getExecutionTime() {
47-
return executionTime;
48+
return remainsExecutionTime;
4849
}
4950

5051
double Process::getBlockTime() {
@@ -63,6 +64,22 @@ double Process::getTurnaroundTime() {
6364
return turnaroundTime;
6465
}
6566

67+
Page Process::getPage() {
68+
if (pages[0].getFirstUse() > this->totalExecutationTime - remainsExecutionTime)
69+
return Page(0,0,0,0);
70+
if (pages[0].getLifeTime() > 0)
71+
return pages[0];
72+
else {
73+
pages.erase(pages.begin());
74+
if (pages[0].getLifeTime() > 0)
75+
return pages[0];
76+
}
77+
}
78+
79+
std::vector<Page> Process::getAllPages() {
80+
return pages;
81+
}
82+
6683
void Process::updateSubmissionTime(double _submissionTime) {
6784
this->submissionTime = _submissionTime;
6885
}
@@ -73,7 +90,7 @@ void Process::setResponseTime(double _elapsedTime) {
7390
}
7491

7592
void Process::setWaitingTime(double _elapsedTime) {
76-
this->waitingTime = _elapsedTime - (this->submissionTime + this->executionTime__);
93+
this->waitingTime = _elapsedTime - (this->submissionTime + this->totalExecutationTime);
7794
}
7895

7996
void Process::setTurnaroundTime(double _elapsedTime) {
@@ -86,4 +103,4 @@ void Process::incrementTimesExecuted() {
86103

87104
void Process::setPriority(int priority) {
88105
this->priority = priority;
89-
}
106+
}

Process.h

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include <iostream>
22
#include <string>
33
#include <tuple>
4+
#include <map>
5+
#include <vector>
6+
#include "Page.h"
47

58
#ifndef SIMPLE_OS_SIMULATOR_PROCESS_H
69
#define SIMPLE_OS_SIMULATOR_PROCESS_H
@@ -11,6 +14,7 @@
1114
#define _getPriority(x) std::get<2>(x)
1215
#define _getExecutionTime(x) std::get<3>(x)
1316
#define _getBlockedTime(x) std::get<4>(x)
17+
#define _getPages(x) std::get<5>(x)
1418

1519
/***
1620
* Data structure of the process
@@ -19,7 +23,7 @@
1923
class Process {
2024
public:
2125
Process();
22-
Process(std::tuple<int, double, int, double, double>);
26+
Process(std::tuple<int, double, int, double, double, std::vector<Page>>);
2327
int getPID();
2428
int getPriority();
2529
int getTimesExecuted();
@@ -29,21 +33,25 @@ class Process {
2933
double getResponseTime();
3034
double getWaitingTime();
3135
double getTurnaroundTime();
36+
Page getPage();
37+
std::vector<Page> getAllPages();
3238
void updateSubmissionTime(double _submissionTime);
3339
void setResponseTime(double _elapsedTime);
3440
void setWaitingTime(double _elapsedTime);
3541
void setTurnaroundTime(double _elapsedTime);
3642
void setPriority(int priority);
3743
void incrementTimesExecuted();
38-
void decrementExecutionTime() {if (executionTime > 0) --executionTime;};
39-
void decrementBlockTime() {if (blockTime > 0) --blockTime;};
44+
void decrementExecutionTime() { if (remainsExecutionTime > 0) --remainsExecutionTime; }
45+
void decrementBlockTime() { if (blockTime > 0) --blockTime; }
46+
void decrementPageLifeTime() { if (pages[0].getLifeTime() > 0) pages[0].decrementLifeTime(); }
4047

41-
//only for lottery algorithm (dettermine the range of tickets)
48+
//only for lottery algorithm (determine the range of tickets)
4249
uint32_t firstTicket, lastTicket;
43-
double executionTime__;
50+
double totalExecutationTime;
4451
private:
52+
std::vector<Page> pages;
4553
int PID, priority, timesExecuted;
46-
double submissionTime, executionTime, blockTime, waitingTime, responseTime, turnaroundTime;
54+
double submissionTime, remainsExecutionTime, blockTime, waitingTime, responseTime, turnaroundTime;
4755
};
4856

4957

Algorithms.h ProcessSchedulingAlgorithms.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@
1010
#include "Process.h"
1111
#include "sOS-Sim.h"
1212

13-
namespace Algorithms {
13+
namespace ProcessSchedulingAlgorithms {
1414

1515
static uint32_t maxProcessMultiprogramming = 0;
1616

1717
/***
1818
* First-Come-First-Served (FCFS) scheduling algorithm [also know as First-In-Fist-Out (FIFO)]
1919
* Used to medium-term Scheduling
20-
* @return (*readysuspendedQueue)[0]
20+
* @return (*waitingQueue)[0]
2121
*/
22-
static bool FCFS(std::vector<Process>* readysuspendedQueue, std::vector<Process>* readyQueue, double _elapsedTime) {
22+
static bool FCFS(std::vector<Process>* waitingQueue, std::vector<Process>* readyQueue, double _elapsedTime) {
2323
try {
24-
if (!readysuspendedQueue->empty()) {
24+
if (!waitingQueue->empty()) {
2525
Simulator::DebugLog(_elapsedTime,
26-
"Processo " + std::to_string((*readysuspendedQueue)[0].getPID()) + " pronto");
26+
"Processo " + std::to_string((*waitingQueue)[0].getPID()) + " pronto");
2727

28-
(*readysuspendedQueue)[0].updateSubmissionTime(_elapsedTime);
29-
readyQueue->push_back((*readysuspendedQueue)[0]);
30-
readysuspendedQueue->erase(readysuspendedQueue->begin());
28+
(*waitingQueue)[0].updateSubmissionTime(_elapsedTime);
29+
readyQueue->push_back((*waitingQueue)[0]);
30+
waitingQueue->erase(waitingQueue->begin());
3131
return true;
3232
} else return false;
3333
}
@@ -169,7 +169,7 @@ namespace Algorithms {
169169
if (!readyQueue->empty()) {
170170

171171
// set Quantum as 4
172-
(*_quantum) = 4;
172+
(*_quantum) = 2;
173173
// set response time
174174
if (firstTimeRunning((*readyQueue)[0])) (*readyQueue)[0].setResponseTime(_elapsedTime);
175175

0 commit comments

Comments
 (0)