Monday, 7 August 2017

fifo.java (OS)

import java.io.*;
class Stack
{
private int array[], frameSize, top, hit, fault;

Stack(int n)
{
frameSize = n;
array=new int[frameSize];
hit=0;
fault=0;
top=0;
for(int i=0;i<frameSize;i++)
array[i]=-1;
}
void print()
{
for(int i:array)
System.out.print(i+" ");
System.out.println();
}
boolean isHit(int n)
{
for(int i:array)
if(i==n)
{
hit++;
return true;
}
return false;
}
void insert(int n)
{
print();
if(!isHit(n))
{
array[top]=n;
top=(top+1)%frameSize;
fault++;
}
}
int getHit()
{
return hit;
}
int getFault()
{
return fault;
}
}
class FIFO
{
public static void main(String args[])
{
Stack s=new Stack(3);
int n=20;
int inp[]={7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};
for(int i=0;i<n;i++)
s.insert(inp[i]);
System.out.println("HIT : "+s.getHit()+" FAULT : "+s.getFault());
}
}

Output:

-1 -1 -1
7 -1 -1
7 0 -1
7 0 1
2 0 1
2 0 1
2 3 1
2 3 0
4 3 0
4 2 0
4 2 3
0 2 3
0 2 3
0 2 3
0 1 3
0 1 2
0 1 2
0 1 2
7 1 2
7 0 2
HIT : 5 FAULT : 15

No comments:

Post a Comment