Write a program to simulate the Least-Recently-Used (LRU) Page Replacement Algorithm that appears on page 366 of the textbook. In particular, the simulator should accept the following input from standard input (stdin):

  • Number of Virtual Pages
  • Number of Page Frames Available (not used by page table)
  • Number of References
  • Page Referenced Read or Write (0 or 1, respectively) duration
  • Page Referenced Read or Write (0 or 1, respectively) duration
  • ::

For example:

5
3
2
0 0 2
1 1 1
3 0 1

is valid input. There are 5 virtual pages (numbered 0, 1, ... 4), 3 page frames (numbered 0, 1, 2) in memory, 3 references listed: page 0 is read from for 2 clock ticks, page 1 is written to for 1 clock tick, and page 3 is read from for 1 clock tick.

Maintain a page table as an array. Use page number as an index to the array. The array should include an R-bit, an M-bit, and a Present/Absent bit, as well as the frame currently mapped.

R-bit = 1 if page has been referenced recently (read/write) M-bit = 1 if page has been modified (needs to be written to disk)

Corresponding to the previous example, the page table could look like this way:

1 0 1 0
1 1 1 1
0 0 0 0
1 0 1 2
0 0 0 0

The first row indicates the virtual page 0 is mapped to the physical page 0. The page has been referenced and is present in the physical memory.

Assume that the R-bit is reset to 0 after a set of 6 pages have been referenced. This is to simulate a reset after each clock interrupt.

The output from the simulator should indicate whenever a fault occurs, and which page has been evicted (if one is evicted). Always select the smallest numbered page in a class to be evicted. In addition, the output should include the total number of faults that occurred.

The possible output would look something like:

: :
Fault: no page evicted, page 0 brought in to memory.
Fault: page 2 evicted, page 3 brought in to memory.
: :
Total number of faults is 23.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.