aboutsummaryrefslogtreecommitdiff
path: root/lru.py
diff options
context:
space:
mode:
Diffstat (limited to 'lru.py')
-rw-r--r--lru.py21
1 files changed, 14 insertions, 7 deletions
diff --git a/lru.py b/lru.py
index 3042bba..53b122a 100644
--- a/lru.py
+++ b/lru.py
@@ -1,15 +1,19 @@
1#!/usr/bin/env python3 1#!/usr/bin/env python3
2 2
3# UPEM / System programming / Project: Memory paging simulator
4# Pacien TRAN-GIRARD, Adam NAILI
5
3from mem import Memory 6from mem import Memory
4from pageexception import * 7from pageexception import *
5 8
9
6class LruPaginator: 10class LruPaginator:
7 def __init__(self, mem): 11 def __init__(self, mem):
8 self.mem = mem 12 self.mem = mem
9 self._global_counter = 1 13 self._global_counter = 1
10 self._check_table = [0 for _ in range(len(mem.main)+len(mem.virt))] 14 self._check_table = [0 for _ in range(len(mem.main) + len(mem.virt))]
11 self._main_cursor = 0 15 self._main_cursor = 0
12 16
13 def _update_check_table(self, page): 17 def _update_check_table(self, page):
14 """ 18 """
15 >>> lru = LruPaginator(Memory(3,5)) 19 >>> lru = LruPaginator(Memory(3,5))
@@ -25,11 +29,11 @@ class LruPaginator:
25 29
26 def _check_where_to_replace(self): 30 def _check_where_to_replace(self):
27 """ 31 """
28 >>> lru = LruPaginator(Memory(3,5)) 32 >>> lru = LruPaginator(Memory(3, 5))
29 >>> lru._check_where_to_replace() 33 >>> lru._check_where_to_replace()
30 >>> print(lru._main_cursor) 34 >>> print(lru._main_cursor)
31 0 35 0
32 >>> lru.mem.main = [3,4,2] 36 >>> lru.mem.main = [3, 4, 2]
33 >>> lru._check_table = [1, 3, 6, 4, 5, 0, 0, 0] 37 >>> lru._check_table = [1, 3, 6, 4, 5, 0, 0, 0]
34 >>> lru._check_where_to_replace() 38 >>> lru._check_where_to_replace()
35 >>> print(lru._main_cursor) 39 >>> print(lru._main_cursor)
@@ -39,8 +43,10 @@ class LruPaginator:
39 if page is None: 43 if page is None:
40 self._main_cursor = self.mem.main.index(page) 44 self._main_cursor = self.mem.main.index(page)
41 return 45 return
46
42 min = self._check_table[self.mem.main[0]] 47 min = self._check_table[self.mem.main[0]]
43 self._main_cursor = 0 48 self._main_cursor = 0
49
44 for page in self.mem.main: 50 for page in self.mem.main:
45 if min > self._check_table[page]: 51 if min > self._check_table[page]:
46 min = self._check_table[page] 52 min = self._check_table[page]
@@ -54,13 +60,14 @@ class LruPaginator:
54 overwritten = self.mem.main[self._main_cursor] 60 overwritten = self.mem.main[self._main_cursor]
55 self.mem.main[self._main_cursor] = page 61 self.mem.main[self._main_cursor] = page
56 self._update_check_table(page) 62 self._update_check_table(page)
57 return overwritten 63 return overwritten
58 64
59 def load(self, page): 65 def load(self, page):
60 if not self.mem.page_in_range(page): raise PageException 66 if not self.mem.page_in_range(page): raise PageException
61 if page in self.mem.main: 67 if page in self.mem.main:
62 self._update_check_table(page) 68 self._update_check_table(page)
63 return 69 return
70
64 if page in self.mem.virt: self.mem.remove_virt(page) 71 if page in self.mem.virt: self.mem.remove_virt(page)
65 overwritten = self._put_main(page) 72 overwritten = self._put_main(page)
66 if overwritten is not None: self.mem.put_virt(overwritten) 73 if overwritten is not None: self.mem.put_virt(overwritten)