aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpacien2018-03-17 20:38:29 +0100
committerpacien2018-03-17 20:38:29 +0100
commitff949b6802756f5a15cb528208e3cc71d7a7526c (patch)
treee1b65f3fa3e652562e34680852a5b957ec6cb19b
downloadmempager-simulator-ff949b6802756f5a15cb528208e3cc71d7a7526c.tar.gz
Bootstrap project
-rw-r--r--.gitignore3
-rw-r--r--fifo.py30
-rw-r--r--mem.py23
-rwxr-xr-xmyMemory.py48
-rw-r--r--readme.md19
5 files changed, 123 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..27db8fc
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
1__pycache__/
2*.py[cod]
3
diff --git a/fifo.py b/fifo.py
new file mode 100644
index 0000000..7fa55a0
--- /dev/null
+++ b/fifo.py
@@ -0,0 +1,30 @@
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
7
8
9class FifoPaginator:
10 def __init__(self, mem):
11 self.mem = mem
12 self._main_cursor = 0
13 self._virt_cursor = 0
14
15 def _put_main(self, page):
16 overwritten = self.mem.main[self._main_cursor]
17 self.mem.main[self._main_cursor] = page
18 self._main_cursor = (self._main_cursor + 1) % len(self.mem.main)
19 return overwritten
20
21 def _put_virt(self, page):
22 self.mem.virt[self._virt_cursor] = page
23 self._virt_cursor += 1
24
25 def load(self, page):
26 if not self.mem.has_page(page): raise IndexError
27 if page in self.mem.main: return
28 overwritten = self._put_main(page)
29 if overwritten not in self.mem.virt: self._put_virt(overwritten)
30
diff --git a/mem.py b/mem.py
new file mode 100644
index 0000000..7137ede
--- /dev/null
+++ b/mem.py
@@ -0,0 +1,23 @@
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
6
7class Memory:
8 def __init__(self, main_size, virt_size):
9 self.main = [None] * main_size
10 self.virt = [None] * virt_size
11
12 def _mem_to_string(self, mem):
13 return ''.join(['%d' % n if n is not None else 'x' for n in mem])
14
15 def dump_main(self):
16 return self._mem_to_string(self.main)
17
18 def dump_virt(self):
19 return self._mem_to_string(self.virt)
20
21 def has_page(self, page):
22 return 0 <= page < len(self.virt)
23
diff --git a/myMemory.py b/myMemory.py
new file mode 100755
index 0000000..dd2aaa6
--- /dev/null
+++ b/myMemory.py
@@ -0,0 +1,48 @@
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 fifo import FifoPaginator
8
9paginators = {
10 'fifo': FifoPaginator
11}
12
13
14def output(cmd, msg):
15 print('> \033[1A' + cmd + '\t' + msg)
16
17
18def main(main_size, virt_size, paginator):
19 mem = Memory(main_size, virt_size)
20 pag = paginator(mem)
21
22 while True:
23 cmd = input('> ')
24 if cmd.isnumeric():
25 try:
26 pag.load(int(cmd))
27 output(cmd, mem.dump_main())
28 except:
29 output(cmd, "Error: invalid page.")
30 elif cmd == 'p':
31 output(cmd, mem.dump_main() + '\t' + mem.dump_virt())
32 elif cmd == 'x':
33 break
34 else:
35 output(cmd, 'Error: invalid command.')
36
37
38if __name__ == '__main__':
39 from argparse import ArgumentParser
40 argparser = ArgumentParser()
41 argparser.add_argument('-s', '--mem_size', type = int, required = True)
42 argparser.add_argument('-v', '--virtual_size', type = int, required = True)
43 argparser.add_argument('-a', '--algorithm', required = True, choices = paginators.keys())
44
45 args = argparser.parse_args()
46 assert args.mem_size < args.virtual_size
47 main(args.mem_size, args.virtual_size, paginators[args.algorithm])
48
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..468970c
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,19 @@
1# UPEM / Programmation système / Projet : Simulation de gestion de mémoire virtuelle
2
3
4## Usage
5
6* RTFM: `man ./myMemory.1`
7* YOLO: `./myMemory.py -s 3 -v 8 -a fifo`
8
9
10## Authors
11
12* Pacien TRAN-GIRARD
13* Adam NAILI
14
15
16## License
17
18Project distributed under the terms of the Creative Commons BY-NC-SA 3.0 license.
19