-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfcfs.py
58 lines (49 loc) · 1.71 KB
/
fcfs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from queue_ import Queue
from utils import *
from stack import Stack
from process import Process
class FCFS:
counter = 0
def __init__(self, process_stack):
FCFS.counter += 1
self.name = f"FCFS: {FCFS.counter}"
self.process_stack = process_stack
self.queue = Queue()
self.details = {"state": [], "level":[]}
self.time_step = 0
def step(self):
if not ( self.process_stack.isEmpty() and self.queue.isEmpty() ):
# Get arrived process
arrived_processes = getArrivedProcesses(self.process_stack, self.time_step)
for process in arrived_processes:
self.queue.push(process)
process = self.queue.peak()
if process:
self.details["state"].append(process.name)
self.details["level"].append(0)
process.duration -= 1
if process.duration:
self.queue.changePeakProcess(process)
else:
self.queue.pop()
else:
self.details["state"].append("idle")
self.details["level"].append(0)
self.time_step += 1
return True
return False
def run(self):
while self.step():
continue
return self.details
if __name__ == "__main__":
process_stack = Stack()
process_stack.push(Process(0, 5))
process_stack.push(Process(3, 10))
process_stack.push(Process(4, 9))
process_stack.push(Process(7, 2))
process_stack.push(Process(8, 3))
process_stack.sort()
fcfs = FCFS(process_stack=process_stack)
details = fcfs.run()
plotGanttChart(details)