Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Process(object):
- """docstring for Process"""
- def __init__(self, name, exec_time, exec_order, priority, pages):
- super(Process, self).__init__()
- self.name = name
- self.exec_time = exec_time
- self.exec_order = exec_order
- self.priority = priority
- self.pages = pages
- self.finished = False
- def __str__(self):
- return self.name
- def isProcessDone(processes):
- for p in processes:
- if not p.finished:
- return False
- return True
- def seconde_chance(process, pagesHistory, oldestPageIndex, clock):
- pagesToAdd = process.pages
- pagingErrors = 0
- for page in pagesToAdd:
- if len(pagesHistory) == 0:
- pagesHistory.append([page])
- pagingErrors += 1
- clock[0]['page'] = page
- elif len(pagesHistory[-1]) == 1:
- if not page in pagesHistory[-1]:
- pagesHistory.append([pagesHistory[0][0], page])
- pagingErrors += 1
- clock[1]['page'] = page
- else:
- pagesHistory.append(pagesHistory[-1])
- elif len(pagesHistory[-1]) == 2:
- if not page in pagesHistory[-1]:
- pagesHistory.append([pagesHistory[0][0], pagesHistory[1][1], page])
- pagingErrors += 1
- clock[2]['page'] = page
- else:
- pagesHistory.append(pagesHistory[-1])
- else:
- if not page in pagesHistory[-1]:
- while clock[oldestPageIndex]['bits'] == 1:
- clock[oldestPageIndex]['bits'] = 0
- oldestPageIndex += 1
- oldestPageIndex %= 3
- clock[oldestPageIndex]['page'] = page
- clock[oldestPageIndex]['bits'] = 1
- pagingErrors += 1
- else:
- pagesHistory.append(pagesHistory[-1])
- process.priority += pagingErrors
- return pagesHistory, oldestPageIndex, clock
- def execution_queue(processes, quantum, currentExecQueue, execQueueHistory, pagesHistory, oldestPageIndex, clock):
- if currentExecQueue == [[]] * 10:
- for p in processes:
- currentExecQueue[p.priority].append(p)
- else:
- for case in currentExecQueue:
- if not case == []:
- for i in range(quantum):
- if case[0].exec_time - 1 >= 0:
- case[0].exec_time -= 1
- execQueueHistory.append(case[0].name)
- else:
- execQueueHistory.append("X")
- if case[0].exec_time == 0:
- case[0].finished = True
- print(case[0].name + " is finished!")
- else:
- seconde_chance(case[0], pagesHistory, oldestPageIndex, clock)
- case[0].priority += quantum
- if case[0].priority > 9:
- case[0].priority = 9
- currentExecQueue[case[0].priority].append(case[0])
- case.remove(case[0])
- return {
- "currentExecQueue": currentExecQueue,
- "execQueueHistory": execQueueHistory,
- "pagesHistory": pagesHistory,
- "oldestPageIndex": oldestPageIndex,
- "clock": clock
- }
- def main():
- quantum = 3
- t1 = Process("T1", 10, 0, 1, [1,5])
- t2 = Process("T2", 8, 1, 1, [2,5])
- t3 = Process("T3", 4, 2, 1, [2,3,6])
- t4 = Process("T4", 4, 3, 1, [4])
- processes = [t1, t2, t3, t4]
- pagesHistory = []
- oldestPageIndex = 0
- executionQueue = [[]] * 10
- execQueueHistory = []
- clock = [
- {
- 'page': 0,
- 'bits': 0
- }
- ] * quantum
- while not isProcessDone(processes):
- returnDict = (execution_queue(processes, quantum, executionQueue, execQueueHistory, pagesHistory, oldestPageIndex, clock))
- executionQueue = returnDict['currentExecQueue']
- execQueueHistory = returnDict['execQueueHistory']
- pagesHistory = returnDict['pagesHistory']
- oldestPageIndex = returnDict['oldestPageIndex']
- clock = returnDict["clock"]
- for pageOrder in pagesHistory:
- print(pageOrder)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement