First In First Out page replacement algorithm (FIFO) program in java

The Simplest page replacement algorithm is First In First Out (FIFO) .A First In First Out replacement algorithm (FIFO) associated with each page the time when that page was brought into memory.When a page must be replaced ,the oldest page is chosen.We can create First In First Out (FIFO) queue to hold all pages in memory.The First In First Out page replacement algorithm (FIFO) is easy to understand and program.


import java.io. * ;
class FIFO {
	public static void main(String args[]) throws IOException {
		int fifo[] = new int[3];
		int n;
		BufferedReader br = new BufferedReader(new InputStreamReader(System. in ));
		System.out.println("Enter the number of inputs :");
		n = Integer.parseInt(br.readLine());
		int inp[] = new int[n];
		System.out.println("Enter the inputs:");
		for (int i = 0; i < n; i++)
		inp[i] = Integer.parseInt(br.readLine());
		System.out.println("---------------");
		for (int i = 0; i < 3; 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 < 3; k++)
			if (fifo[k] == inp[i]) {
				check = true;
				Hit = Hit + 1;
			}
			if (check == false) {
				fifo[j] = inp[i];
				j++;
				if (j >= 3) j = 0;
				Fault = Fault + 1;
			}

		}
		System.out.println("HIT:" + Hit + " FAULT;" + Fault);
	}
}

/*

First In First Out page replacement algorithm (FIFO) Output:

Enter the number of inputs :
10
Enter the inputs:
2
3
5
4
2
5
7
3
8
7
---------------
HIT:2 FAULT;8

*/

You may also like...

1 Response

  1. vidyalankar says:

    right hai lekin kuch samjha nai

Leave a Reply

Your email address will not be published. Required fields are marked *