title: implementing LRU in C++
body: <p>I am trying to implement LRU  Page Replacement. I  was able to get FIFO algorithm to work. But i am not sure how to keep track of the least recently used?</p>
  
  <p>I am reading in a file. its structured like first number is pid(1) and second number is the ref(45) and so forth Like:</p>
  
  <pre><code>1 45
  1 46
  1 45
  1 44
  2 76
  2 75
  2 77
  2 77
  </code></pre>
  
  <p>So, i am using a class array, and parsing the file  line by line and if its not in the array to put the pid and ref there in that index. If the array is full then go back to the beginning ad start all over.</p>
  
  <pre><code>class pagetable
  {
  
  public:
  int pid;
  int ref;
  int faults;
  pagetable();
  };
  pagetable* page = new pagetable[frames];
  </code></pre>
  
  <p>I am prompting for the number of frames.
  I am prompting for the file name and storing it in</p>
  
  <pre><code>ifstream inputStream;
  </code></pre>
  
  <p>Then i can call my LFU function and grab each pid and ref to check.</p>
  
  <pre><code>int runsimLFU(ifstream &amp;inputStream, pagetable* page, int frames ){
  
  int i =0;
  int j=0;
  bool flag = false;
  int cnt=0;
  int index = 0;
  int value = 0;
  
  while(1){
  
    inputStream &gt;&gt; pid;
    inputStream &gt;&gt; ref;
  
        page[count].pid = pid;
        page[count].ref = ref;
    pagefaults++;
  </code></pre>
  
  <p>Something like this i can keep grabbing each line of the file.</p>
  
  <p>this is how i am searching the array</p>
  
  <pre><code>bool findinarray(pagetable* page, int frames, int pid, int ref)
  {
  
   for(int i=0; i &lt; frames+1; i++) {
      if(page[i].pid == pid &amp;&amp; page[i].ref == ref)
      {
          return true;
  
      }
   }
      return false;
  
   }
  </code></pre>
  
  <p>Two questions</p>
  
  <p>1) I am unsure how to keep track of the LRU. i would imagine a second array and a counter variable but thats as far as i can see what to do.</p>
  
  <p>2) once i know the LRU and when the incoming pid, ref is not in the array i stuff that into the array at index LRU number?</p>
  
  <p>Thank you</p>