Rostrup

Priority

Jan 17th, 2022 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.17 KB | None | 0 0
  1. import queue
  2. from openpyxl import load_workbook
  3.  
  4. class Info:
  5.     pid = 0
  6.     arrt = 0
  7.     burst = 0
  8.     pri = 0
  9.  
  10.     def __init__(self, pid, arrt, burst, pri=1):
  11.         self.pid = pid
  12.         self.arrt = arrt
  13.         self.burst = burst
  14.         self.pri = pri
  15.  
  16.     def run(self):
  17.         self.burst -= 1
  18.         return self.burst
  19.  
  20. class Processes:
  21.     tu = 0
  22.     wt = None
  23.     cp = None
  24.     all_p = {}
  25.  
  26.     def __init__(self, info_list):
  27.         for i in info_list:
  28.             if i.arrt in self.all_p:
  29.                 self.all_p[i.arrt].append(i)
  30.             else:
  31.                 self.all_p[i.arrt] = [i]
  32.         self.wt = queue.SimpleQueue()
  33.  
  34.     def add_queue(self):
  35.         if Processes.tu in self.all_p:
  36.             add = self.all_p.pop(self.tu)
  37.             for data in add:
  38.                 self.wt.put(data)
  39.             return True
  40.         return False
  41.  
  42.     def execution(self):
  43.         if self.active_p:
  44.             time_left = self.active_p.execute()
  45.             print(f"Time Unit {Processes.tu}: PID {self.active_p.pid} executes. {time_left} Instructions left. Queue = {self.wt.qsize()}.")
  46.             if time_left == 0:
  47.                 self.active_p = None
  48.             else:
  49.                 print(f"Time Unit {Processes.tu}: CPU idle.")
  50.  
  51.     def start(self):
  52.         for arrt in self.all_p:
  53.             process_data = self.all_p[arrt]
  54.  
  55.         while True:
  56.             add_queue = self.add_queue()
  57.             next = Alg.next_process(self.active_p, self.wt, add_queue)
  58.             execute = True
  59.             if Alg.scheduele == "RR" and next != self.active_p:
  60.                 print(f"Time Unit {Processes.tu}: Context switch.")
  61.                 execute = False
  62.             self.active_p = next
  63.             if execute:
  64.                 self.execution()
  65.  
  66.             if self.task() == False:
  67.                 break
  68.             Processes.tu += 1
  69.  
  70.     def task(self):
  71.         if self.all_p or not self.task.empty() or self.active_p != None:
  72.             return True
  73.         return False
  74.  
  75. class Alg:
  76.     schedule = "PRI"
  77.  
  78.     def pri(execution, wt):
  79.         if execution:
  80.             return execution
  81.         elif not wt.empty():
  82.             t_queue = []
  83.             high_pri = None
  84.             index = None
  85.             for i in range(wt.qsize()):
  86.                 data = wt.get()
  87.                 if high_pri == None or data.pri < high_pri:
  88.                     high_pri = data.pri
  89.                     index = i
  90.                     t_queue.append(data)
  91.  
  92.             execution = t_queue[index]
  93.             t_queue.pop(index)
  94.  
  95.             for data in t_queue:
  96.                 wt.put(data)
  97.             return execution
  98.  
  99.     def next_task(execution, wt, add_queue):
  100.         if Alg.pri == "PRI":
  101.             return Alg.pri(execution, wt)
  102.  
  103. def excell_data():
  104.     workbook = load_workbook(filename="cpu-scheduling.xlsx")
  105.     sheet = workbook.active
  106.     info_list = []
  107.     for cell in sheet.iter_rows(min_row=2, values_only=True):
  108.         file =Info(cell[0], cell[1], cell[2], cell[3])
  109.         info_list.append(file)
  110.     return info_list
  111.  
  112. if __name__ == "__main__":
  113.     info_list = excell_data()
  114.     cpu = Processes(info_list)
  115.     cpu.start()
  116.  
Add Comment
Please, Sign In to add comment