soon. We can keep track of the last time each block was accessed (assign a timestamp
to the block), and select as the victim block the block that has been used
least recently. This is the least recently used (LRU) algorithm. Unfortunately,
LRU requires the system to keep a history of accesses for every cache block,
which requires significant space and slows down the operation of the cache.
There are ways to approximate LRU, but that is beyond the scope of this book.
(Refer to the references at the end of the chapter for more information.)
First in, first out (FIFO) is another popular approach. With this algorithm,
the block that has been in cache the longest (regardless of how recently it has
been used) would be selected as the victim to be removed from cache memory.
Another approach is to select a victim at random. The problem with LRU and
FIFO is that there are degenerate referencing situations in which they can be made to
thrash (constantly throw out a block, then bring it back, then throw it out, then bring
it back, repeatedly). Some people argue that random replacement, although it sometimes
throws out data that will be needed soon, never thrashes. Unfortunately, it is
difficult to have truly random replacement, and it can decrease average performance.
The algorithm selected often depends on how the system will be used. No
single (practical) algorithm is best for all scenarios. For that reason, designers use
algorithms that perform well under a wide variety of circumstances.