aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam NAILI2018-04-02 16:14:45 +0200
committerAdam NAILI2018-04-02 16:14:45 +0200
commit3dc844c1dfea604bc43890794a5cbc4c5fdef89d (patch)
tree3599db4a41125f3934d2d9ebc7aa6eaed2a9aaab
parent0441d28435e8db1d60c1ae30c81e3a54f12149c8 (diff)
downloadmempager-simulator-3dc844c1dfea604bc43890794a5cbc4c5fdef89d.tar.gz
PageException created and LRU,Second Chance Handling
-rw-r--r--fifo.py3
-rw-r--r--lru.py71
-rwxr-xr-xmyMemory.py10
-rw-r--r--pageexception.py3
-rw-r--r--secondchance.py34
5 files changed, 117 insertions, 4 deletions
diff --git a/fifo.py b/fifo.py
index 61e70f5..c48629f 100644
--- a/fifo.py
+++ b/fifo.py
@@ -4,6 +4,7 @@
4# Pacien TRAN-GIRARD et Adam NAILI 4# Pacien TRAN-GIRARD et Adam NAILI
5 5
6from mem import Memory 6from mem import Memory
7from pageexception import *
7 8
8 9
9class FifoPaginator: 10class FifoPaginator:
@@ -18,7 +19,7 @@ class FifoPaginator:
18 return overwritten 19 return overwritten
19 20
20 def load(self, page): 21 def load(self, page):
21 if not self.mem.page_in_range(page): raise IndexError 22 if not self.mem.page_in_range(page): raise PageException
22 if page in self.mem.main: return 23 if page in self.mem.main: return
23 if page in self.mem.virt: self.mem.remove_virt(page) 24 if page in self.mem.virt: self.mem.remove_virt(page)
24 overwritten = self._put_main(page) 25 overwritten = self._put_main(page)
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()
diff --git a/myMemory.py b/myMemory.py
index 2439aae..6e1014a 100755
--- a/myMemory.py
+++ b/myMemory.py
@@ -4,10 +4,14 @@
4# Pacien TRAN-GIRARD et Adam NAILI 4# Pacien TRAN-GIRARD et Adam NAILI
5 5
6from mem import Memory 6from mem import Memory
7from pageexception import *
7from fifo import FifoPaginator 8from fifo import FifoPaginator
8 9from lru import LruPaginator
10from secondchance import SecondChancePaginator
9paginators = { 11paginators = {
10 'fifo': FifoPaginator 12 'fifo': FifoPaginator,
13 'lru': LruPaginator,
14 'secondchance': SecondChancePaginator
11} 15}
12 16
13 17
@@ -25,7 +29,7 @@ def main(main_size, virt_size, paginator):
25 try: 29 try:
26 pag.load(int(cmd)) 30 pag.load(int(cmd))
27 output(cmd, mem.dump_main()) 31 output(cmd, mem.dump_main())
28 except IndexError: 32 except PageException:
29 output(cmd, "Error: invalid page.") 33 output(cmd, "Error: invalid page.")
30 elif cmd == 'p': 34 elif cmd == 'p':
31 output(cmd, mem.dump_main() + '\t' + mem.dump_virt()) 35 output(cmd, mem.dump_main() + '\t' + mem.dump_virt())
diff --git a/pageexception.py b/pageexception.py
new file mode 100644
index 0000000..9a1519d
--- /dev/null
+++ b/pageexception.py
@@ -0,0 +1,3 @@
1class PageException(Exception):
2 def __init__(self):
3 super().__init__()
diff --git a/secondchance.py b/secondchance.py
new file mode 100644
index 0000000..b0fb1f9
--- /dev/null
+++ b/secondchance.py
@@ -0,0 +1,34 @@
1#!/usr/bin/env python3
2
3# UPEM / Programmation système / Projet : Simulation de gestion de mémoire virtuelle
4# Pacien TRAN-GIRARD et Adam NAILI
5
6from mem import Memory
7from pageexception import *
8
9
10class SecondChancePaginator:
11 def __init__(self, mem):
12 self.mem = mem
13 self._check_table = [0 for _ in range(len(mem.main)+len(mem.virt))]
14 self._main_cursor = 0
15
16 def _put_main(self, page):
17 overwritten = self.mem.main[self._main_cursor]
18 while(overwritten is not None and self._check_table[overwritten] == 1):
19 self._main_cursor=(self._main_cursor + 1) % len(self.mem.main)
20 self._check_table[overwritten] = 0
21 overwritten = self.mem.main[self._main_cursor]
22 self.mem.main[self._main_cursor] = page
23 self._main_cursor = (self._main_cursor + 1) % len(self.mem.main)
24 return overwritten
25
26 def load(self, page):
27 if not self.mem.page_in_range(page): raise PageException
28 if page in self.mem.main:
29 self._check_table[page] = 1
30 return
31 if page in self.mem.virt: self.mem.remove_virt(page)
32 overwritten = self._put_main(page)
33 if overwritten is not None: self.mem.put_virt(overwritten)
34