Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MemorySimulator:
- def __init__(self, M, Mp, S, P, l):
- self.M = M
- self.Mp = Mp
- self.S = S
- self.P = P
- self.l = l
- self.hit_counter = []
- self.miss_counter = []
- self.frame_counter = 0
- self.entry_mem = M - Mp
- self.memory_list = []
- self.clock_list = []
- for p in range(len(self.l)):
- tup = [(None, p)]
- self.frame_counter += self.l[p]
- self.memory_list.extend(tup * self.l[p])
- self.clock_list.append((0, True, p))
- self.clock_list.extend([(0, False, p)] * (self.l[p] - 1))
- self.miss_counter.append(0)
- self.hit_counter.append(0)
- def handle_request(self, addr, i):
- whereis_true = 0
- whereis_start = 0
- whereis_end = 0
- found_start = False
- found_end = False
- # ALGORITMO DELL'OROLOGIO
- for im in range(len(self.clock_list)):
- if self.clock_list[im][2] == i and found_end is False:
- if found_start is False:
- found_start = True
- whereis_start = im
- if self.clock_list[im][1] is True:
- whereis_true = im
- if found_end is False and im == (len(self.clock_list) - 1):
- whereis_end = im
- found_end = True
- break
- elif (self.clock_list[im][2] != i) and (found_start is True):
- whereis_end = im - 1
- break
- page_addr = int(addr / self.P)
- for proc in range(whereis_start, whereis_end + 1):
- if self.memory_list[proc][0] == page_addr:
- self.memory_list[proc] == (1, self.clock_list[proc][1], i)
- ind_fis = (addr % self.P) + (self.entry_mem + (self.P *
- proc))
- self.hit_counter[i] += 1
- return(proc, ind_fis)
- a = whereis_true
- b = whereis_end
- while True:
- if self.clock_list[a][0] == 0:
- # sostituzione
- self.miss_counter[i] += 1
- self.memory_list[a] = (page_addr, i)
- self.clock_list[a] = (1, False, i)
- ind_fis = (addr % self.P) + (self.entry_mem + (self.P *
- proc))
- if a == b:
- self.clock_list[whereis_start] = (self.clock_list[whereis_start][0], True, i)
- else:
- self.clock_list[a + 1] = (self.clock_list[a + 1][0], True, i)
- return(a, ind_fis)
- else:
- self.clock_list[a] = (0, False, i)
- if a == b:
- a = whereis_start
- else:
- a += 1
- def get_memory(self):
- return self.memory_list
- def get_stats(self):
- return(self.hit_counter, self.miss_counter)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement