Search code examples
paginglru

Calculating cache memory based on LRU algorithm


Assuming i have 4 blocks of cache memory, Using the LRU (Least Recently Used) replacement algorithm on this following sequence of access to memory blocks: 1 2 3 4 5 2 5 4 1 5 2 3 :

1   2   3   4   5   2   5   4   1   5   2   3

1   1   1   1   5   5   5   5   5   5   5   5
    2   2   2   2   2   2   2   2   2   2   2
        3   3   3   3   3   3   1   1   1   1
            4   4   4   4   4   4   4   4   3

So in the end, the cache memory will contains this memory blocks : "5 2 1 3"

But the correct result is "1 5 2 3"

Please tell me what am I doing wrong here!

Edit:

I will be honest, I'm doing an excercise and can't get help from anywhere but here, and may be I misread the question, so this is the original question :

enter image description here


Solution

  • So is my simulation of the LRU wrong? Honestly i still don't get it!

    The simulation goes awry in column 5 where you add the newest data to the oldest position.

    Here's the correction:

                1   2   3   3   3   2   2   4   1  <-- Oldest accesses
            1   2   3   4   4   2   5   4   1   5
        1   2   3   4   5   2   5   4   1   5   2
    1   2   3   4   5   2   5   4   1   5   2   3  <-- Newest accesses
    
    • Notice that the newest row is always equal to your input sequence.
    • The other elements age one row at a time unless they are brought to the front with a new access