Optimal Page Replacement in Java

Optimal Page Replacement in JAVA

Optimal Page Replacement

The memory in an operating system is divided into pages. These pages are brought into the primary memory from the secondary memory as the CPU demands them. Pages that contain such critical data and information are swapped between different memory locations on the basis of their demand. To ensure that page which is not in frequent demand do not occupy critical memory space, the operating system sends it back to the secondary memory using a scheduling algorithm. The concept is also known as the Optimal Page Replacement technique.

Optimal Page replacement program in JAVA

// importing packages to use classes in the page replacement program
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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(System.in));
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
System.out.println(" Enter the total number of Frames: ");
frames = Integer.parseInt(br.readLine());

//Entering the string size of the reference
System.out.println(" Enter the reference string size:");
strng_size = Integer.parseInt(br.readLine());

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
System.out.println(" Enter the reference string: ");
for(int i = 0; i < strng_size; i++)
{
ref[i] = Integer.parseInt(br.readLine());
}

System.out.println();
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++)
System.out.printf("%3d ",mem_layout[j][i]);
System.out.println();
}

System.out.println("Hits: " + hit);
System.out.println("Hit Ratio: " + (float)((float)hit/str_len));
System.out.println("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

1 1 1 1 1 1 1 1 6 6 6 6 6 6 6 6 6 2 4 4
-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