Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 378ccaf

Browse files
code files
1 parent 69dd0bf commit 378ccaf

File tree

5 files changed

+572
-0
lines changed

5 files changed

+572
-0
lines changed

‎Part2/Code/fifo.cpp‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <sstream>
4+
#include <vector>
5+
#include <queue>
6+
#include <unordered_set>
7+
using namespace std;
8+
9+
//FIFO implementation
10+
int ffPR(const vector<int>& rStr, int fC) { //rstr as referenceString, fC as frameCount
11+
unordered_set<int> fam; //frames as fam, pagesQueue as pgQ
12+
queue<int> pgQ; // To maintain the fifo order
13+
int pgF = 0; //pageFaults as pgF
14+
15+
for (int pg : rStr) { //page as pg
16+
17+
if (fam.find(pg) == fam.end()) {
18+
19+
pgF++;
20+
21+
// If memory is full, remove the oldest page
22+
if (fam.size() == fC) {
23+
int oldPG = pgQ.front();
24+
pgQ.pop();
25+
fam.erase(oldPG);
26+
}
27+
28+
// Add the new page to memory and the queue
29+
fam.insert(pg);
30+
pgQ.push(pg);
31+
}
32+
}
33+
34+
return pgF;
35+
}
36+
37+
38+
void pTC(const string& fileName) { //pTC as processTestCase
39+
ifstream file(fileName);
40+
if (!file.is_open()) {
41+
cerr << "Error: Could not open file " << fileName << endl;
42+
return;
43+
}
44+
45+
string line;
46+
vector<int> rStr;
47+
int fC = 0;
48+
49+
while (getline(file, line)) {
50+
if (line.find("Reference String:") != string::npos) {
51+
// Extract reference string
52+
stringstream ss(line.substr(line.find(":") + 1));
53+
int pg;
54+
while (ss >> pg) {
55+
rStr.push_back(pg);
56+
if (ss.peek() == ',') ss.ignore();
57+
}
58+
} else if (line.find("Frame Count:") != string::npos) {
59+
// Extract frame count
60+
fC = stoi(line.substr(line.find(":") + 1));
61+
}
62+
}
63+
file.close();
64+
65+
66+
int totlPgF = ffPR(rStr, fC);
67+
68+
69+
cout << "File: " << fileName << endl;
70+
cout << "Reference String: ";
71+
for (int pg : rStr) cout << pg << " ";
72+
cout << "\nFrame Count: " << fC << endl;
73+
cout << "Total Page Faults: " << totlPgF << "\n" << endl;
74+
}
75+
76+
int main(int argc, char *argv[]) {
77+
78+
if (argc < 2) {
79+
cerr << "Usage: " << argv[0]
80+
<< " <referenceString1.txt> <referenceString2.txt> <referenceString3.txt> "
81+
<< "<referenceString4.txt> <referenceString5.txt> <referenceString6.txt> "
82+
<< "<referenceString7.txt> <referenceString8.txt> <referenceString9.txt> "
83+
<< "<referenceString10.txt>" << endl;
84+
return 1;
85+
}
86+
87+
88+
for (int i = 1; i < argc; ++i) {
89+
pTC(argv[i]);
90+
}
91+
92+
return 0;
93+
}

‎Part2/Code/lfu.cpp‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <vector>
4+
#include <unordered_map>
5+
#include <sstream>
6+
#include <algorithm>
7+
#include <string>
8+
using namespace std;
9+
10+
// Implementation of the function to find least frequency
11+
int findLFU(const vector<int>& fam, const unordered_map<int, int>& fqncy) {
12+
int lfu_pg = fam[0]; //lfu_page as lfupg
13+
int min_fqncy = fqncy.at(lfu_pg);
14+
15+
for (int pg : fam) {
16+
if (fqncy.at(pg) < min_fqncy) {
17+
lfu_pg = pg;
18+
min_fqncy = fqncy.at(pg);
19+
}
20+
}
21+
return lfu_pg;
22+
}
23+
24+
// LFU Page Replacement Algorithm
25+
int lfuPageReplacement(const vector<int>& rStr, int fC) {
26+
vector<int> fam; // To store current frames
27+
unordered_map<int, int> fqncy; // To store frequency of pages
28+
int pgF = 0;
29+
30+
for (int pg : rStr) {
31+
fqncy[pg]++;
32+
33+
34+
if (find(fam.begin(), fam.end(), pg) != fam.end()) {
35+
continue; // No page fault
36+
}
37+
38+
pgF++;
39+
if (fam.size() < fC) {
40+
41+
fam.push_back(pg);
42+
} else {
43+
44+
int lfu_pg = findLFU(fam, fqncy);
45+
auto it = find(fam.begin(), fam.end(), lfu_pg);
46+
if (it != fam.end()) {
47+
fam.erase(it); // Remove LFU page
48+
}
49+
fam.push_back(pg); // Add new page
50+
}
51+
}
52+
53+
return pgF;
54+
}
55+
56+
void pTC(const string& fileName) {
57+
ifstream inputFile(fileName);
58+
if (!inputFile.is_open()) {
59+
cerr << "Error opening file: " << fileName << endl;
60+
return;
61+
}
62+
63+
string line, referenceStringLine, frameCountLine;
64+
vector<int> rStr;
65+
int fC = 0;
66+
67+
while (getline(inputFile, line)) {
68+
if (line.find("Reference String:") != string::npos) {
69+
// Extract the reference string after "Reference String:"
70+
string refStr = line.substr(line.find(":") + 1);
71+
stringstream refStream(refStr);
72+
string pageStr;
73+
while (getline(refStream, pageStr, ',')) {
74+
rStr.push_back(stoi(pageStr)); // Parse pages
75+
}
76+
} else if (line.find("Frame Count:") != string::npos) {
77+
// Extract the frame count after "Frame Count:"
78+
string frameCountStr = line.substr(line.find(":") + 1);
79+
fC = stoi(frameCountStr); // Parse frame count
80+
}
81+
}
82+
83+
inputFile.close();
84+
85+
86+
if (rStr.empty() || fC == 0) {
87+
cerr << "Invalid input format in file: " << fileName << endl;
88+
return;
89+
}
90+
91+
92+
int totlPgF = lfuPageReplacement(rStr, fC);
93+
94+
95+
cout << "File: " << fileName << endl;
96+
cout << "Reference String: ";
97+
for (int page : rStr) cout << page << " ";
98+
cout << "\nFrame Count: " << fC << endl;
99+
cout << "Total Page Faults: " << totlPgF << "\n" << endl;
100+
}
101+
102+
int main(int argc, char* argv[]) {
103+
104+
if (argc < 2) {
105+
cerr << "Usage: " << argv[0]
106+
<< " <referenceString1.txt> <referenceString2.txt> <referenceString3.txt> "
107+
<< "<referenceString4.txt> <referenceString5.txt> <referenceString6.txt> "
108+
<< "<referenceString7.txt> <referenceString8.txt> <referenceString9.txt> "
109+
<< "<referenceString10.txt>" << endl;
110+
return 1;
111+
}
112+
113+
114+
for (int i = 1; i < argc; ++i) {
115+
pTC(argv[i]);
116+
}
117+
118+
return 0;
119+
}

‎Part2/Code/lru.cpp‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <sstream>
4+
#include <vector>
5+
using namespace std;
6+
7+
8+
struct Page {
9+
int pgNum; //pageNumber as pgNum, referenceBit as refB, capacity as cap
10+
bool refB;
11+
12+
Page(int pgNum) : pgNum(pgNum), refB(false) {}
13+
};
14+
15+
// Implementation of LRU Approximation with Second Chance Page Replacement Algorithm
16+
int lruAproxSC(const vector<int>& pgs, int cap) {
17+
vector<Page> fams; // frames as fams, pageFaults as pgF, pointer as pntr
18+
int pgF = 0;
19+
int pntr = 0;
20+
21+
for (int curntPg : pgs) {
22+
bool pgFnd = false; //pageFound as pgFnd
23+
24+
25+
for (auto &page : fams) {
26+
if (page.pgNum == curntPg) {
27+
page.refB = true; // if the Page is hit then Set the reference bit to 1
28+
pgFnd = true;
29+
break;
30+
}
31+
}
32+
33+
if (!pgFnd) {
34+
35+
pgF++;
36+
37+
if (fams.size() < cap) {
38+
39+
fams.push_back(Page(curntPg));
40+
} else {
41+
42+
while (true) {
43+
if (!fams[pntr].refB) {
44+
// Incase reference bit is set to 0 then replace this page
45+
fams[pntr] = Page(curntPg);
46+
break;
47+
} else {
48+
// If reference bit is set to 1 the reset it to 0 and move the pointer
49+
fams[pntr].refB = false;
50+
pntr = (pntr + 1) % cap;
51+
}
52+
}
53+
}
54+
55+
pntr = (pntr + 1) % cap;
56+
}
57+
}
58+
59+
return pgF;
60+
}
61+
62+
void pTC(const string& fileName) {
63+
ifstream file(fileName);
64+
if (!file.is_open()) {
65+
cerr << "Error: Could not open file " << fileName << endl;
66+
return;
67+
}
68+
69+
string line;
70+
vector<int> rStr;
71+
int fC = 0;
72+
73+
74+
while (getline(file, line)) {
75+
if (line.find("Reference String:") != string::npos) {
76+
// Extract the reference string
77+
stringstream ss(line.substr(line.find(":") + 1));
78+
int page;
79+
while (ss >> page) {
80+
rStr.push_back(page);
81+
if (ss.peek() == ',') ss.ignore(); // Ignore commas
82+
}
83+
} else if (line.find("Frame Count:") != string::npos) {
84+
// Extract the frame count
85+
fC = stoi(line.substr(line.find(":") + 1));
86+
}
87+
}
88+
file.close();
89+
90+
91+
int totalPageFaults = lruAproxSC(rStr, fC);
92+
93+
94+
cout << "File: " << fileName << endl;
95+
cout << "Reference String: ";
96+
for (int page : rStr) cout << page << " ";
97+
cout << "\nFrame Count: " << fC << endl;
98+
cout << "Total Page Faults of LRU(Second Chance): " << totalPageFaults << "\n" << endl;
99+
}
100+
101+
int main(int argc, char *argv[]) {
102+
103+
if (argc < 2) {
104+
cerr << "Usage: " << argv[0]
105+
<< " <referenceString1.txt> <referenceString2.txt> <referenceString3.txt> "
106+
<< "<referenceString4.txt> <referenceString5.txt> <referenceString6.txt> "
107+
<< "<referenceString7.txt> <referenceString8.txt> <referenceString9.txt> "
108+
<< "<referenceString10.txt>" << endl;
109+
return 1;
110+
}
111+
112+
113+
for (int i = 1; i < argc; ++i) {
114+
pTC(argv[i]);
115+
}
116+
117+
return 0;
118+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /