I am trying to implement a NRU page replacement algorithm. The algorithm itself is as follows: Break all pages that are in physical memory into 4 classes:
ΠΊΠ»Π°ΡΡ 0: R-Bit = 0, M-Bit = 0 ΠΊΠ»Π°ΡΡ 1: R-Bit = 0, M-Bit = 1 ΠΊΠ»Π°ΡΡ 2: R-Bit = 1, M-Bit = 0 ΠΊΠ»Π°ΡΡ 3: R-Bit = 1, M-Bit = 1 After that you need to select the page RANDOM page from the lowest non-empty class. This is a snatch of my code. It works and replaces the pages with one drawback - it searches for the page of the minimum class (that is, the banal search for the minimum) and returns this page as a candidate for replacement. The problem is that this is not a random page.
if ((processTable[pid].valid) && (processTable[pid].pageTable != NULL)) for (page = 0; page < processTable[pid].size; page++) { if (!isPagePresent(pid, page)) { continue; } else if (getPageClass(pid, page) <= minPageClass) { minPageClass = getPageClass(pid, page); pageToRemove = page; frame = processTable[pid].pageTable[page].frame; } } My other idea was to create 4 data structures for each class (for example, an array) and a travel page for each class to add to the data structure, and then extract it from the data structure using random data. But it comes out too complicated code and it seems to me that there is a simpler way, how can I find a random page without resorting to using 4 data structures .... Ie. essentially there are:
Π‘Π’Π ΠΠΠΠ¦Π ΠΠΠΠ‘Π‘ 1 2 2 1 3 3 4 0 5 1 6 0 And of all these, only pages 4 and 6 are minimal and you need to return the random of these two.