{
static int nof,nop,i,index,m,frames[]=new int[100],fs[]=new int [100],n,miss=0,hit=0,l=0,flag1,flag2;
public static void main(String args[])
{
Scanner s=new Scanner(System.in); System.out.println("Enter the number of frames : "); nof=s.nextInt(); for(i=0;i<nof;i++) { frames[i]=-1; fs[i]=0; }
System.out.println("Enter the number of pages : "); nop=s.nextInt();; int pages[]=new int[10]; System.out.println("Enter the pages : "); for(i=0;i<nop;i++) { pages[i]=s.nextInt();; } while(l<nop) { flag1=0;flag2=0; for(int k=0;k<nof;k++) { if(pages[l]==frames[k]) { hit++; flag1=1; flag2=1; break; } } if(flag1==0) { for(i=0;i<nof;i++) { if(frames[i]==-1) { frames[i]=pages[l]; miss++; flag2=1; break; } } } if(flag2==0) { for(i=0;i<nof;i++) fs[i]=0; for(m=l-1,n=1;n<nof;n++,m--) { for(i=0;i<nof;i++) { if(frames[i]==pages[m]) fs[i]=1; } } for(i=0;i<nof;i++) { if(fs[i]==0) index=i; } frames[index]=pages[l]; miss++; } l=l+1; display(); }
}
static void display()
{
for(i=0;i<nof;i++) { System.out.println("\n"+frames[i]); } System.out.println("\nMiss : "+miss+"\n\n\n");
}
}
/* OUTPUT
D:\SECMPN 60>javac LRU.java
D:\SECMPN 60>java LRU
Enter the number of frames :
3
Enter the number of pages :
7
Enter the pages :
1
2
4
1
3
5
6
1
-1
-1
Miss : 1
1
2
-1
Miss : 2
1
2
4
Miss : 3
1
2
4
Miss : 3
1
3
4
Miss : 4
1
3
5
Miss : 5
6
3
5
Miss :