class Cpu(object): def __init__(self): self.pcb = None self.__mutex = RLock() self.__pcb_not_set = Condition(self.__mutex) self.__mem_not_allocated = Condition(self.__mutex) self.__round_robin_policy_on = False def enable_round_robin(self, round_robin_quantum): self.__round_robin_policy_on = True self.__round_robin = RoundRobin(round_robin_quantum) def pcb_not_set(self): return self.__pcb_not_set def set_kernel(self, kernel): self.__kernel = kernel def is_pcb_set(self): return self.pcb != None def set_current_pcb(self, pcb): with self.__pcb_not_set: self.pcb = pcb self.__pcb_not_set.notify() def reset_pcb(self): self.pcb = None def get_current_pcb(self): return self.pcb def __get_mem_manager(self): return self.__kernel.get_mem_manager() def __get_irq_manager(self): return self.__kernel.get_irq_manager() def fetch_decode_and_execute(self): with self.__pcb_not_set: while(not self.is_pcb_set()): self.__pcb_not_set.wait() with self.__mutex: self.__fetch() self.__decode() self.__execute() def __fetch(self): pcb = self.get_current_pcb() address = self.__get_mem_manager().current_instruction_address(pcb) with self.__mem_not_allocated: while self.__get_mem_manager().get(pcb,address) == None: self.__mem_not_allocated.wait() self.__current_instruction = self.__get_mem_manager().get(pcb, address ) def __decode(self): self.__send_interruption_if_is_io() self.__send_interruption_if_is_kill() def __send_interruption_if_is_kill(self): if(self.__current_instruction.is_kill_instruction()): self.send_end() def __send_interruption_if_is_io(self): if(self.__current_instruction.is_io_instruction()): self.send_io() def __execute(self): self.__execute_if_is_cpu_instruction() def __execute_if_is_cpu_instruction(self): if (self.__current_instruction.is_cpu_instruction()): self.__current_instruction.run() self.get_current_pcb().increment_pc() def send_interruption(self, a_interruption): self.__get_irq_manager().handle(Irq(a_interruption, self.get_current_pcb())) def send_timeout(self): self.send_interruption(TIMEOUT_INTERRUPT) def send_end(self): self.send_interruption(KILL_INTERRUPT) def send_io(self): self.send_interruption(IO_INTERRUPT) def on_signal(self): if self.__round_robin_policy_on: self.__round_robin.handle_action(self) else: self.fetch_decode_and_execute()
def enable_round_robin(self, round_robin_quantum): self.__round_robin_policy_on = True self.__round_robin = RoundRobin(round_robin_quantum)