From 3dc844c1dfea604bc43890794a5cbc4c5fdef89d Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Mon, 2 Apr 2018 16:14:45 +0200 Subject: PageException created and LRU,Second Chance Handling --- fifo.py | 3 ++- lru.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ myMemory.py | 10 +++++--- pageexception.py | 3 +++ secondchance.py | 34 +++++++++++++++++++++++++++ 5 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 lru.py create mode 100644 pageexception.py create mode 100644 secondchance.py diff --git a/fifo.py b/fifo.py index 61e70f5..c48629f 100644 --- a/fifo.py +++ b/fifo.py @@ -4,6 +4,7 @@ # Pacien TRAN-GIRARD et Adam NAILI from mem import Memory +from pageexception import * class FifoPaginator: @@ -18,7 +19,7 @@ class FifoPaginator: return overwritten def load(self, page): - if not self.mem.page_in_range(page): raise IndexError + if not self.mem.page_in_range(page): raise PageException if page in self.mem.main: return if page in self.mem.virt: self.mem.remove_virt(page) 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 @@ +#!/usr/bin/env python3 + +from mem import Memory +from pageexception import * + +class LruPaginator: + def __init__(self, mem): + self.mem = mem + self._global_counter = 1 + self._check_table = [0 for _ in range(len(mem.main)+len(mem.virt))] + self._main_cursor = 0 + + def _update_check_table(self, page): + """ + >>> lru = LruPaginator(Memory(3,5)) + >>> page = 3 + >>> lru._update_check_table(page) + >>> print(lru._check_table[page]) + 1 + >>> print(lru._global_counter) + 2 + """ + self._check_table[page] = self._global_counter + self._global_counter += 1 + + def _check_where_to_replace(self): + """ + >>> lru = LruPaginator(Memory(3,5)) + >>> lru._check_where_to_replace() + >>> print(lru._main_cursor) + 0 + >>> lru.mem.main = [3,4,2] + >>> lru._check_table = [1, 3, 6, 4, 5, 0, 0, 0] + >>> lru._check_where_to_replace() + >>> print(lru._main_cursor) + 0 + """ + for page in self.mem.main: + if page is None: + self._main_cursor = self.mem.main.index(page) + return + min = self._check_table[self.mem.main[0]] + self._main_cursor = 0 + for page in self.mem.main: + if min > self._check_table[page]: + min = self._check_table[page] + self._main_cursor = self.mem.main.index(page) + + def _put_main(self, page): + self._check_where_to_replace() + print("Cursor:") + print(self._main_cursor) + print("") + overwritten = self.mem.main[self._main_cursor] + self.mem.main[self._main_cursor] = page + self._update_check_table(page) + return overwritten + + def load(self, page): + if not self.mem.page_in_range(page): raise PageException + if page in self.mem.main: + self._update_check_table(page) + return + if page in self.mem.virt: self.mem.remove_virt(page) + overwritten = self._put_main(page) + if overwritten is not None: self.mem.put_virt(overwritten) + + +if __name__ == "__main__": + import doctest + doctest.testmod() diff --git a/myMemory.py b/myMemory.py index 2439aae..6e1014a 100755 --- a/myMemory.py +++ b/myMemory.py @@ -4,10 +4,14 @@ # Pacien TRAN-GIRARD et Adam NAILI from mem import Memory +from pageexception import * from fifo import FifoPaginator - +from lru import LruPaginator +from secondchance import SecondChancePaginator paginators = { - 'fifo': FifoPaginator + 'fifo': FifoPaginator, + 'lru': LruPaginator, + 'secondchance': SecondChancePaginator } @@ -25,7 +29,7 @@ def main(main_size, virt_size, paginator): try: pag.load(int(cmd)) output(cmd, mem.dump_main()) - except IndexError: + except PageException: output(cmd, "Error: invalid page.") elif cmd == 'p': 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 @@ +class PageException(Exception): + def __init__(self): + 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 @@ +#!/usr/bin/env python3 + +# UPEM / Programmation système / Projet : Simulation de gestion de mémoire virtuelle +# Pacien TRAN-GIRARD et Adam NAILI + +from mem import Memory +from pageexception import * + + +class SecondChancePaginator: + def __init__(self, mem): + self.mem = mem + self._check_table = [0 for _ in range(len(mem.main)+len(mem.virt))] + self._main_cursor = 0 + + def _put_main(self, page): + overwritten = self.mem.main[self._main_cursor] + while(overwritten is not None and self._check_table[overwritten] == 1): + self._main_cursor=(self._main_cursor + 1) % len(self.mem.main) + self._check_table[overwritten] = 0 + overwritten = self.mem.main[self._main_cursor] + self.mem.main[self._main_cursor] = page + self._main_cursor = (self._main_cursor + 1) % len(self.mem.main) + return overwritten + + def load(self, page): + if not self.mem.page_in_range(page): raise PageException + if page in self.mem.main: + self._check_table[page] = 1 + return + if page in self.mem.virt: self.mem.remove_virt(page) + overwritten = self._put_main(page) + if overwritten is not None: self.mem.put_virt(overwritten) + -- cgit v1.2.3