Skip to content

Commit cac9e91

Browse files
authored
Added PAGE Replacement Algorithms
1 parent 4fe084d commit cac9e91

File tree

2 files changed

+270
-0
lines changed

2 files changed

+270
-0
lines changed

7PAGE_REPLACEMENT_FIFO.cxx

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include<iostream>
2+
#include<queue>
3+
4+
using namespace std;
5+
6+
int n; //Number of Frames in main memory
7+
8+
queue<char>ongoing;
9+
10+
bool present_in(char ch)
11+
{
12+
queue<char>local;
13+
local=ongoing;
14+
while(local.size()!=0)
15+
{
16+
if(local.front()==ch)
17+
{
18+
return true;
19+
}
20+
local.pop();
21+
}
22+
return false;
23+
24+
}
25+
26+
27+
void fifo(char string[])
28+
{
29+
char ch;
30+
int i=0;
31+
float fault=0;
32+
float hit=0;
33+
bool present;
34+
35+
ch=string[0];
36+
while(ch!='\0')
37+
{
38+
if(ongoing.size()!=n)
39+
{
40+
present=present_in(ch);
41+
42+
if(present)
43+
hit=hit+1;
44+
else
45+
{
46+
ongoing.push(ch);
47+
fault=fault+1;
48+
}
49+
50+
}
51+
else
52+
{
53+
present=present_in(ch);
54+
55+
if(present)
56+
hit=hit+1;
57+
else
58+
{
59+
ongoing.pop();
60+
ongoing.push(ch);
61+
fault=fault+1;
62+
}
63+
}
64+
65+
66+
67+
i++;
68+
ch=string[i];
69+
}
70+
71+
float hit_ratio,miss_ratio;
72+
hit_ratio=hit/i;
73+
miss_ratio=fault/i;
74+
75+
cout<<"\nNUMBER OF PAGES IN STRING IS "<<i;
76+
cout<<"\nNUMBER OF FAULTS IS "<<fault;
77+
cout<<"\nNUMBER OF HITS IS "<<hit;
78+
cout<<"\nHIT RATIO "<<hit_ratio;
79+
cout<<"\nMISS RATIO "<<miss_ratio;
80+
}
81+
82+
83+
int main()
84+
{
85+
char string[40];
86+
87+
cout<<"\n Enter the string ";
88+
cin>>string;
89+
90+
cout<<"\nEnter number of frames ";
91+
cin>>n;
92+
93+
fifo(string);
94+
95+
}

8PAGE_REPLACEMENT_LRU.cxx

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
#include<iostream>
2+
#include<queue>
3+
4+
using namespace std;
5+
6+
int n; //Number of Frames in main memory
7+
8+
queue<char>ongoing;
9+
queue<char>lru;
10+
11+
//TO CHECK WHEATHER A CHARACTER IS PRESENT IN A GIVEN QUEUE
12+
bool present_in(char ch,queue<char>myqueue)
13+
{
14+
queue<char>local;
15+
local=myqueue;
16+
while(local.size()!=0)
17+
{
18+
if(local.front()==ch)
19+
{
20+
return true;
21+
}
22+
local.pop();
23+
}
24+
return false;
25+
26+
}
27+
28+
//TO FILL THE LEAST RECENTLY USED QUEUE
29+
/*
30+
step1)If the size of lru is not equal to n(means less then n,n is the number of frames)
31+
CASE1)If character is present in LRU,Then remove the charater from its occuring position without disturbing the order of other elements and add it to the last in lru queue so that it becomes most recently used
32+
CASE2)If character is not present in the LRU then simply add character to the LRU list
33+
34+
step2)If the size of lru is equal to n(n is the number of frames)
35+
CASE1)If character is present in LRU,Then remove the character from its occuring position without disturbing the order of other elements and add it to the last in lru queue so that it becomes most recently used.
36+
CASE2)If character is not present in the LRU then simply pop out a character from LRU add character to the LRU list
37+
*/
38+
39+
void update_lru(char ch)
40+
{
41+
queue<char>local;
42+
while(lru.size()!=0)
43+
{
44+
if(lru.front()==ch)
45+
{
46+
lru.pop();
47+
}
48+
else
49+
{
50+
local.push(lru.front());
51+
lru.pop();
52+
}
53+
}
54+
local.push(ch);
55+
lru=local;
56+
}
57+
58+
59+
void fill_lru(char ch)
60+
{
61+
bool present;
62+
present=present_in(ch,lru);
63+
64+
65+
if(lru.size()!=n)
66+
{
67+
68+
if(present)
69+
update_lru(ch);
70+
else
71+
lru.push(ch);
72+
}
73+
74+
else
75+
{
76+
if(present)
77+
update_lru(ch);
78+
else
79+
{
80+
lru.pop();
81+
lru.push(ch);
82+
}
83+
}
84+
}
85+
86+
void least(char string[])
87+
{
88+
char ch;
89+
int i=0;
90+
float fault=0;
91+
float hit=0;
92+
bool present;
93+
94+
ch=string[0];
95+
while(ch!='\0')
96+
{
97+
present=present_in(ch,ongoing);
98+
99+
if(ongoing.size()!=n)
100+
{
101+
if(present)
102+
hit=hit+1;
103+
else
104+
{
105+
ongoing.push(ch);
106+
fault=fault+1;
107+
}
108+
109+
}
110+
111+
else
112+
{
113+
if(present)
114+
hit=hit+1;
115+
else
116+
{
117+
char replace;
118+
replace=lru.front();
119+
120+
queue<char>local;
121+
122+
fault=fault+1;
123+
124+
while(!ongoing.empty())
125+
{
126+
if(ongoing.front()==replace)
127+
{
128+
ongoing.pop();
129+
local.push(ch);
130+
131+
}
132+
else
133+
{
134+
local.push(ongoing.front());
135+
ongoing.pop();
136+
}
137+
}
138+
ongoing=local;
139+
140+
141+
}
142+
}
143+
144+
145+
fill_lru(ch);
146+
147+
i++;
148+
ch=string[i];
149+
}
150+
151+
float hit_ratio,miss_ratio;
152+
hit_ratio=hit/i;
153+
miss_ratio=fault/i;
154+
155+
cout<<"\nNUMBER OF PAGES IN STRING IS "<<i;
156+
cout<<"\nNUMBER OF FAULTS IS "<<fault;
157+
cout<<"\nNUMBER OF HITS IS "<<hit;
158+
cout<<"\nHIT RATIO "<<hit_ratio;
159+
cout<<"\nMISS RATIO "<<miss_ratio;
160+
}
161+
162+
163+
int main()
164+
{
165+
char string[40];
166+
167+
cout<<"\n Enter the string ";
168+
cin>>string;
169+
170+
cout<<"\nEnter number of frames ";
171+
cin>>n;
172+
173+
least(string);
174+
175+
}

0 commit comments

Comments
 (0)