Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import queue
- from openpyxl import load_workbook
- class Info:
- pid = 0
- arrt = 0
- burst = 0
- pri = 0
- def __init__(self, pid, arrt, burst, pri=1):
- self.pid = pid
- self.arrt = arrt
- self.burst = burst
- self.pri = pri
- def run(self):
- self.burst -= 1
- return self.burst
- class Processes:
- tu = 0
- wt = None
- cp = None
- all_p = {}
- def __init__(self, info_list):
- for i in info_list:
- if i.arrt in self.all_p:
- self.all_p[i.arrt].append(i)
- else:
- self.all_p[i.arrt] = [i]
- self.wt = queue.SimpleQueue()
- def add_queue(self):
- if Processes.tu in self.all_p:
- add = self.all_p.pop(self.tu)
- for data in add:
- self.wt.put(data)
- return True
- return False
- def execution(self):
- if self.active_p:
- time_left = self.active_p.execute()
- print(f"Time Unit {Processes.tu}: PID {self.active_p.pid} executes. {time_left} Instructions left. Queue = {self.wt.qsize()}.")
- if time_left == 0:
- self.active_p = None
- else:
- print(f"Time Unit {Processes.tu}: CPU idle.")
- def start(self):
- for arrt in self.all_p:
- process_data = self.all_p[arrt]
- while True:
- add_queue = self.add_queue()
- next = Alg.next_process(self.active_p, self.wt, add_queue)
- execute = True
- if Alg.scheduele == "RR" and next != self.active_p:
- print(f"Time Unit {Processes.tu}: Context switch.")
- execute = False
- self.active_p = next
- if execute:
- self.execution()
- if self.task() == False:
- break
- Processes.tu += 1
- def task(self):
- if self.all_p or not self.task.empty() or self.active_p != None:
- return True
- return False
- class Alg:
- schedule = "PRI"
- def pri(execution, wt):
- if execution:
- return execution
- elif not wt.empty():
- t_queue = []
- high_pri = None
- index = None
- for i in range(wt.qsize()):
- data = wt.get()
- if high_pri == None or data.pri < high_pri:
- high_pri = data.pri
- index = i
- t_queue.append(data)
- execution = t_queue[index]
- t_queue.pop(index)
- for data in t_queue:
- wt.put(data)
- return execution
- def next_task(execution, wt, add_queue):
- if Alg.pri == "PRI":
- return Alg.pri(execution, wt)
- def excell_data():
- workbook = load_workbook(filename="cpu-scheduling.xlsx")
- sheet = workbook.active
- info_list = []
- for cell in sheet.iter_rows(min_row=2, values_only=True):
- file =Info(cell[0], cell[1], cell[2], cell[3])
- info_list.append(file)
- return info_list
- if __name__ == "__main__":
- info_list = excell_data()
- cpu = Processes(info_list)
- cpu.start()
Add Comment
Please, Sign In to add comment