def awake_process(self, pid): with open("suspend_list.csv", "r") as fr: titles = fr.readline() titles = titles.split(",") titles[-1] = titles[-1][:-1] pid_f = titles.index("pid") priority = titles.index("priority") event_name = titles.index("event_name") happen_time = titles.index("happen_time") running_time = titles.index("running_time") mark = False for line in fr.readlines(): line = line.split(",") if pid == int(line[pid_f]): self.create_process( Job(event_name=line[event_name], happen_time=YidanTime(line[happen_time]), running_time=YidanTime(line[running_time]), priority=int(line[priority]))) mark = True fr.close() if mark: delete_a_record(pid, "suspend_list.csv") return True else: return False
def run_process(self): "Excute the current process in the cpu" if self.running_process == None: a = self.dispatch_process() if a == False: return # Check the required event is still happending? if not self.check_event_happending(self.running_process): self.waitEvent_process() a = self.dispatch_process() if a == False: return print("{}, {}".format(self.running_process.pcb.pid, self.running_process.pcb.running_time)) self.running_process.pcb.running_time -= 1 if self.running_process.pcb.running_time == YidanTime(0): self.exit_process(self.running_process) self.running_process = None self.dispatch_process() # Time Period minus 1 self.time_period -= 1 if self.time_period == YidanTime(0): self.timeUp_process()
def __init__(self, memory_size=1024 * 8): # ************************************************************* # Memory Control Part # Initialize the memory of the System # Partner System for memory managing self.empty_memory = [ Memory(lower_bound=0, upper_bound=memory_size - 1) ] self.used_memory = [] self.space_enough = True self.threshold = 16 # Arranging a max size of the pcb, imitating the real os self.pcb_max_size = 128 # ************************************************************* # The ID of the process allocated for the process self.pid = 0 # ************************************************************* # ALGORITHM AREA self.apply_memory_algorithm = algorithm_memory_apply_best_adapt self.recycle_memory_algo = algorithm_memory_recycle # ************************************************************* # EVENT CONTROL AREA self.clock = YidanTime(0) self.time_period = YidanTime(0) self.events = [ Event(name="event1", semaphore=2), Event(name="event2", semaphore=1), Event(name="event3", semaphore=2), Event(name="event4", semaphore=1), Event(name="event5", semaphore=2) ] self.current_event = [] self.event_sustain = YidanTime(0) self.menu_show = True # ************************************************************* # LIST MANAGE PART self.blocked_list = {} for event in self.events: self.blocked_list[event.name] = [] self.ready_list = {} for event in self.events: self.ready_list[event.name] = [] self.running_process = None
def ask_job_info(self): # Generate a Job by asking the user print("Please Input the event that job required") eventNum = input() print("Please Input the job happen time") happenTime = YidanTime(input()) print("Please Input the time that the job need to run") runningTime = YidanTime(input()) print("Please Input the priority of the job") priority = int(input()) return Job(eventNum, happenTime, runningTime, priority)
def run(self): self.events = [ Event(name="event1", semaphore=2), Event(name="event2", semaphore=1), Event(name="event3", semaphore=2), ] self.current_event = [ Event(name="event4", semaphore=1), Event(name="event5", semaphore=2) ] self.show_list() # self.load_job_info(filename = "data.csv") while (1): if self.menu_show: self.menu() self.clock += 1 if self.clock == YidanTime("24:00"): break if self.event_sustain == YidanTime(0): # self.event_happen() self.show_event() self.event_sustain = self.event_sustain - 1 self.block_ready_switch() self.show_list() self.run_process() else: self.event_sustain = self.event_sustain - 1 self.run_process() if self.space_enough == False: suspend_processes = [] for i in self.blocked_list: suspend_processes.append( random.choice(self.blocked_list[i])) for process in suspend_process: self.suspend_process(process) self.space_enough = True else: with open("suspend_list.csv", "r") as fr: lines = fr.readlines() if len(lines) > 1: titles = lines[0].split(",") self.awake_process(pid=lines[1][titles.index("pid")])
def __init__(self, event_name, happen_time, running_time, priority): happen_time = YidanTime(happen_time) running_time = YidanTime(running_time) priority = int(priority) assert type(event_name) is str, "job parameter type error!" assert type(happen_time) is YidanTime, "job parameter type error!" assert type(running_time) is YidanTime, "job parameter type error!" assert type(priority) is int, "job parameter type error!" self.event_name = event_name self.happen_time = happen_time self.running_time = running_time self.priority = priority
def calculate(self): # This method is for calculating the job requirement # Channel is the channel amount offered to this algorithm # Algorithm is the algorithm used in this computation # What will be calculated and return? # *** Beginning time: Beginning time of each job # *** End Time: End Time of each Job # *** Cycling Time # *** Weighted Cycling Time # *** Average Cycling Time # *** Average Weighted Cycling Time. aCyT = YidanTime(0) aWCT = 0 for job in self.excuted_job: job.wCyclingTime = job.cyclingTime / job.runningTime aCyT += job.cyclingTime aWCT += job.wCyclingTime print("Job: {} Cycling Time: {:>5}, Weighted Cycling Time: {:>5}". format(job.sequence, job.cyclingTime, job.wCyclingTime)) print("****************************************") print("Average Cycling Time:{:>5}".format(aCyT / 5)) print("Average Weighted Cycling Time:{:>5}".format(aWCT / 5)) return None
def event_happen(self): self.events = self.events + self.current_event self.current_event = [] event_num = random.randint(0, len(self.events)) for i in range(event_num): self.current_event.append( self.events.pop(random.randint(0, len(self.events) - 1))) self.event_sustain = YidanTime(random.randint(1, 5))
def __init__(self, sequence, enterTime, runningTime, priority, waitingTime): self.sequence = sequence self.enterTime = YidanTime(enterTime) self.runningTime = YidanTime(runningTime) self.priority = priority self.endTime = YidanTime(0) self.waitingTime = YidanTime(waitingTime) self.inChannelTime = YidanTime(0) self.cyclingTime = YidanTime(0) self.runnedTime = YidanTime(0) self.wCyclingTime = YidanTime(0)
def run(self): self.event_happen() self.show_list() # self.load_job_info(filename = "data.csv") while (1): self.clock += 1 if self.menu_show: self.menu() if self.clock == YidanTime("24:00"): break if self.event_sustain == YidanTime(0): self.event_happen() self.show_event() self.event_sustain = self.event_sustain - 1 self.add_block_to_ready() self.show_list() self.run_process() else: self.event_sustain = self.event_sustain - 1 self.run_process() if self.space_enough == False: suspend_processes = [] for i in self.blocked_list: suspend_processes.append( random.choice(self.blocked_list[i])) for process in suspend_process: self.suspend_process(process) self.space_enough = True
def run(self): # Imitate the actual system running in this function assert self.channel == 1 or ( self.channel != 1 and self.priority == 1 ), "while channel is one, you need offered the priority of the job" self.execute_list = self.job_list.copy() while (1): # Print out the current time self.happen_job_check() job = self.cm.excuteAllChannels(self.clock) if type(job) == Job: self.excuted_job.append(job) if self.clock.minute % 10 == 0: print("\n******************************") print("Current Time ", self.clock) print("******************************\n") while (1): result = self.cm.isChannelEmpty(self.clock) if result == False: break else: if self.isOtherJob(): self.cm.addJob(self.happened_job[0], self.clock) self.happened_job = self.happened_job[1:] else: break self.clock = self.clock + 1 if self.clock == YidanTime("14:00"): print("\n******************************") print("Current Time ", self.clock) print("******************************\n") print( "====================> Time Over <====================") break
def __init__(self, algorithm=algorithm_rrh, file=None): print("Does Priority Apply for Your Job List? 1 for Yes, 0 for No") self.priority = int(input()) self.job_list = self.initialize(file) print("Input the channel of the system") self.channel = int(input()) self.clock = YidanTime(0) self.cm = channel_manager(self.channel) self.algorithm = algorithm self.excuted_job = [] self.happened_job = []
def awake_process(self, pid): print("Here is the awaking process") with open("suspend_list.csv", "r") as fr: print("openfile") titles = fr.readline() titles = titles.split(",") titles[-1] = titles[-1][:-1] pid_f = titles.index("pid") priority = titles.index("priority") event_name = titles.index("event_name") happen_time = titles.index("happen_time") running_time = titles.index("running_time") mark = False print("find pid") for line in fr.readlines(): line = line.split(",") if pid == int(line[pid_f]): print("runningTime", line[running_time]) self.create_process( Job(event_name=line[event_name], happen_time=YidanTime(line[happen_time]), running_time=line[running_time], priority=int(line[priority]))) mark = True print("before close") print("before close") # fr.close() print("Here is the awaking process after close") if mark: print("Found the process") delete_a_record(pid, "suspend_list.csv") return True else: print("Havnt found the process") return False
def __init__(self): self.currentJobTime = 0 self.job = None self.clock = YidanTime(0)