-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrtf.py
55 lines (49 loc) · 1.65 KB
/
srtf.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
from process import *
from queue_ import Queue
from stack import Stack
from utils import *
from priority_queue import *
class SRTF():
counter = 0
def __init__(self, process_stack):
SRTF.counter += 1
self.name = f"SRTF: {SRTF.counter}"
self.process_stack = process_stack
self.queue = PriorityQueue()
self.waiting_processes = []
self.time_step = 0
self.details = {"state": [], "level": []}
def step(self):
if not (self.process_stack.isEmpty() and self.queue.isEmpty() and not self.waiting_processes):
# Get arrived process
arrived_processes = getArrivedProcesses(self.process_stack, self.time_step)
for process in arrived_processes:
self.queue.push(process)
process = self.queue.pop()
if process:
self.details["state"].append(process.name)
self.details["level"].append(0)
process.decrementDuration()
if process.duration:
self.queue.push(process)
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__":
stack = Stack()
stack.push(Process(0, 10))
stack.push(Process(4, 8))
stack.push(Process(8, 7))
stack.push(Process(12, 1))
stack.sort()
arrival_times = getProcessData(stack)
srtf = SRTF(stack)
df = srtf.run()
plotGanttChart(df)