Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. class MemorySimulator:
  2.  
  3.     def __init__(self, M, Mp, S, P, l):
  4.  
  5.         self.M = M
  6.         self.Mp = Mp
  7.         self.S = S
  8.         self.P = P
  9.         self.l = l
  10.  
  11.         self.hit_counter = []
  12.         self.miss_counter = []
  13.         self.frame_counter = 0
  14.         self.entry_mem = M - Mp
  15.         self.memory_list = []
  16.         self.clock_list = []
  17.  
  18.         for p in range(len(self.l)):
  19.             tup = [(None, p)]
  20.             self.frame_counter += self.l[p]
  21.             self.memory_list.extend(tup * self.l[p])
  22.             self.clock_list.append((0, True, p))
  23.             self.clock_list.extend([(0, False, p)] * (self.l[p] - 1))
  24.             self.miss_counter.append(0)
  25.             self.hit_counter.append(0)
  26.  
  27.     def handle_request(self, addr, i):
  28.  
  29.         whereis_true = 0
  30.         whereis_start = 0
  31.         whereis_end = 0
  32.         found_start = False
  33.         found_end = False
  34.  
  35. #  ALGORITMO DELL'OROLOGIO
  36.         for im in range(len(self.clock_list)):
  37.  
  38.             if self.clock_list[im][2] == i and found_end is False:
  39.  
  40.                 if found_start is False:
  41.  
  42.                     found_start = True
  43.                     whereis_start = im
  44.  
  45.                 if self.clock_list[im][1] is True:
  46.  
  47.                     whereis_true = im
  48.  
  49.                 if found_end is False and im == (len(self.clock_list) - 1):
  50.  
  51.                     whereis_end = im
  52.                     found_end = True
  53.                     break
  54.  
  55.             elif (self.clock_list[im][2] != i) and (found_start is True):
  56.  
  57.                 whereis_end = im - 1
  58.                 break
  59.  
  60.         page_addr = int(addr / self.P)
  61.  
  62.         for proc in range(whereis_start, whereis_end + 1):
  63.  
  64.             if self.memory_list[proc][0] == page_addr:
  65.                 self.memory_list[proc] == (1, self.clock_list[proc][1], i)
  66.                 ind_fis = (addr % self.P) + (self.entry_mem + (self.P *
  67.                                                                proc))
  68.                 self.hit_counter[i] += 1
  69.                 return(proc, ind_fis)
  70.  
  71.         a = whereis_true
  72.         b = whereis_end
  73.  
  74.         while True:
  75.  
  76.                 if self.clock_list[a][0] == 0:
  77.                     #  sostituzione
  78.                     self.miss_counter[i] += 1
  79.                     self.memory_list[a] = (page_addr, i)
  80.                     self.clock_list[a] = (1, False, i)
  81.                     ind_fis = (addr % self.P) + (self.entry_mem + (self.P *
  82.                                                                    proc))
  83.                     if a == b:
  84.  
  85.                         self.clock_list[whereis_start] = (self.clock_list[whereis_start][0], True, i)
  86.  
  87.                     else:
  88.  
  89.                         self.clock_list[a + 1] = (self.clock_list[a + 1][0], True, i)
  90.  
  91.                     return(a, ind_fis)
  92.  
  93.                 else:
  94.                     self.clock_list[a] = (0, False, i)
  95.  
  96.                     if a == b:
  97.  
  98.                         a = whereis_start
  99.  
  100.                     else:
  101.  
  102.                         a += 1
  103.  
  104.     def get_memory(self):
  105.  
  106.         return self.memory_list
  107.  
  108.     def get_stats(self):
  109.  
  110.         return(self.hit_counter, self.miss_counter)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement