Clock Page Replacement algorithm in java

On-campus and online computer science courses to Learn the basic concepts of Computer Science.This tutorial will cover c ,c++, java, data structure and algorithm,computer graphics,microprocessor,analysis of algorithms,Digital Logic Design and Analysis,computer architecture,computer networks,operating system.
code in Java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ClockReplacement {

    public static void main(String[] args) throws IOException 
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int frames, pointer = 0, hit = 0, fault = 0,ref_len;
        int buffer[][];
        int reference[];
        int mem_layout[][];
  int used_layout[][];
        
        System.out.println("Please enter the number of Frames: ");
        frames = Integer.parseInt(br.readLine());
        
        System.out.println("Please enter the length of the Reference string: ");
        ref_len = Integer.parseInt(br.readLine());
        
        reference = new int[ref_len];
        mem_layout = new int[ref_len][frames];
  used_layout = new int[ref_len][frames];
        buffer = new int[frames][2];
        for(int j = 0; j < frames; j++)
        {
         buffer[j][0] = -1;
         buffer[j][1] = 0;
        }
        System.out.println("Please enter the reference string: ");
        for(int i = 0; i < ref_len; i++)
        {
            reference[i] = Integer.parseInt(br.readLine());
        }
        System.out.println();
        for(int i = 0; i < ref_len; i++)
        {
         int search = -1;
         for(int j = 0; j < frames; j++)
         {
          if(buffer[j][0] == reference[i])
          {
           search = j;
           hit++;
           buffer[j][1] = 1;
           break;
          } 
         }
         if(search == -1)
         {
          
          while(buffer[pointer][1] == 1)
          {
           buffer[pointer][1] = 0;
           pointer++;
           if(pointer == frames)
            pointer = 0;
          }
          buffer[pointer][0] = reference[i];
          buffer[pointer][1] = 1;
          fault++;
          pointer++;
          if(pointer == frames)
           pointer = 0;
         }
            for(int j = 0; j < frames; j++)
   {
    mem_layout[i][j] = buffer[j][0];
    used_layout[i][j] = buffer[j][1];
   }
        }
        
        for(int i = 0; i < frames; i++)
        {
            for(int j = 0; j < ref_len; j++)
                System.out.printf("%3d %d ",mem_layout[j][i],used_layout[j][i]);
            System.out.println();
        }
        
        System.out.println("The number of Hits: " + hit);
        System.out.println("Hit Ratio: " + (float)((float)hit/ref_len));
        System.out.println("The number of Faults: " + fault);
    }
    
}
output:-
Please enter the number of Frames: 
4
Please enter the length of the Reference string: 
12
Please enter the reference string: 
1
2
3
4
1
2
5
1
2
3
4
5

  1 1   1 1   1 1   1 1   1 1   1 1   5 1   5 1   5 1   5 1   4 1   4 1 
 -1 0   2 1   2 1   2 1   2 1   2 1   2 0   1 1   1 1   1 1   1 0   5 1 
 -1 0  -1 0   3 1   3 1   3 1   3 1   3 0   3 0   2 1   2 1   2 0   2 0 
 -1 0  -1 0  -1 0   4 1   4 1   4 1   4 0   4 0   4 0   3 1   3 0   3 0 
The number of Hits: 2
Hit Ratio: 0.16666667
The number of Faults: 10
--------------------------------

0 comments: