Advertisement
FiddleComputers

Untitled

Sep 24th, 2022
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 KB | None | 0 0
  1. class Process(object):
  2.     """docstring for Process"""
  3.     def __init__(self, name, exec_time, exec_order, priority, pages):
  4.         super(Process, self).__init__()
  5.         self.name = name
  6.         self.exec_time = exec_time
  7.         self.exec_order = exec_order
  8.         self.priority = priority
  9.         self.pages = pages
  10.         self.finished = False
  11.  
  12.     def __str__(self):
  13.         return self.name
  14.  
  15. def isProcessDone(processes):
  16.     for p in processes:
  17.         if not p.finished:
  18.             return False
  19.     return True
  20.  
  21. def seconde_chance(process, pagesHistory, oldestPageIndex, clock):
  22.     pagesToAdd = process.pages
  23.     pagingErrors = 0
  24.     for page in pagesToAdd:
  25.         if len(pagesHistory) == 0:
  26.             pagesHistory.append([page])
  27.             pagingErrors += 1
  28.             clock[0]['page'] = page
  29.         elif len(pagesHistory[-1]) == 1:
  30.             if not page in pagesHistory[-1]:
  31.                 pagesHistory.append([pagesHistory[0][0], page])
  32.                 pagingErrors += 1
  33.                 clock[1]['page'] = page
  34.             else:
  35.                 pagesHistory.append(pagesHistory[-1])
  36.         elif len(pagesHistory[-1]) == 2:
  37.             if not page in pagesHistory[-1]:
  38.                 pagesHistory.append([pagesHistory[0][0], pagesHistory[1][1], page])
  39.                 pagingErrors += 1
  40.                 clock[2]['page'] = page
  41.             else:
  42.                 pagesHistory.append(pagesHistory[-1])
  43.         else:
  44.             if not page in pagesHistory[-1]:
  45.                 while clock[oldestPageIndex]['bits'] == 1:
  46.                     clock[oldestPageIndex]['bits'] = 0
  47.                     oldestPageIndex += 1
  48.                     oldestPageIndex %= 3
  49.  
  50.                 clock[oldestPageIndex]['page'] = page
  51.                 clock[oldestPageIndex]['bits'] = 1
  52.                 pagingErrors += 1
  53.  
  54.             else:
  55.                 pagesHistory.append(pagesHistory[-1])
  56.  
  57.         process.priority += pagingErrors
  58.  
  59.     return pagesHistory, oldestPageIndex, clock
  60.  
  61. def execution_queue(processes, quantum, currentExecQueue, execQueueHistory, pagesHistory, oldestPageIndex, clock):
  62.     if currentExecQueue == [[]] * 10:
  63.         for p in processes:
  64.             currentExecQueue[p.priority].append(p)
  65.     else:
  66.         for case in currentExecQueue:
  67.             if not case == []:
  68.                 for i in range(quantum):
  69.                     if case[0].exec_time - 1 >= 0:
  70.                         case[0].exec_time -= 1
  71.                         execQueueHistory.append(case[0].name)
  72.                     else:
  73.                         execQueueHistory.append("X")
  74.  
  75.                 if case[0].exec_time == 0:
  76.                     case[0].finished = True
  77.                     print(case[0].name + " is finished!")
  78.                 else:
  79.                     seconde_chance(case[0], pagesHistory, oldestPageIndex, clock)
  80.                     case[0].priority += quantum
  81.                     if case[0].priority > 9:
  82.                         case[0].priority = 9
  83.                     currentExecQueue[case[0].priority].append(case[0])
  84.  
  85.  
  86.                 case.remove(case[0])
  87.  
  88.     return {
  89.         "currentExecQueue": currentExecQueue,
  90.         "execQueueHistory": execQueueHistory,
  91.         "pagesHistory": pagesHistory,
  92.         "oldestPageIndex": oldestPageIndex,
  93.         "clock": clock
  94.     }
  95.  
  96. def main():
  97.     quantum = 3
  98.     t1 = Process("T1", 10, 0, 1, [1,5])
  99.     t2 = Process("T2", 8, 1, 1, [2,5])
  100.     t3 = Process("T3", 4, 2, 1, [2,3,6])
  101.     t4 = Process("T4", 4, 3, 1, [4])
  102.     processes = [t1, t2, t3, t4]
  103.     pagesHistory = []
  104.     oldestPageIndex = 0
  105.     executionQueue = [[]] * 10
  106.     execQueueHistory = []
  107.     clock = [
  108.         {
  109.             'page': 0,
  110.             'bits': 0
  111.         }
  112.     ] * quantum
  113.  
  114.     while not isProcessDone(processes):
  115.         returnDict = (execution_queue(processes, quantum, executionQueue, execQueueHistory, pagesHistory, oldestPageIndex, clock))
  116.         executionQueue = returnDict['currentExecQueue']
  117.         execQueueHistory = returnDict['execQueueHistory']
  118.         pagesHistory = returnDict['pagesHistory']
  119.         oldestPageIndex = returnDict['oldestPageIndex']
  120.         clock = returnDict["clock"]
  121.  
  122.     for pageOrder in pagesHistory:
  123.         print(pageOrder)
  124.  
  125. if __name__ == '__main__':
  126.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement