Write a Java program that calculates the number of page faul

Write a Java program that calculates the number of page faults using three different page replacement algorithms.

- The program should accept 2 command line arguments, a file name and the number of frames. You would run the program like: java hw2 input1.txt 3

- The program should open the file (the first argument) and read in the page reference string. Two example files have been provided (input1.txt and input2.txt). The file will have one line of data consisting of comma separated numbers (there won\'t be any negative values).

- The program should then calculate and output the number of page faults for the optimal, FIFO, and LRU page replacement algorithms.

- The number of frames must be dynamic (since it is given as an argument) but will be at least 1. So from 1 to some very large number, it should still work. Do NOT hard code this value.

- It is best that you try out additional input files (other than the ones I provide) for testing.


*******************
Example outputs
*******************

The output for input1.txt with 3 frames would be:

Optimal: 9
FIFO: 15
LRU: 12


The output for input2.txt with 2 frames would be:

Optimal: 4
FIFO:
LRU:

Solution

Assuming input1.txt and input2.txt as

7,0,1,2,0,3,1,7,2,3

No.of frames : 3

Command Line arguments : input1.txt 3

package com.pagereplacementAlg;

import java.io.*;

class PageReplacementAlg {
   public static void main(String args[]) throws IOException

   {
       BufferedReader br = new BufferedReader(new FileReader(args[0]));
       String strPages = br.readLine();
       String pages1[] = strPages.split(\",\");
       int pages[]= new int[pages1.length];
      
       for (int i = 0; i < pages1.length; i++) {
            try {
                pages[i] = Integer.parseInt(pages1[i]);
            } catch (NumberFormatException nfe) {
             
            };
       }
      
       int f = Integer.parseInt(args[1]);
       System.out.println(pages1[2]);
       BufferedReader obj = new BufferedReader(
               new InputStreamReader(System.in));

       int page = 0, ch, n,pgf = 0, chn = 0;

       n = pages.length;
       boolean flag;


      

           System.out.println(\"Menu\");

           System.out.println(\"1.FIFO\");

           System.out.println(\"2.LRU\");

           System.out.println(\"3.LFU\");

           System.out.println(\"4.EXIT\");

           System.out.println(\"ENTER YOUR CHOICE: \");

           ch = Integer.parseInt(obj.readLine());

           switch (ch)

           {

           case 1:

               int pt = 0;

               int frame[] = new int[f];

               for (int i = 0; i < f; i++)

               {

                   frame[i] = -1;

               }

               do {

                   int pg = 0;

                   for (pg = 0; pg < n; pg++)

                   {

                       page = pages[pg];

                       flag = true;

                       for (int j = 0; j < f; j++)

                       {

                           if (page == frame[j])

                           {

                               flag = false;

                               break;

                           }

                       }

                       if (flag)

                       {

                           frame[pt] = page;

                           pt++;

                           if (pt == f)

                               pt = 0;

                           System.out.print(\"frame :\");

                           for (int j = 0; j < f; j++)

                               System.out.print(frame[j] + \"   \");

                           System.out.println();

                           pgf++;

                       }

                       else

                       {

                           System.out.print(\"frame :\");

                           for (int j = 0; j < f; j++)

                               System.out.print(frame[j] + \" \");

                           System.out.println();

                       }

                       chn++;

                   }

               } while (chn != n);

               System.out.println(\"Page fault:\" + pgf);

               break;

           case 2:

               int k = 0;


               int frame1[] = new int[f];

               int a[] = new int[f];

               int b[] = new int[f];

               for (int i = 0; i < f; i++)

               {

                   frame1[i] = -1;

                   a[i] = -1;

                   b[i] = -1;

               }

               do {

                   int pg = 0;

                   for (pg = 0; pg < n; pg++)

                   {

                       page = pages[pg];

                       flag = true;

                       for (int j = 0; j < f; j++)

                       {

                           if (page == frame1[j])

                           {
                               flag = false;
                               break;
                           }

                       }

                       for (int j = 0; j < f && flag; j++)

                       {

                           if (frame1[j] == a[f - 1])

                           {
                               k = j;

                               break;
                           }

                       }

                       if (flag)

                       {

                           frame1[k] = page;

                           System.out.println(\"frame :\");

                           for (int j = 0; j < f; j++)

                               System.out.print(frame1[j] + \" \");

                           pgf++;

                           System.out.println();

                       }

                       else

                       {

                           System.out.println(\"frame :\");

                           for (int j = 0; j < f; j++)

                               System.out.print(frame1[j] + \" \");

                           System.out.println();

                       }

                       int p = 1;

                       b[0] = page;

                       for (int j = 0; j < a.length; j++)

                       {

                           if (page != a[j] && p < f)

                           {

                               b[p] = a[j];

                               p++;

                           }

                       }

                       for (int j = 0; j < f; j++)

                       {

                           a[j] = b[j];

                       }

                       chn++;

                   }

               } while (chn != n);

               System.out.println(\"Page fault:\" + pgf);

               break;

           case 3:
              
               int pointer = 0, hit = 0, fault = 0;
                boolean isFull = false;
                int buffer[];
                int mem_layout[][];

               n = pages1.length;
              
                mem_layout = new int[n][f];
                buffer = new int[f];
                for(int j = 0; j < f; j++)
                        buffer[j] = -1;
              
                for(int i = 0; i < n; i++)
                {
                 int search = -1;
                 for(int j = 0; j < f; j++)
                 {
                  if(buffer[j] == pages[i])
                  {
                   search = j;
                   hit++;
                   break;
                  }
                 }
                 if(search == -1)
                 {
                  if(isFull)
                  {
                   int index[] = new int[f];
                   boolean index_flag[] = new boolean[f];
                   for(int j = i + 1; j < n; j++)
                   {
                    for(int k1 = 0; k1 < f; k1++)
                    {
                     if((pages[j] == buffer[k1]) && (index_flag[k1] == false))
                     {
                      index[k1] = j;
                      index_flag[k1] = true;
                      break;
                     }
                    }
                   }
                   int max = index[0];
                   pointer = 0;
                   if(max == 0)
                    max = 200;
                   for(int j = 0; j < f; j++)
                   {
                    if(index[j] == 0)
                     index[j] = 200;
                    if(index[j] > max)
                    {
                     max = index[j];
                     pointer = j;
                    }
                   }
                  }
                  buffer[pointer] = pages[i];
                  fault++;
                  if(!isFull)
                  {
                   pointer++;
                      if(pointer == f)
                      {
                       pointer = 0;
                       isFull = true;
                      }
                  }
                 }
                    for(int j = 0; j < f; j++)
                        mem_layout[i][j] = buffer[j];
                }
              
                for(int i = 0; i < f; i++)
                {
                    for(int j = 0; j < n; j++)
                        System.out.printf(\"%3d \",mem_layout[j][i]);
                    System.out.println();
                }
              
                System.out.println(\"The number of Faults: \" + fault);

           case 4:

               break;

           }

   }

}

Write a Java program that calculates the number of page faults using three different page replacement algorithms. - The program should accept 2 command line arg
Write a Java program that calculates the number of page faults using three different page replacement algorithms. - The program should accept 2 command line arg
Write a Java program that calculates the number of page faults using three different page replacement algorithms. - The program should accept 2 command line arg
Write a Java program that calculates the number of page faults using three different page replacement algorithms. - The program should accept 2 command line arg

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site