Rostrup

Round Robin

Jan 17th, 2022 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.09 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 data in info_list:
  28.             if data.arrt in self.all_p:
  29.                 self.info_list[data.arrt].append(data)
  30.             else:
  31.                 self.info_list[data.arrt] = [data]
  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 tasks(self):
  71.         if self.all_p or not self.wt.empty() or self.active_p != None:
  72.             return True
  73.         return False
  74.  
  75. class Alg:
  76.     schedule = "RR"
  77.     quantum = 3
  78.  
  79.     def round_robin(execution, wt):
  80.         if execution == None:
  81.             schedule.quantum = 3
  82.             if not wt.empty():
  83.                 return wt.get()
  84.         elif not wt.empty():
  85.             if schedule.quantum == 0:
  86.                 schedule.quantum = 3
  87.                 wt.put(execution)
  88.                 return wt.get()
  89.             else:
  90.                 schedule.quantum -= 1
  91.                 return execution
  92.         return execution
  93.  
  94.     def next_process(execution, wt, add_queue):
  95.         if schedule.quantum == "RR":
  96.             return schedule.round_robin(execution, wt)
  97.  
  98. def excell_data():
  99.     workbook = load_workbook(filename="cpu-scheduling.xlsx")
  100.     sheet = workbook.active
  101.     info_list = []
  102.     for cell in sheet.iter_rows(min_row=2, values_only=True):
  103.         file =Info(cell[0], cell[1], cell[2], cell[3])
  104.         info_list.append(file)
  105.     return info_list
  106.  
  107. if __name__ == "__main__":
  108.     info_list = excell_data()
  109.     cpu = Processes(info_list)
  110.     cpu.start()
  111.    
Add Comment
Please, Sign In to add comment