-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b03439
commit df94692
Showing
10 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"files.associations": { | ||
"iostream": "cpp" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
clean: | ||
rm -rf *.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <iostream> | ||
#include <fstream> | ||
#include <vector> | ||
#include <queue> | ||
#include <algorithm> | ||
#include <cstdlib> | ||
#include <ctime> | ||
using namespace std; | ||
|
||
void FIFO(int pages, int frames, vector<int> requests) | ||
{ | ||
vector<int> pageTable(pages, -1); // array of size eq to num of addressable pages | ||
queue<int> pageQueue; // to store pages in queue, i.e. which are in main memory | ||
int pageFaults = 0; | ||
int hit = 0; | ||
// cout << "Total Requests: "<< requests.size() << endl; | ||
for (int i = 0; i < requests.size(); i++) | ||
{ | ||
int page = requests[i] - 1; | ||
// cout << "Requesting " << page+1 <<" ..."; | ||
if (pageTable[page] == -1) | ||
{ | ||
pageFaults++; | ||
// cout << "FAULT"; | ||
if (pageQueue.size() == frames) // no empty slots left | ||
{ | ||
int victimPage = pageQueue.front(); | ||
pageTable[victimPage] = -1; // eviction by fifo policy | ||
pageQueue.pop(); | ||
} | ||
pageQueue.push(page); | ||
pageTable[page] = 1; | ||
} else { | ||
// cout << "HIT"; | ||
hit++; | ||
} | ||
// cout << endl; | ||
} | ||
cout << "FIFO Page Faults: " << pageFaults << endl; | ||
cout << "FIFO Page Hits: " << hit << endl; | ||
|
||
} | ||
|
||
// hits: 5 12 16 for 10 20 30 | ||
int main(int argc, char *argv[]) | ||
{ | ||
int pages = atoi(argv[1]); | ||
int frames = atoi(argv[2]); | ||
int blocks = atoi(argv[3]); | ||
string filename = argv[4]; | ||
vector<int> requests; | ||
ifstream infile(filename); | ||
int page; | ||
while (infile >> page) { | ||
requests.push_back(page); | ||
} | ||
infile.close(); | ||
FIFO(pages, frames, requests); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include <iostream> | ||
#include <fstream> | ||
#include <vector> | ||
#include <queue> | ||
#include <algorithm> | ||
#include <cstdlib> | ||
using namespace std; | ||
|
||
void LRU(int pages, int frames, vector<int> requests) | ||
{ | ||
vector<int> pageTable(pages, -1); // array of size eq to num of addressable pages | ||
vector<int> pageList; // to store pages in order of their use | ||
int pageFaults = 0; | ||
int hit = 0; | ||
// cout << "Total Requests: "<< requests.size() << endl; | ||
for (int i = 0; i < requests.size(); i++) | ||
{ | ||
int page = requests[i] - 1; | ||
// cout << "Requesting " << page+1 <<" ..."; | ||
if (pageTable[page] == -1) | ||
{ | ||
pageFaults++; | ||
// cout << "FAULT"; | ||
if (pageList.size() == frames) // no empty slots left | ||
{ | ||
int victimPage = pageList.back(); | ||
pageTable[victimPage] = -1; // eviction by lru policy | ||
pageList.pop_back(); // evict | ||
} | ||
pageList.insert(pageList.begin(), page); // insert at beg | ||
pageTable[page] = 1; | ||
} else { | ||
// cout << "HIT"; | ||
hit++; | ||
pageList.erase(find(pageList.begin(), pageList.end(), page)); | ||
pageList.insert(pageList.begin(), page); | ||
} | ||
// cout << endl; | ||
} | ||
cout << "LRU Page Faults: " << pageFaults << endl; | ||
cout << "LRU Page Hits: " << hit << endl; | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int pages = atoi(argv[1]); | ||
int frames = atoi(argv[2]); | ||
int blocks = atoi(argv[3]); | ||
string filename = argv[4]; | ||
vector<int> requests; | ||
ifstream infile(filename); | ||
int page; | ||
while (infile >> page) { | ||
requests.push_back(page); | ||
} | ||
infile.close(); | ||
LRU(pages, frames, requests); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include <iostream> | ||
#include <fstream> | ||
#include <vector> | ||
#include <queue> | ||
#include <algorithm> | ||
#include <cstdlib> | ||
#include <ctime> | ||
using namespace std; | ||
|
||
void Random(int pages, int frames, vector<int> requests) | ||
{ | ||
vector<int> pageTable(pages, -1); | ||
int pageFaults = 0; | ||
int hit = 0; | ||
srand(time(NULL)); | ||
for (int i = 0; i < requests.size(); i++) | ||
{ | ||
int page = requests[i]; | ||
if (pageTable[page] == -1) | ||
{ | ||
pageFaults++; | ||
if (count(pageTable.begin(), pageTable.end(), 1) == frames) | ||
{ | ||
int victimPage = rand() % pages; | ||
while (pageTable[victimPage] != 1) | ||
{ | ||
victimPage = rand() % pages; | ||
} | ||
pageTable[victimPage] = -1; | ||
} | ||
pageTable[page] = 1; | ||
} else { | ||
hit++; | ||
} | ||
} | ||
cout << "Random Page Faults: " << pageFaults << endl; | ||
cout << "Random Page Hits: " << hit << endl; | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int pages = atoi(argv[1]); | ||
int frames = atoi(argv[2]); | ||
int blocks = atoi(argv[3]); | ||
string filename = argv[4]; | ||
vector<int> requests; | ||
ifstream infile(filename); | ||
int page; | ||
while (infile >> page) | ||
{ | ||
requests.push_back(page); | ||
} | ||
infile.close(); | ||
Random(pages, frames, requests); | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 10 32 2 12 5 2 16 9 30 21 35 6 8 7 17 22 38 45 53 43 10 8 20 30 16 18 56 60 57 53 27 35 24 32 13 17 4 5 18 20 52 28 25 18 9 19 3 31 59 11 6 23 28 37 48 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/bash | ||
|
||
g++ fifo.cpp -o fifo.out | ||
./fifo.out $1 $2 $3 $4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
|
||
g++ lru.cpp -o lru.out | ||
./lru.out $1 $2 $3 $4 | ||
# 60 20 60 req1.dat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/bash | ||
|
||
g++ random.cpp -o random.out | ||
./random.out $1 $2 $3 $4 |