Skip to content

Commit c736a70

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 6ef267d commit c736a70

File tree

2 files changed

+72
-42
lines changed

2 files changed

+72
-42
lines changed

cpu_scheduling_interface.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44

55
</div>
66

7-
A **GUI-based CPU Scheduling Algorithm Visualizer** built using **Python**, **Tkinter**, and **Matplotlib**.
7+
A **GUI-based CPU Scheduling Algorithm Visualizer** built using **Python**, **Tkinter**, and **Matplotlib**.
88
It allows users to add custom processes and simulate various scheduling algorithms with **real-time Gantt chart animation**, **ready queue visualization**, and **performance statistics**.
99

1010
---
1111

1212
## 🚀 Features
1313

14-
✅ Interactive **Tkinter GUI**
15-
✅ Supports multiple **CPU scheduling algorithms**
16-
✅ Real-time **Gantt chart animation** using Matplotlib
17-
✅ Displays **Ready Queue** (for Round Robin & Preemptive algorithms)
18-
✅ Shows **average waiting time**, **turnaround time**, and **response time**
19-
✅ Add or delete processes dynamically
20-
✅ Clean and responsive design
14+
✅ Interactive **Tkinter GUI**
15+
✅ Supports multiple **CPU scheduling algorithms**
16+
✅ Real-time **Gantt chart animation** using Matplotlib
17+
✅ Displays **Ready Queue** (for Round Robin & Preemptive algorithms)
18+
✅ Shows **average waiting time**, **turnaround time**, and **response time**
19+
✅ Add or delete processes dynamically
20+
✅ Clean and responsive design
2121

2222
---
2323

@@ -60,7 +60,7 @@ Displays process execution in timeline order, showing process IDs along the time
6060

6161
## 🧠 Working
6262

63-
1. Enter process details (`PID`, `Arrival`, `Burst`, `Priority`)
63+
1. Enter process details (`PID`, `Arrival`, `Burst`, `Priority`)
6464
2. Choose your desired **Scheduling Algorithm**
6565
3. (Optional) Enter Quantum value (for Round Robin)
6666
4. Click **Run**
@@ -72,9 +72,9 @@ Displays process execution in timeline order, showing process IDs along the time
7272
## 🛠️ Tech Stack
7373

7474
- **Python 3.8+**
75-
- **Tkinter** → GUI Framework
76-
- **Matplotlib** → Animation and Gantt Chart
77-
- **Threading** → Live simulation without freezing GUI
75+
- **Tkinter** → GUI Framework
76+
- **Matplotlib** → Animation and Gantt Chart
77+
- **Threading** → Live simulation without freezing GUI
7878

7979
---
8080

@@ -115,4 +115,4 @@ Shashwat
115115

116116
Made with ❤️ using Python & Tkinter
117117

118-
</div>
118+
</div>

scheduling/cpu_scheduling_interface.py

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
55
import threading, time, copy
66

7+
78
# ===================== Scheduler Engine ===================== #
89
class SchedulerEngine:
910
def __init__(self, processes, algorithm, quantum=2):
@@ -32,7 +33,7 @@ def simulate(self):
3233
yield from self._simulate_rr()
3334
self._calculate_stats()
3435

35-
#first come first serve
36+
# first come first serve
3637
def _simulate_fcfs(self):
3738
t = 0
3839
processes = sorted(self.processes, key=lambda p: p["arrival"])
@@ -44,14 +45,16 @@ def _simulate_fcfs(self):
4445
yield (t, p["pid"], [])
4546
t += 1
4647
p["completion"] = t
47-
48-
#shortest job first non preemptive
48+
49+
# shortest job first non preemptive
4950
def _simulate_sjf_np(self):
5051
t = 0
5152
processes = sorted(self.processes, key=lambda p: p["arrival"])
5253
done = 0
5354
while done < len(processes):
54-
ready = [p for p in processes if p["arrival"] <= t and "completion" not in p]
55+
ready = [
56+
p for p in processes if p["arrival"] <= t and "completion" not in p
57+
]
5558
if not ready:
5659
t += 1
5760
yield (t, None, [])
@@ -63,7 +66,7 @@ def _simulate_sjf_np(self):
6366
t += 1
6467
p["completion"] = t
6568
done += 1
66-
69+
6770
# shortest job first preemptive
6871
def _simulate_sjf_p(self):
6972
t = 0
@@ -83,13 +86,15 @@ def _simulate_sjf_p(self):
8386
p["completion"] = t + 1
8487
done += 1
8588
t += 1
86-
87-
# priority non preemptive
89+
90+
# priority non preemptive
8891
def _simulate_priority_np(self):
8992
t = 0
9093
done = 0
9194
while done < len(self.processes):
92-
ready = [p for p in self.processes if p["arrival"] <= t and "completion" not in p]
95+
ready = [
96+
p for p in self.processes if p["arrival"] <= t and "completion" not in p
97+
]
9398
if not ready:
9499
t += 1
95100
yield (t, None, [])
@@ -102,12 +107,14 @@ def _simulate_priority_np(self):
102107
p["completion"] = t
103108
done += 1
104109

105-
# priority preemptive
110+
# priority preemptive
106111
def _simulate_priority_p(self):
107112
t = 0
108113
done = 0
109114
while done < len(self.processes):
110-
ready = [p for p in self.processes if p["arrival"] <= t and p["remaining"] > 0]
115+
ready = [
116+
p for p in self.processes if p["arrival"] <= t and p["remaining"] > 0
117+
]
111118
if not ready:
112119
t += 1
113120
yield (t, None, [])
@@ -152,7 +159,6 @@ def _simulate_rr(self):
152159
p["completion"] = t
153160
done += 1
154161

155-
156162
def _calculate_stats(self):
157163
for p in self.processes:
158164
pid = p["pid"]
@@ -165,7 +171,8 @@ def _calculate_stats(self):
165171
rt = first_exec - arrival
166172
self.stats.append((pid, arrival, burst, completion, tat, wt, rt))
167173

168-
# Interface
174+
175+
# Interface
169176
class CPUSchedulerGUI:
170177
def __init__(self, root):
171178
self.root = root
@@ -179,7 +186,9 @@ def setup_ui(self):
179186
top_frame = ttk.Frame(self.root)
180187
top_frame.pack(pady=10)
181188

182-
self.tree = ttk.Treeview(top_frame, columns=("pid", "arrival", "burst", "priority"), show="headings")
189+
self.tree = ttk.Treeview(
190+
top_frame, columns=("pid", "arrival", "burst", "priority"), show="headings"
191+
)
183192
for col in self.tree["columns"]:
184193
self.tree.heading(col, text=col.capitalize())
185194
self.tree.pack(side="left")
@@ -198,23 +207,36 @@ def setup_ui(self):
198207
self.arrival_e.grid(row=1, column=1)
199208
self.burst_e.grid(row=2, column=1)
200209
self.priority_e.grid(row=3, column=1)
201-
ttk.Button(form, text="Add", command=self.add_process).grid(row=4, column=0, pady=5)
202-
ttk.Button(form, text="Delete", command=self.delete_process).grid(row=4, column=1)
210+
ttk.Button(form, text="Add", command=self.add_process).grid(
211+
row=4, column=0, pady=5
212+
)
213+
ttk.Button(form, text="Delete", command=self.delete_process).grid(
214+
row=4, column=1
215+
)
203216

204217
algo_frame = ttk.Frame(self.root)
205218
algo_frame.pack(pady=10)
206219
ttk.Label(algo_frame, text="Algorithm:").pack(side="left")
207-
self.algo_cb = ttk.Combobox(algo_frame, values=[
208-
"FCFS", "SJF (Non-Preemptive)", "SJF (Preemptive)",
209-
"Priority (Non-Preemptive)", "Priority (Preemptive)", "Round Robin"
210-
])
220+
self.algo_cb = ttk.Combobox(
221+
algo_frame,
222+
values=[
223+
"FCFS",
224+
"SJF (Non-Preemptive)",
225+
"SJF (Preemptive)",
226+
"Priority (Non-Preemptive)",
227+
"Priority (Preemptive)",
228+
"Round Robin",
229+
],
230+
)
211231
self.algo_cb.current(0)
212232
self.algo_cb.pack(side="left", padx=5)
213233
ttk.Label(algo_frame, text="Quantum:").pack(side="left")
214234
self.quantum_e = ttk.Entry(algo_frame, width=5)
215235
self.quantum_e.insert(0, "2")
216236
self.quantum_e.pack(side="left")
217-
ttk.Button(algo_frame, text="Run", command=self.run_scheduling).pack(side="left", padx=10)
237+
ttk.Button(algo_frame, text="Run", command=self.run_scheduling).pack(
238+
side="left", padx=10
239+
)
218240

219241
self.ready_label = ttk.Label(self.root, text="Ready Queue:")
220242
self.ready_list = tk.Listbox(self.root, height=3)
@@ -225,9 +247,12 @@ def setup_ui(self):
225247
self.canvas = FigureCanvasTkAgg(self.figure, master=self.root)
226248
self.canvas.get_tk_widget().pack()
227249

228-
self.result_box = ttk.Treeview(self.root,
229-
columns=("pid", "arrival", "burst", "completion", "tat", "wt", "rt"),
230-
show="headings", height=6)
250+
self.result_box = ttk.Treeview(
251+
self.root,
252+
columns=("pid", "arrival", "burst", "completion", "tat", "wt", "rt"),
253+
show="headings",
254+
height=6,
255+
)
231256
for col in self.result_box["columns"]:
232257
self.result_box.heading(col, text=col.upper())
233258
self.result_box.pack(pady=10)
@@ -241,14 +266,15 @@ def add_process(self):
241266
arrival = int(self.arrival_e.get())
242267
burst = int(self.burst_e.get())
243268
priority = int(self.priority_e.get() or 0)
244-
self.processes.append({"pid": pid, "arrival": arrival, "burst": burst, "priority": priority})
269+
self.processes.append(
270+
{"pid": pid, "arrival": arrival, "burst": burst, "priority": priority}
271+
)
245272
self.tree.insert("", "end", values=(pid, arrival, burst, priority))
246273
except ValueError:
247274
messagebox.showerror("Error", "Invalid input")
248275

249276
def delete_process(self):
250-
sel = self.tree.selection()
251-
if sel:
277+
if sel := self.tree.selection():
252278
pid = self.tree.item(sel[0])["values"][0]
253279
self.processes = [p for p in self.processes if p["pid"] != pid]
254280
self.tree.delete(sel[0])
@@ -277,7 +303,9 @@ def animate(self):
277303
current_pid = pid
278304
colors.setdefault(pid, plt.cm.tab20(len(colors) % 20))
279305
self.ax.barh(0, 1, left=x, color=colors[pid])
280-
self.ax.text(x + 0.5, 0, pid, ha="center", va="center", color="white", fontsize=9)
306+
self.ax.text(
307+
x + 0.5, 0, pid, ha="center", va="center", color="white", fontsize=9
308+
)
281309
x += 1
282310
self.ax.set_xticks(range(0, x + 1))
283311
self.ax.set_yticks([])
@@ -300,11 +328,13 @@ def show_results(self):
300328
total_tat += row[4]
301329
total_rt += row[6]
302330
n = len(self.engine.stats)
303-
self.avg_label.config(text=f"AVG WT = {total_wt/n:.2f} | AVG TAT = {total_tat/n:.2f} | AVG RT = {total_rt/n:.2f}")
331+
self.avg_label.config(
332+
text=f"AVG WT = {total_wt / n:.2f} | AVG TAT = {total_tat / n:.2f} | AVG RT = {total_rt / n:.2f}"
333+
)
334+
304335

305336
# main call
306337
if __name__ == "__main__":
307338
root = tk.Tk()
308339
CPUSchedulerGUI(root)
309340
root.mainloop()
310-

0 commit comments

Comments
 (0)