-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (52 loc) · 1.79 KB
/
main.py
File metadata and controls
70 lines (52 loc) · 1.79 KB
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
59
60
61
62
63
64
65
from core.process import Process
from core.process import Process
from algorithms.fcfs import fcfs
from algorithms.round_robin import round_robin
from algorithms.priority import priority_scheduling
from algorithms.preemptive_priority import preemptive_priority_scheduling
from algorithms.sjf import sjf
from algorithms.srtf import srtf
def create_processes(with_priority=False):
if with_priority:
return [
Process(1, 0, 5, priority=2),
Process(2, 1, 3, priority=1),
Process(3, 2, 8, priority=3),
]
else:
return [
Process(1, 0, 5),
Process(2, 1, 3),
Process(3, 2, 8),
]
def print_result(title, processes):
print(f"\n=== {title} ===")
print("PID | WT | TAT")
for p in processes:
print(f"P{p.pid} | {p.waiting_time} | {p.turnaround_time}")
if __name__ == "__main__":
print("CPU Scheduling Simulator\n")
# FCFS
processes = create_processes()
result = fcfs(processes)
print_result("FCFS", result)
# Round Robin
processes = create_processes()
result = round_robin(processes, time_quantum=2)
print_result("Round Robin (TQ=2)", result)
# Priority (Non-Preemptive)
processes = create_processes(with_priority=True)
result = priority_scheduling(processes)
print_result("Priority (Non-Preemptive)", result)
# Priority (Preemptive)
processes = create_processes(with_priority=True)
result = preemptive_priority_scheduling(processes)
print_result("Priority (Preemptive)", result)
# SJF
processes = create_processes()
result = sjf(processes)
print_result("SJF (Non-Preemptive)", result)
# SRTF
processes = create_processes()
result = srtf(processes)
print_result("SRTF (Preemptive)", result)