-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bcsm-S21-047 OS Project.cpp
411 lines (349 loc) · 10.4 KB
/
Bcsm-S21-047 OS 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <string>
#include <windows.h>
#include <stdlib.h>
using namespace std;
bool isPowerOfTwo(int n)
{
if (n == 0)
return false;
return (ceil(log2(n)) == floor(log2(n)));
}
string setcolor(unsigned short color)
{
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon, color);
return "";
}
void String_table_generator_oneBit(string arr[],int x,int Number_of_bits)
{
string zeros;
for(int i=0; i<Number_of_bits; i++)
{
zeros=zeros+"0";
}
for(int i=0; i<x; i++)
{
arr[i]=zeros;
}
for(int i =0; i<x; i++)
{
int k = Number_of_bits-1;
int n=i;
for(int j=Number_of_bits-1; n>0; j--)
{
int p = n%2;
if(p==1)
{
arr[i][k] = '1';
}
else
{
arr[i][k] = '0';
}
n=n/2;
k--;
}
}
}
void String_table_generator(string **arr,int x,int y,int Number_of_bits)
{
string zeros;
for(int i=0; i<Number_of_bits; i++)
{
zeros=zeros+"0";
}
for(int i=0; i<x; i++)
{
for( int j=0; j<y; j++)
{
arr[i][0]="Null";
arr[i][1]=zeros;
}
}
for(int i =0; i<x; i++)
{
int k = Number_of_bits-1;
int n=i;
for(int j=y-1; n>0; j--)
{
int p = n%2;
if(p==1)
{
arr[i][1][k] = '1';
}
else
{
arr[i][1][k] = '0';
}
n=n/2;
k--;
}
}
}
void string_arr_DisplayOneBit(string arr[],int x)
{
for( int i=0; i<x; i++)
{
cout<<i<<" ";
cout<<arr[i]<<" ";
cout<<endl;
}
}
void string_arr_Display(string **arr,int x,int y)
{
cout<<"-------------------"<<endl;
cout<<"Pages"<<"\t"<<"Frame"<<endl;
cout<<"-------------------"<<endl;
for( int i=0; i<x; i++)
{
for( int j=0; j<y; j++)
{
cout<<arr[i][j]<<"\t";
}
cout<<endl;
}
}
void Display_Memories(string **logical_arr,string **Physical_arr,int x,int y,int pageSize,int Segment_bit)
{
cout<<"\n ------------------------------------- > Displaying < -------------------------------------\n"<<endl;
cout<<"-------------------------------------------------------------------------------------------"<<endl;
cout<<"Virtual Addresses"<<"\t\t\t\t\t\t\t"<<"Physical Addresses"<<endl;
cout<<"-------------------------------------------------------------------------------------------"<<endl;
int br=0;
for( int i=0; i<x; i++)
{
if(br==pageSize)
{
cout<<"--------------------------"<<"\t\t\t\t\t"<<"--------------------------"<<endl;
br=0;
}
br++;
if(logical_arr[i][0]!="Null")
{
setcolor(2);
}
if(i<10)
{
cout<<"0";
}
cout<<i<<" ";
for( int j=0; j<y; j++)
{
cout<<setw(10)<<logical_arr[i][j];
}
setcolor(7);
cout<<"\t\t\t\t\t";
if(i<10)
{
cout<<"0";
}
cout<<i<<" ";
if(Physical_arr[i][0]!="Null")
{
setcolor(2);
}
for( int j=0; j<y; j++)
{
cout<<setw(10)<<Physical_arr[i][j];
}
cout<<endl;
setcolor(7);
}
}
void Page_table(string **page_table,string Phy_frame[],string logi_page[],int x)
{
for(int i=0; i<x; i++)
{
page_table[i][0] = logi_page[i];
page_table[i][1] = Phy_frame[rand() % x];
}
}
int main()
{
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
cout << "\t * * * * * * * * * * * * * * * * * * * * * * ** * * * " << endl;
Sleep(50);
cout << "\t * * * * * * * * * * * * * * * * * " << endl;
Sleep(50);
cout << "\t * * * * * * * * * * * * * * * * *** * * * " << endl;
Sleep(50);
cout << "\t * * * * * * * * * * * * * * * * * " << endl;
Sleep(50);
cout << "\t * * * * * * * * * * * * * * * * * * * * * * * ** * * * " << endl;
Sleep(50);
cout << "\t\t\t\t=============================================" << endl;
Sleep(50);
cout << "\t\t\t\t====MEMORY MANAGEMENT - OPERATING SYSTEM====" << endl;
Sleep(50);
cout << "\t\t\t\t=============================================" << endl;
Sleep(50);
cout << "\n\n\t\t\t\t======================PROJECT BY=====================" << endl;
Sleep(50);
cout << "\n\n\n\t\t\t\tBCSM-S21-047--------------Muneeb Ahmad\n";
Sleep(50);
cout << "\t\t\t\t=============================================" << endl;
Sleep(50);
cout<<endl;
cout<<endl;
cout<<endl;
int Space_Size=0;
label:
cout<<"Enter Logical/Physical Address Size (Space Size): ";
cin>>Space_Size;
if(!isPowerOfTwo(Space_Size))
{
setcolor(4);
cout<<"Invalid Space Size!!!"<<endl;
cout<<"Space Size Must be in power of 2"<<endl;
setcolor(7);
cout<<"Again ";
goto label;
}
int Number_of_bits = (ceil(log2(Space_Size)));
//cout<<"Number of Bits Are : "<<Number_of_bits<<endl;
label1:
int Page_size = 0;
cout<<"Enter Page Size: ";
cin>>Page_size;
if(!isPowerOfTwo(Page_size))
{
setcolor(4);
cout<<"Invalid Page Number!!!"<<endl;
cout<<"Page Number Must be in power of 2"<<endl;
setcolor(7);
cout<<"Again ";
goto label1;
}
int Number_of_Pages = Space_Size/Page_size;
cout<<"---- > Total Number of Pages Are : "<<Number_of_Pages<<endl;
int Segment_bits = (ceil(log2(Number_of_Pages)));
//cout<<"BreakBit Are : "<<Segment_bits<<endl;
int offset_bits = Number_of_bits - Segment_bits;
//cout<<"Offset Bits Are : "<<offset_bits<<endl;
int Segment_Address_Size = pow(2,Segment_bits);
int Offset_Address_Size = pow(2,offset_bits);
//Arrays For Logical Memory
string Logical_main_Add[Segment_Address_Size];
string Logical_Offset_Add[Offset_Address_Size];
string **Logical_Add = new string *[Space_Size];
for(int i=0; i<Space_Size; i++)
{
Logical_Add[i] = new string[2];
}
//Arrays For Physical Memory
string Physical_main_Add[Segment_Address_Size];
string Physical_Offset_Add[Offset_Address_Size];
string **Physical_Add = new string *[Space_Size];
for(int i=0; i<Space_Size; i++)
{
Physical_Add[i] = new string[2];
}
String_table_generator_oneBit(Logical_main_Add,Segment_Address_Size,Segment_bits);
String_table_generator_oneBit(Logical_Offset_Add,Offset_Address_Size,offset_bits);
String_table_generator_oneBit(Physical_main_Add,Segment_Address_Size,Segment_bits);
String_table_generator_oneBit(Physical_Offset_Add,Offset_Address_Size,offset_bits);
String_table_generator(Logical_Add,Space_Size,2,Number_of_bits);
String_table_generator(Physical_Add,Space_Size,2,Number_of_bits);
cout<<"\n\nDisplaying Logical Main Address"<<endl;
string_arr_DisplayOneBit(Logical_main_Add,Segment_Address_Size);
//cout<<"\n\nDisplaying Logical Offset Address"<<endl;
//string_arr_DisplayOneBit(Logical_Offset_Add,y);
cout<<"\n\nDisplaying Physical Main Address"<<endl;
string_arr_DisplayOneBit(Physical_main_Add,Segment_Address_Size);
//cout<<"\n\nDisplaying Physical Offset Address"<<endl;
//string_arr_DisplayOneBit(Physical_Offset_Add,y);
// cout<<"\n\nDisplaying logical Address"<<endl;
// string_arr_Display(Logical_Add,Space_Size,2);
// cout<<"\n\nDisplaying Physical Address"<<endl;
// string_arr_Display(Physical_Add,Space_Size,2);
Display_Memories(Logical_Add,Physical_Add,Space_Size,2,Page_size,Segment_bits);
string **Pagetable = new string *[Segment_Address_Size];
for(int i=0; i<Segment_Address_Size; i++)
{
Pagetable[i] = new string[2];
}
Page_table(Pagetable,Logical_main_Add,Physical_main_Add,Segment_Address_Size);
cout<<"\n\nDisplaying PageTable"<<endl;
string_arr_Display(Pagetable,Segment_Address_Size,2);
string Instruction;
label6:
cout<<"\n\nEnter the Instruction : ";
getline(cin >> ws, Instruction);
int inc_place;
label3:
cout<<"Enter Instruction Address (in Decimal) : ";
cin>>inc_place;
if(inc_place>Space_Size)
{
setcolor(4);
cout<<"Invalid Space Size!!!"<<endl;
cout<<"Space Size Must be in power of 2"<<endl;
setcolor(7);
cout<<"Again ";
goto label3;
}
if(Logical_Add[inc_place][0]!="Null")
{
setcolor(4);
cout<<"This Block Is Already filled With Another Instruction"<<endl;
cout<<"Space Size Must be in power of 2"<<endl;
setcolor(7);
cout<<"Again ";
goto label3;
}
Logical_Add[inc_place][0]=Instruction;
string seg_logi,seg_phy,offset_logi,fin_add;
for(int i =0; i<Segment_bits; i++)
{
seg_logi = seg_logi + Logical_Add[inc_place][1][i];
}
for(int i =Segment_bits; i<Number_of_bits; i++)
{
offset_logi = offset_logi + Logical_Add[inc_place][1][i];
}
cout<<"\nSegment address is: "<<seg_logi<<endl;
cout<<"\nOffset address is: "<<offset_logi<<endl;
for(int i = 0 ; i < Segment_Address_Size ; i++)
{
if(Pagetable[i][0]==seg_logi)
{
seg_phy=Pagetable[i][1];
cout<<"\n\nIn PageTable -- "<<Pagetable[i][0]<<" <==> "<<Pagetable[i][1]<<endl;
}
}
fin_add = seg_phy+offset_logi;
for(int i =0 ; i<Space_Size; i++)
{
if(Physical_Add[i][1] == fin_add)
{
Physical_Add[i][0] = Instruction;
}
}
cout<<"\n\nDisplaying Address Sheet"<<endl;
Display_Memories(Logical_Add,Physical_Add,Space_Size,2,Page_size,Segment_bits);
char a;
label5:
cout<<"\n\n Do you Want to Enter Another Instruction (y/n) : ";
cin>>a;
if(a=='y' || a=='Y')
{
goto label6;
}
else if(a=='n' || a=='N')
{
exit(1);
}
else
{
setcolor(4);
cout<<"Invalid Input:"<<endl;
setcolor(7);
goto label5;
}
}