Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c464cbc

Browse files
committedOct 18, 2024·
Apply recommended fixes from PR review
1 parent 2c39830 commit c464cbc

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed
 

‎cpu_scheduling_algorithms/fcfs.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,46 @@ def fcfs(processes: list[tuple[int, int, int]]) -> None:
1313
1414
processes: list of tuples where each tuple contains the
1515
process_id, arrival_time and burst_time of each process
16+
17+
>>> processes = [(1, 0, 3), (2, 2, 7), (3, 1, 4), (4, 5, 2)]
18+
>>> fcfs(processes)
19+
Average Waiting Time: 6.0
20+
*---*----*-------*--
21+
1 3 2 4
1622
"""
17-
n = len(processes)
18-
processes.sort(key=lambda x: x[1])
19-
waiting_times = [0] * n
23+
total_processes = len(processes)
24+
processes.sort(key=lambda process: process[1])
25+
waiting_times = [0] * total_processes
2026
total_waiting_time = 0
2127

22-
for i in range(1, n):
28+
for i in range(1, total_processes):
2329
waiting_times[i] = processes[i - 1][2] + waiting_times[i - 1]
2430
total_waiting_time += waiting_times[i]
2531

26-
print(f"Average Waiting Time: {total_waiting_time / n}")
32+
print(f"Average Waiting Time: {total_waiting_time / total_processes}")
2733

2834
"""
2935
Printing the Gantt Chart for the processes in the FCFS order
3036
The - and * symbols are used to represent the burst time and
3137
idle time respectively for each process.
3238
"""
3339
last_burst = 0
34-
for i in range(n):
40+
for i in range(total_processes):
3541
print("-" * last_burst, end="")
3642
print("*", end="")
3743
last_burst = processes[i][2]
3844
print("-" * last_burst, end="")
3945
print("\n", end="")
4046

4147
last_burst = 0
42-
for i in range(n):
48+
for i in range(total_processes):
4349
print(" " * last_burst, end="")
4450
print(f"{processes[i][0]}", end="")
4551
last_burst = processes[i][2]
4652
print("\n", end="")
4753

4854

49-
def main():
55+
def main() -> None:
5056
"""
5157
Main function to demonstrate the FCFS CPU scheduling algorithm
5258
pass an array of (process_id, arrival_time, burst_time) to fcfs

0 commit comments

Comments
 (0)
Please sign in to comment.