aboutsummaryrefslogtreecommitdiff
path: root/randomp.py
blob: 48e9abfd0d50c8b279356d2c539758331e9c7fff (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
#!/usr/bin/env python3

# UPEM / Programmation système / Projet : Simulation de gestion de mémoire virtuelle
# Pacien TRAN-GIRARD et Adam NAILI

from random import randint
from mem import Memory
from pageexception import PageException


class RandomPaginator:
  def __init__(self, mem):
    self.mem = mem
    self._main_occupancy = 0

  def _random_page(self):
    if self._main_occupancy < len(self.mem.main):
      return self._main_occupancy
    else:
      return randint(0, len(self.mem.main) - 1)

  def _put_main(self, page):
    index = self._random_page()
    overwritten = self.mem.main[index]
    self.mem.main[index] = page
    self._main_occupancy += 1
    return overwritten

  def load(self, page):
    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)
    if overwritten is not None: self.mem.put_virt(overwritten)