LRU de sustitución de páginas Técnica:
Cuando se hace referencia a una página, la página puede ser requerida en la memoria caché.
If in the cache
: tenemos que ponerlo al frente de la cola del caché.
If NOT in the cache
: lo traemos en caché. En palabras simples, agregamos una nueva página al frente de la cola del caché. Si la caché está llena, es decir, todos los marcos están llenos, eliminamos una página de la parte posterior de la cola de caché y agregamos la nueva página al frente de la cola de caché.
# Cache Size
csize = int(input())
# Sequence of pages
pages = list(map(int,input().split()))
# Take a cache list
cache=[]
# Keep track of number of elements in cache
n=0
# Count Page Fault
fault=0
for page in pages:
# If page exists in cache
if page in cache:
# Move the page to front as it is most recent page
# First remove from cache and then append at front
cache.remove(page)
cache.append(page)
else:
# Cache is full
if(n==csize):
# Remove the least recent page
cache.pop(0)
else:
# Increment element count in cache
n=n+1
# Page not exist in cache => Page Fault
fault += 1
cache.append(page)
print("Page Fault:",fault)
de entrada/salida
Input:
3
1 2 3 4 1 2 5 1 2 3 4 5
Output:
Page Fault: 10
Véase también http://stackoverflow.com/questions/3639744/least-recently-used-cache-using-c y http: // stackoverflow. com/questions/2057424/lru-implementation-in-production-code – timday