aboutsummaryrefslogtreecommitdiff
path: root/lru.py
diff options
context:
space:
mode:
authorAdam NAILI2018-04-02 16:14:45 +0200
committerAdam NAILI2018-04-02 16:14:45 +0200
commit3dc844c1dfea604bc43890794a5cbc4c5fdef89d (patch)
tree3599db4a41125f3934d2d9ebc7aa6eaed2a9aaab /lru.py
parent0441d28435e8db1d60c1ae30c81e3a54f12149c8 (diff)
downloadmempager-simulator-3dc844c1dfea604bc43890794a5cbc4c5fdef89d.tar.gz
PageException created and LRU,Second Chance Handling
Diffstat (limited to 'lru.py')
-rw-r--r--lru.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/lru.py b/lru.py
new file mode 100644
index 0000000..3042bba
--- /dev/null
+++ b/lru.py
@@ -0,0 +1,71 @@
1#!/usr/bin/env python3
2
3from mem import Memory
4from pageexception import *
5
6class LruPaginator:
7 def __init__(self, mem):
8 self.mem = mem
9 self._global_counter = 1
10 self._check_table = [0 for _ in range(len(mem.main)+len(mem.virt))]
11 self._main_cursor = 0
12
13 def _update_check_table(self, page):
14 """
15 >>> lru = LruPaginator(Memory(3,5))
16 >>> page = 3
17 >>> lru._update_check_table(page)
18 >>> print(lru._check_table[page])
19 1
20 >>> print(lru._global_counter)
21 2
22 """
23 self._check_table[page] = self._global_counter
24 self._global_counter += 1
25
26 def _check_where_to_replace(self):
27 """
28 >>> lru = LruPaginator(Memory(3,5))
29 >>> lru._check_where_to_replace()
30 >>> print(lru._main_cursor)
31 0
32 >>> lru.mem.main = [3,4,2]
33 >>> lru._check_table = [1, 3, 6, 4, 5, 0, 0, 0]
34 >>> lru._check_where_to_replace()
35 >>> print(lru._main_cursor)
36 0
37 """
38 for page in self.mem.main:
39 if page is None:
40 self._main_cursor = self.mem.main.index(page)
41 return
42 min = self._check_table[self.mem.main[0]]
43 self._main_cursor = 0
44 for page in self.mem.main:
45 if min > self._check_table[page]:
46 min = self._check_table[page]
47 self._main_cursor = self.mem.main.index(page)
48
49 def _put_main(self, page):
50 self._check_where_to_replace()
51 print("Cursor:")
52 print(self._main_cursor)
53 print("")
54 overwritten = self.mem.main[self._main_cursor]
55 self.mem.main[self._main_cursor] = page
56 self._update_check_table(page)
57 return overwritten
58
59 def load(self, page):
60 if not self.mem.page_in_range(page): raise PageException
61 if page in self.mem.main:
62 self._update_check_table(page)
63 return
64 if page in self.mem.virt: self.mem.remove_virt(page)
65 overwritten = self._put_main(page)
66 if overwritten is not None: self.mem.put_virt(overwritten)
67
68
69if __name__ == "__main__":
70 import doctest
71 doctest.testmod()