a year ago
2.0 kB
ALGORITHM Least Recently Used
//page replacement technique used for memory management that replaces the page that is least recently used
//INPUT: Frames, Page Request, Memory
//OUTPUT: Page Fault
pageRequest[] ← [1, 2, 3, 4, 1, 5, 1, 2, 6, 7, 2, 1, 5, 3, 4, 6]    //sets the value for pageReqiest
capacity ← 3    //size of frames
cache ←  new lru_algorithm(capacity);   //initialize a variable to keep track of the number of page faults that occur
pageFaults ← 0  //initializes pageFaults value to 0

BEGIN                          // marks the beginning of the pseudocode
  READ capacity                // reads in the capacity of the cache
  SET cache ← new LinkedHashMap<Integer, Integer>(capacity, 0.75f, true)  // initializes the cache as a LinkedHashMap with the given capacity and sets the access order flag to true
  FOR i ← 0 to pageRequests.length - 1 DO  // iterates over each page request in the array
    SET pageRequest ← pageRequests[i]   // sets the current page request to the value at the current index in the array
    SET value ← cache.get(pageRequest)  // gets the corresponding value from the cache for the current page request
    IF value = null THEN                // checks if the value is null, indicating a cache miss
      SET value ← -1                    // sets the value to -1, indicating a page fault
      INCREMENT pageFaults by 1         // increments the page fault counter
      SET value ← pageRequest           // sets the value to the current page request
      cache.put(pageRequest, value)     // inserts the new page request and value into the cache
    END IF
    PRINT pageRequest, cache.keySet(), pageFaults  // prints out the current page request, the current state of the cache (as a set of keys), and the current page fault count
  PRINT "Total page faults:", pageFaults   // prints out the total number of page faults after the loop completes
END                                  // marks the end of the pseudocode