Instructions:
1 .Class name and java file name should be same.
2. Us the below command to run the programs
javac [Link]
java filename
1. Least Recently Used Page Replacement Policy:
import [Link].*;
class LRU
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("ENTER NUMBER OF FRAMES");
int nf=[Link]();
[Link]("ENTER NUMBER OF REFERENCES");
int nr=[Link]();
int page[]=new int[nr];
int frame[]=new int[nr];
[Link]("ENTER REFERENCE");
for(int i=0;i<nr;i++)
page[i]=[Link]();
for(int i=0;i<nf;i++)
frame[i]=-1;
for(int k=0;k<nf;k++)
[Link](" "+frame[k]);
[Link]();
int flag=0,hit=0,miss=0,front=0,rear=-1;
int age[]=new int[nf];
for(int i=0;i<nf;i++)
age[i]=0;
for(int j=0,i=0;j<nf && i<nr;j=j%nf,i++)
{
flag=0;
for(int y=0;y<nf;y++)
{
if(frame[y]==page[i])
{
flag=1;
hit++;
age[y]=0;
}
}
if(flag==0)
if(frame[j]==-1)
{
frame[j]=page[i];
miss++;
rear++;
j++;
}
else
{
int max=age[0],loc=0;
miss++;
for(int b=0;b<nf;b++)
if(age[b]>max)
{
max=age[b];loc=b;
}
frame[loc]=page[i];
age[loc]=0;
j++;
}
for(int k=0;k<nf;k++)
[Link](" "+frame[k]);
[Link]();
for(int a=0;a<nf;a++)
{
if(frame[a]==-1)
age[a]=0;
else
age[a]++;
}
/*for(int k=0;k<nf;k++)
[Link](" "+age[k]);
[Link]();*/
}
float hr=(float)hit/((float)hit+(float)miss);
[Link]("Hit="+hit+" miss="+miss);
[Link]("page replacement ratio="+hr);
}
}
OUTPUT:
ENTER NUMBER OF FRAMES
3
ENTER NUMBER OF REFERENCES
12
ENTER REFERENCE
1
2
3
4
1
2
5
1
2
3
4
5
-1 -1 -1
1 -1 -1
1 2 -1
123
423
413
412
512
512
512
312
342
345
Hit=2 miss=10
page replacement ratio=0.16666667
2. Optimal Page replacement Policy:
// importing packages to use classes in the page replacement program
import [Link];
import [Link];
import [Link];
public class OptimalReplacement {
// creation of the main class to implement Optimal page replacement algorithm
public static void main(pagestring[] args) throws IOException
{
Countbuffer bfr = new Countbuffer(new InputStreamReader([Link]));
int frames, pointer = 0, hit = 0, fault = 0,strng_size;
boolean isFull = false;
int buffer[];
int ref[];
int mem_layout[][];
//Entering the number of frames
[Link](" Enter the total number of Frames: ");
frames = [Link]([Link]());
//Entering the string size of the reference
[Link](" Enter the reference string size:");
strng_size = [Link]([Link]());
ref = new int[ref_len];
mem_layout = new int[strng_size][frames];
buffer = new int[frames];
for(int j = 0; j < frames; j++)
buffer[j] = -1;
//code to enter the reference string to carry out optimal page replacement
[Link](" Enter the reference string: ");
for(int i = 0; i < strng_size; i++)
{
ref[i] = [Link]([Link]());
}
[Link]();
for(int i = 0; i < strng_size; i++)
{
int search = -1;
for(int j = 0; j < frames; j++)
{
if(buffer[j] == ref[i])
{
search = j;
hit++;
break;
}
}
// code to update the stack checking its capacity
if(search == -1)
{
if(isFull)
{
int index[] = new int[frames];
boolean index_flag[] = new boolean[frames];
for(int j = i + 1; j < ref_len; j++)
{
for(int k = 0; k < frames; k++)
{
if((ref[j] == buffer[k]) && (index_flag[k] == false))
{
index[k] = j;
index_flag[k] = true;
break;
}
}
}
//updating pointer to the correct memory location after checking capacity
buffer[pointer] = ref[i];
fault++;
if(!isFull)
{
pointer++;
if(pointer == frames)
{
pointer = 0;
isFull = true;
}
}
}
for(int j = 0; j < frames; j++)
mem_layout[i][j] = buffer[j];
}
// code to display the number strings
for(int i = 0; i < frames; i++)
{
for(int j = 0; j < ref_len; j++)
[Link]("%3d ",mem_layout[j][i]);
[Link]();
}
[Link]("Hits: " + hit);
[Link]("Hit Ratio: " + (float)((float)hit/str_len));
[Link]("Faults: " + fault);
}
}
OUTPUT:
Enter the total number of Frames:
3
Enter the reference string size:
20
Enter the reference string:
1
2
3
2
1
5
2
1
6
2
5
6
3
1
3
6
1
2
4
3
11111111666666666244
-1 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1
-1 -1 3 3 3 5 5 5 5 5 5 5 3 3 3 3 3 3 3 3
Hits: 11
Hit Ratio: 0.55
Faults: 9
3. First In First Out (FIFO) Page Replacement Policy:
import [Link].*;
class FIFO
{
public static void main(String args[]) throws IOException
{
int n;
int f;
float rat;
BufferedReader br=new BufferedReader
(new InputStreamReader([Link]));
[Link]("Enter the number of FRAMES :");
f=[Link]([Link]());
int fifo[]=new int[f];
[Link]("Enter the number of INPUTS :");
n=[Link]([Link]());
int inp[]=new int[n];
[Link]("Enter INPUT:");
for(int i=0;i<n;i++)
inp[i]=[Link]([Link]());
[Link]("----------------------");
for(int i=0;i<f;i++)
fifo[i]=-1;
int Hit=0;
int Fault=0;
int j=0;
boolean check;
for(int i=0;i<n;i++)
{
check=false;
for(int k=0;k<f;k++)
if(fifo[k]==inp[i])
{
check=true;
Hit=Hit+1;
}
if(check==false)
{
fifo[j]=inp[i];
j++;
if(j>=f)
j=0;
Fault=Fault+1;
}
}
rat = (float)Hit/(float)n;
[Link]("HIT:"+Hit+" FAULT:"+Fault+" HIT RATIO:"+rat);
}
}
OUTPUT:
Enter the number of FRAMES :
3
Enter the number of INPUTS :
12
Enter INPUT:
1
2
3
4
1
2
5
1
2
3
4
5
----------------------
HIT:3 FAULT:9 HIT RATIO:0.25