-
Notifications
You must be signed in to change notification settings - Fork 1
/
LRU.py
50 lines (38 loc) · 1.13 KB
/
LRU.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from frame import Frame
def LRU(frames, pages):
page_error = 0
time = 0
for page in pages:
page_found = False
# When page is already in a frame
for frame in frames:
if frame.page == page:
page_found = True
frame.set_change(time)
break
if page_found:
time += 1
continue
# Checking if any frame is empty
for frame in frames:
if frame.page is None:
frame.set_page(page, time)
page_found = True
page_error += 1
break
if page_found:
time += 1
continue
current = None
frame_chosen = None
# Choosing a first-in frame
for frame in frames:
if current is None or frame.change < current:
current = frame.change
frame_chosen = frame
# Changing page for the last recently used page
frame_chosen.set_page(page, time)
page_error += 1
time += 1
# Returning number of page errors
return page_error