Skip to content

Commit

Permalink
a8
Browse files Browse the repository at this point in the history
  • Loading branch information
Whitelisted2 committed Mar 19, 2023
1 parent 9b03439 commit df94692
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Assignment 08/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"iostream": "cpp"
}
}
2 changes: 2 additions & 0 deletions Assignment 08/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
clean:
rm -rf *.out
60 changes: 60 additions & 0 deletions Assignment 08/fifo.cpp
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;
}
59 changes: 59 additions & 0 deletions Assignment 08/lru.cpp
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;
}
56 changes: 56 additions & 0 deletions Assignment 08/random.cpp
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 added Assignment 08/random.out
Binary file not shown.
1 change: 1 addition & 0 deletions Assignment 08/req1.dat
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
4 changes: 4 additions & 0 deletions Assignment 08/run_fifo.sh
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
5 changes: 5 additions & 0 deletions Assignment 08/run_lru.sh
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
4 changes: 4 additions & 0 deletions Assignment 08/run_random.sh
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

0 comments on commit df94692

Please sign in to comment.