Programming

Page Replacement Algorithm in Java like: FIFO,LRU,LFU

Page Replacement Algorithm like: FIFO,LRU (least recently used), LFU (least frequently used) program in Java:

Download Its Perfect Copy in .txt Format

Instruction to run the below program in windows OS:

1.save the program as   .java in bin folder of jdk.
2.compile the program using ” javac “in cmd prompt
3.run the file using java command.

INPUT is: rectangle.java :
SOURCE CODE:
Advertisements

24 thoughts on “Page Replacement Algorithm in Java like: FIFO,LRU,LFU”

    1. //LFU code fixed
      import java.io.*;

      class LFU
      {
      public static void main(String args[])throws IOException
      {
      BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
      int f,page=0,ch,pgf=0,n,chn=0;
      boolean flag;
      int pages[];
      int k=0;
      pgf=0;
      int sml;
      System.out.println(“enter no. of frames: “);
      f=Integer.parseInt(obj.readLine());
      int frame2[]=new int[f];
      int cnt[]=new int [f];
      flag=true;

      for(int i=0;i<f;i++)
      {
      frame2[i]=-1;
      cnt[i]=0;
      }
      System.out.println("enter the no of pages ");
      n=Integer.parseInt(obj.readLine());
      pages=new int[n];
      System.out.println("enter the page no ");
      for(int j=0;j<n;j++)
      pages[j]=Integer.parseInt(obj.readLine());
      //do
      //{
      int pg=0;
      for(pg=0;pg<n;pg++)
      {
      page=pages[pg];
      flag=true;

      for(int j=0;j<f;j++)
      {
      if(page==frame2[j])
      {
      flag=false;
      cnt[j]++;
      break;
      }
      }
      if(flag)
      {

      sml=cnt[0];
      for(int j=0;j<f;j++)
      {
      if(cnt[j]<sml)
      {
      sml=cnt[j];
      //break;
      }
      }
      for(int j=0;j<f;j++)
      {
      if(sml==cnt[j] )
      {
      frame2[j]=page;
      k=j;
      break;
      }
      }
      cnt[k]=1;
      System.out.print("frame :");
      for(int j=0;j<f;j++)
      System.out.print(frame2[j]+" ");
      System.out.println();

      pgf++;
      }
      else
      {
      System.out.print("frame :");
      for(int j=0;j<f;j++)
      System.out.print(frame2[j]+" ");

      System.out.println();

      }
      chn++;
      }
      //}while(chn!=n);
      System.out.println("Page fault:"+pgf);

      }
      }

      1. Really thanks.
        Honestly, I didn’t tested your code yet.
        However if apart your from you someone tested this, then PLEASE confirm here so that I can update the code download links for updated code versions.

  1. I’ve tried to compline by using “javac Replacepage.java” in CMD but there was an error.
    Nothing .Class was created.
    What wrong i did?

    1. Thanks for testing my code and your query has:
      3-ways to solve this problem:
      1. the general format of compiling a .java file is:
      ” javac “, where your file name should match with main class name,which public static void main(). so kindly check is this criteria is satisfied.

      2.If problem still persist,then I would suggest you to copy only the program code in new file,without any bottom output comment(given in .txt downloadable file), and save it and run,your problem will be solved.

      3. and if your problem is not yet solved,so please copy that compile time error message and paste it in the google.com , you will definitely get something worthwhile.

  2. Here your fault i detected lastnight:
    “Aim:implement page replacement” without ” //” >> made javac not to execute.
    thanks u for soft.It’s very useful in my subject.

  3. Thank You for the information. Can you please post a code for the optimal page replacement algorithm? And it would be great if it could read the data from a file…

    1. No Dude,Actually at present ,I’m not working on such things, So thnks for comment on this Blog, And Reply to all your comment, is tht, if U like Content on this Blog,Take it, And Thnks For Comment.

  4. Hi Bro, I happened to stumble upon this code.

    However, it would be nice if you could tell me what I did wrong.

    I compiled these codes in JCreator Pro V4.00 LE,
    there was no problem.

    However, the output was either all 7 -1 -1
    for all frames, and page fault was 1, or the entire
    frame of -1 keeps looping and crashes.

    I did import java.io.*;

    I was testing out FIFO, using your sample.

  5. I’m doing this on JFrames and not on command prompt. May I know how you implement this on to the JFrames and JPanels or help me with the algorithm in FIFO? I’m just using 2 Textbox of Page Frames and the Reference String.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s