aboutsummaryrefslogtreecommitdiff
path: root/myMemory.py
blob: 02f6dbe84d802c8544b4ae332edb11d3dd59116c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3

# UPEM / System programming / Project: Memory paging simulator
# Pacien TRAN-GIRARD, Adam NAILI

from mem import Memory
from pageexception import *
from fifo import FifoPaginator
from lru import LruPaginator
from secondchance import SecondChancePaginator
from randomp import RandomPaginator

paginators = {
  'fifo': FifoPaginator,
  'lru': LruPaginator,
  'secondchance': SecondChancePaginator,
  'random': RandomPaginator
}


def output(cmd, msg):
  print('> \033[1A' + cmd + '\t' + msg)


def main(main_size, virt_size, paginator):
  mem = Memory(main_size, virt_size)
  pag = paginator(mem)

  while True:
    cmd = input('> ')
    if cmd.isnumeric():
      try:
        pag.load(int(cmd))
        output(cmd, mem.dump_main())
      except PageException:
        output(cmd, "Error: invalid page.")
    elif cmd == 'p':
      output(cmd, mem.dump_main() + '\t' + mem.dump_virt())
    elif cmd == 'x':
      break
    else:
      output(cmd, 'Error: invalid command.')


if __name__ == '__main__':
  from argparse import ArgumentParser
  argparser = ArgumentParser()
  argparser.add_argument('-s', '--mem_size', type = int, required = True)
  argparser.add_argument('-v', '--virtual_size', type = int, required = True)
  argparser.add_argument('-a', '--algorithm', required = True, choices = paginators.keys())

  args = argparser.parse_args()
  assert args.mem_size < args.virtual_size
  main(args.mem_size, args.virtual_size, paginators[args.algorithm])