-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.cpp
366 lines (357 loc) · 9.79 KB
/
project.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include <bits/stdc++.h>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
string get_address_from_PT(int);
string file_name;
char divide_symbol;
stringstream ss;
const string file_ext = ".txt";
class Page
{
public:
int ind;
string data;
int last_used;
static int clock;
Page()
{
ind = -1;
data = "";
last_used = -1;
}
Page(int i, string s)
{
cout << "Page created\n";
ind = i;
data = s;
}
};
int get_digits(int number)
{
int count = 0;
while (number != 0)
{
number = number / 10;
count++;
}
return count;
}
string check_table(int page_no)
{
// resundant to get_address_from_PT
string mystring;
ifstream page_num; // why are you doing this bro ?
page_num.open("table.txt"); // page_no filename.txt
string addr = "";
if (page_num.is_open())
{
while (getline(page_num, mystring))
{
if (mystring[0] == page_no + '0') // int(3) + '0' = ascii(3)
{
for (int i = 1 + get_digits(page_no); i < mystring.size(); i++)
{
addr += mystring[i];
}
cout << " string name = " << mystring;
break;
}
}
}
ifstream page;
page.open(addr);
if (page.is_open())
{
while (getline(page, mystring))
{
cout << mystring << endl;
}
}
return addr;
}
void get_page(int page_no, vector<pair<int, string>> &disk_mem, int m, map<int, int> last_used)
{
// checks if page no. is in disk mem
int n = disk_mem.size();
for (int i = 0; i < n; i++)
{
if (disk_mem[i].first == page_no)
{
cout << "HIT\n";
cout << "Page contains: \n";
ifstream page;
page.open(disk_mem[i].second);
string mystring;
if (page.is_open())
{
while (getline(page, mystring))
{
cout << mystring << endl;
}
}
last_used[page_no]++;
return;
}
}
cout << "MISS\n";
string addr = get_address_from_PT(page_no);
int x = -1;
int y = INT_MAX;
int mx = INT_MIN;
for (int i = 0; i < n; i++)
{
if (last_used[disk_mem[i].first] > mx)
{
mx = last_used[disk_mem[i].first];
}
if (last_used[disk_mem[i].first] < y)
{
y = last_used[disk_mem[i].first];
x = disk_mem[i].first;
}
}
if (n < m)
{
disk_mem.push_back({page_no, addr});
last_used[page_no] = mx + 1;
cout << "!" << page_no << "!" << addr << "!!!!" << endl;
return;
}
else
{
cout << "::\n";
for (int i = 0; i < n; i++)
{
if (disk_mem[i].first == x)
{
disk_mem[i].first = page_no;
disk_mem[i].second = addr;
last_used.erase(x);
mx += 1;
cout << "!!!" << x << "!!" << y << "!!" << mx << endl;
last_used[page_no] = mx + 1;
break;
}
}
return;
}
}
void divide(string file_name, char divide_symbol)
{
// takes file path , opens file , divides into multiple files using symbol
ifstream myfile;
int counter = 0;
string file_ext = ".txt";
myfile.open(file_name + file_ext);
string mystring;
string tmpstring = "";
ofstream content;
ofstream table;
content.open(file_name + "_content" + file_ext);
table.open(file_name + "_table" + file_ext);
if (myfile.is_open())
{
while (getline(myfile, mystring)) // fetch one line
{
if (mystring[0] == divide_symbol) // check if it starts with #
{
if (counter > 0)
{
ofstream tempfile; // open a new file and usme store kardo
tempfile.open(file_name + "_" + to_string(counter) + file_ext);
tempfile << tmpstring;
tempfile.close();
}
tmpstring = mystring;
counter++;
content << to_string(counter) + " " << mystring + "\n";
table << to_string(counter) + " " + file_name + "_" + to_string(counter) + file_ext << endl;
}
else
{
tmpstring += "\n" + mystring;
}
cout << mystring << endl;
}
ofstream tempfile;
tempfile.open(file_name + "_" + to_string(counter) + file_ext);
tempfile << tmpstring;
tempfile.close();
content.close();
table.close();
}
}
string get_address_from_PT(int page_num)
{
// takes page no and returns the page file name
string PT_name = file_name + "_table" + file_ext;
ifstream table;
table.open(PT_name);
string tempstring;
string temppgno;
int tmp;
if (table.is_open())
{
while (getline(table, tempstring))
{
temppgno = string(tempstring.begin(), tempstring.begin() + get_digits(page_num));
cout << "This is the extracted page no. bro" << temppgno << endl;
ss << temppgno;
ss >> tmp;
if (temppgno.compare(to_string(page_num))==0)
{
cout << "Got the page no.\n";
cout<<"------- FOR PN="<<page_num<<" Address="<<string(tempstring.begin()+1 + get_digits(page_num), tempstring.end())<<endl;
return string(tempstring.begin()+1 + get_digits(page_num), tempstring.end()); // yaha 1 mat bhulna
}
}
}
return "Page Fault, Page Adress Not Found";
}
string get_data_from_PN(int page_num)
{
// directly hands over the data inside the page stored in hard disk
ifstream tempstream;
cout<<"Start of getdatafrom PN\n";
string data = "", tmpstring = "";
cout<<"Addres ="<<get_address_from_PT(page_num)<<endl;
tempstream.open(get_address_from_PT(page_num));
if (tempstream.is_open())
{
while (getline(tempstream, tmpstring)) // reading each line
{
cout<<"tempstring = "<<tmpstring<<endl;
cout<<"Data = "<<data<<endl;
data += tmpstring + "\n";
}
}
cout<<" \n-----returned data = ";
cout<<data<<endl;
return data;
}
Page get_page_from_PN(int page_num)
{
return Page(page_num, get_data_from_PN(page_num));
}
void merge()
{
ifstream myfile;
myfile.open("laptop_list.txt");
string mystring;
if (myfile.is_open())
{
while (getline(myfile, mystring))
{
cout << mystring << endl;
}
}
}
int check_page_in_PM(int page_num, vector<Page> &primary_mem)
{
// returns index of required page_num inside Primary mem , -1 if not found
for( int i = 0 ; i<primary_mem.size(); i++)
{
if (primary_mem[i].ind == page_num) // IS IT SAME AS OUR PAGE NO
{
return i;
}
}
return -1;
}
int clck = 0;
bool isEmpty(vector<Page> &primary_mem)
{
// checks if the Page vector is empty or not
for (auto i : primary_mem)
{
if (i.ind != -1)
return false;
}
return true;
}
int get_replace_index(vector<Page> &primary_mem)
{
// returns the index of Least Recently used Page in Primary memory
int min = primary_mem[0].last_used, i, temp = 0;
int n = primary_mem.size();
for (i = 1; i < n; i++)
{
if (min > primary_mem[i].last_used)
{
min = primary_mem[i].last_used;
temp = i;
}
}
cout<<" Replacable index = "<<temp<<endl;
return temp;
}
int load_page(int page_num, vector<Page> &primary_mem)
{
// har baar ek ek karke load karega ye banda
int temp = get_replace_index(primary_mem); // LRU implement
primary_mem[temp] = get_page_from_PN(page_num);
primary_mem[temp].last_used = clck;
cout<<"Page loaded"<<primary_mem[temp].data<<" ind ="<<primary_mem[temp].ind<<endl;
//also need to increase the last used variable ;
return 0;
}
int main()
{
cout << "Enter File name to divide\n";
getline(cin, file_name); // take laptop_list
cout << "Enter divide symbol\n";
cin >> divide_symbol; // #
// cout << file_name;
divide(file_name, divide_symbol); // divide the big file
cout << "Enter number of pages to be stored in primary memory: ";
vector<pair<int, string>> disk_mem;
int primary_mem_size;
cin >> primary_mem_size;
vector<Page> prim_mem(primary_mem_size, Page()); // alternate to disk_mem
map<int, int> last_used;
while (true)
{
clck++;
cout << "Enter page no: ";
int p;
cin >> p;
// get_page(p,disk_mem,primary_mem_size,last_used);
int temp = check_page_in_PM(p, prim_mem);
cout<<"Temp = checkpageinPM resposne"<<temp<<endl;
cout << "Returned Page ind=" << prim_mem[temp].ind << " data=" << prim_mem[temp].data << endl;
cout<<"---Data of page"<<p<<"= "<<get_data_from_PN(p)<<endl;
// if (temp == -1)
// {
// cout<<"Page not found in MM\n";
// load_page(p, prim_mem);
// }
// else
// {
// cout<<"Page found bro !!!!\n";
// cout<<"Data = "<<prim_mem[temp].data<<endl;
// }
if (p == -1)
return 0;
}
// while (true)
// {
// cout << "Enter operation to perform:\n1.Get entire file\n2.Get a page\n3.Exit";
// int c;
// cin >> c;
// if (c == 1)
// {
// merge();
// }
// else if (c == 2)
// {
// get_page();
// }
// else
// {
// break;
// }
// }
return 0;
}