def ask_for_resource(resource, process, res_class): ''' 1. process is blocked and added to resources awaiting processes list 2. resource planner is called ''' if resource is not None: logger.log("Process: " + process.name + " asked for resource " + resource.name) resource.awaiting_processes.add(process) process.awaiting_for_creation = None resource.distribute() else: # there is no such resource created logger.log("Process: " + process.name + " asked for resource that is not yet created") kernel_data.ACTIVE_PROCESS.state = BLOCKED_STATE logger.log(process.name + " now waits for resource " + str(res_class) +" to be created") process.awaiting_for_creation = res_class kernel.schedule()
def distribute(self): ''' Algorithm that distributes resource. (Resurso planuotojas) ''' logger.log("DISTRIBUTOR called on: " + self.name) if self.free(): logger.log("GOT TRUE") pr = self.awaiting_processes.get() # if there are processes waiting for this resource # give resource to them if pr is not None: pr.used_resources.add(self) pr.make_ready() self.awaiting_processes.remove(pr) # lock_with_semaphore when asking_for_resoure # if resource is guarded with semaphore create lock if hasattr(self, 'sem'): self.sem.v() logger.log("LOCKED SEMAPHORE on " + self.name) else: kernel_data.ACTIVE_PROCESS.state = BLOCKED_STATE self.awaiting_processes.add(kernel_data.ACTIVE_PROCESS) logger.log("GOT FALSE") kernel.schedule()