# Program for Page Replacement Algorithms | Set 1 ( LRU)

Prerequisite: Page Replacement Algorithms

In operating systems that use paging for memory management, page replacement algorithm are needed to decide which page needed to be replaced when new page comes in. Whenever a new page is referred and not present in memory, page fault occurs and Operating System replaces one of the existing pages with newly needed page. Different page replacement algorithms suggest different ways to decide which page to replace. The target for all algorithms is to reduce number of page faults.

In Least Recently Used (LRU) algorithm is a Greedy algorithm where the page to be replaced is least recently used. The idea is based on locality of reference, the least recently used page is not likely

Let say the page reference string 7 0 1 2 0 3 0 4 2 3 0 3 2 . Initially we have 4 page slots empty.
Initially all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults
0 is already their so —> 0 Page fault.
when 3 came it will take the place of 7 because it is least recently used —>1 Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0 Page fault because they are already available in the memory. Given memory capacity (as number of pages it can hold) and a string representing pages to be referred, write a function to find number of page faults.

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

```Let capacity be the number of pages that
memory can hold.  Let set be the current
set of pages in memory.

1- Start traversing the pages.
i) If set holds less pages than capacity.
a) Insert page into the set one by one until
the size  of set reaches capacity or all
page requests are processed.
b) Simultaneously maintain the recent occurred
index of each page in a map called indexes.
c) Increment page fault
ii) Else
If current page is present in set, do nothing.
Else
a) Find the page in the set that was least
recently used. We find it using index array.
We basically need to replace the page with
minimum index.
b) Replace the found page with current page.
c) Increment page faults.
d) Update index of current page.

2. Return page faults.
```

Below is implementation of above steps.

## C++

 `//C++ implementation of above algorithm ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find page faults using indexes ` `int` `pageFaults(``int` `pages[], ``int` `n, ``int` `capacity) ` `{ ` `    ``// To represent set of current pages. We use ` `    ``// an unordered_set so that we quickly check ` `    ``// if a page is present in set or not ` `    ``unordered_set<``int``> s; ` ` `  `    ``// To store least recently used indexes ` `    ``// of pages. ` `    ``unordered_map<``int``, ``int``> indexes; ` ` `  `    ``// Start from initial page ` `    ``int` `page_faults = 0; ` `    ``for` `(``int` `i=0; i

## Java

 `// Java implementation of above algorithm ` ` `  `import` `java.util.HashMap; ` `import` `java.util.HashSet; ` `import` `java.util.Iterator; ` ` `  `class` `Test ` `{ ` `    ``// Method to find page faults using indexes ` `    ``static` `int` `pageFaults(``int` `pages[], ``int` `n, ``int` `capacity) ` `    ``{ ` `        ``// To represent set of current pages. We use ` `        ``// an unordered_set so that we quickly check ` `        ``// if a page is present in set or not ` `        ``HashSet s = ``new` `HashSet<>(capacity); ` `      `  `        ``// To store least recently used indexes ` `        ``// of pages. ` `        ``HashMap indexes = ``new` `HashMap<>(); ` `      `  `        ``// Start from initial page ` `        ``int` `page_faults = ``0``; ` `        ``for` `(``int` `i=``0``; i itr = s.iterator(); ` `                     `  `                    ``while` `(itr.hasNext()) { ` `                        ``int` `temp = itr.next(); ` `                        ``if` `(indexes.get(temp) < lru) ` `                        ``{ ` `                            ``lru = indexes.get(temp); ` `                            ``val = temp; ` `                        ``} ` `                    ``} ` `                 `  `                    ``// Remove the indexes page ` `                    ``s.remove(val); ` `      `  `                    ``// insert the current page ` `                    ``s.add(pages[i]); ` `      `  `                    ``// Increment page faults ` `                    ``page_faults++; ` `                ``} ` `      `  `                ``// Update the current page index ` `                ``indexes.put(pages[i], i); ` `            ``} ` `        ``} ` `      `  `        ``return` `page_faults; ` `    ``} ` `     `  `    ``// Driver method ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `pages[] = {``7``, ``0``, ``1``, ``2``, ``0``, ``3``, ``0``, ``4``, ``2``, ``3``, ``0``, ``3``, ``2``}; ` `        `  `        ``int` `capacity = ``4``; ` `         `  `        ``System.out.println(pageFaults(pages, pages.length, capacity)); ` `    ``} ` `} ` `// This code is contributed by Gaurav Miglani `

Output:

```6
```

Another approach: (Without using HashMap)

 `// Java program for page replacement algorithms ` `import` `java.util.ArrayList; ` ` `  `public` `class` `LRU { ` `     `  `    ``// Driver method ` `    ``public` `static` `void` `main(String[] args) { ` `        ``int` `capacity = ``4``; ` `        ``int` `arr[] = {``7``, ``0``, ``1``, ``2``, ``0``, ``3``, ``0``, ``4``, ``2``, ``3``, ``0``, ``3``, ``2``}; ` `         `  `        ``// To represent set of current pages.We use ` `        ``// an Arraylist ` `        ``ArrayList s=``new` `ArrayList<>(capacity); ` `        ``int` `count=``0``; ` `        ``int` `page_faults=``0``; ` `        ``for``(``int` `i:arr) ` `        ``{ ` `            ``// Insert it into set if not present ` `            ``// already which represents page fault ` `            ``if``(!s.contains(i)) ` `            ``{ ` `             `  `            ``// Check if the set can hold equal pages ` `            ``if``(s.size()==capacity) ` `            ``{ ` `                ``s.remove(``0``); ` `                ``s.add(capacity-``1``,i); ` `            ``} ` `            ``else` `                ``s.add(count,i); ` `                ``// Increment page faults ` `                ``page_faults++; ` `                ``++count; ` `         `  `            ``} ` `            ``else` `            ``{ ` `                ``// Remove the indexes page ` `                ``s.remove((Object)i); ` `                ``// insert the current page ` `                ``s.add(s.size(),i);          ` `            ``} ` `         `  `        ``} ` `        ``System.out.println(page_faults); ` `    ``} ` `} `

Output:

```6
```

Note : We can also find the number of page hits. Just have to maintain a separate count.
If the current page is already in the memory then that must be count as Page-hit.

We will discuss other Page-replacement Algorithms in further sets.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.