Skip to content

Commit c989db7

Browse files
Added preemptive (priority + sjf) scheduling algorithms
1 parent a71618f commit c989db7

2 files changed

Lines changed: 155 additions & 0 deletions

File tree

scheduling/preemptive_sjf(srtf).py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"""
2+
Shortest Remaining Time First (SRTF) scheduling is a preemptive version of Shortest Job First (SJF).
3+
At every unit of time, it selects the process with the smallest remaining burst time
4+
among the processes that have arrived.
5+
https://en.wikipedia.org/wiki/Shortest_job_next#Preemptive_SJF_(SRTF)
6+
"""
7+
8+
from statistics import mean
9+
from typing import List
10+
11+
def calculate_srtf_waiting_time(arrival: List[int], burst: List[int]) -> List[int]:
12+
"""
13+
Calculate waiting time for each process using Shortest Remaining Time First (SRTF).
14+
15+
Args:
16+
arrival: List of arrival times of processes.
17+
burst: List of burst times of processes.
18+
19+
Returns:
20+
List of waiting times for each process.
21+
22+
>>> calculate_srtf_waiting_time([0, 1, 2, 3], [8, 4, 9, 5])
23+
[9, 0, 15, 2]
24+
"""
25+
n = len(arrival)
26+
remaining = burst.copy()
27+
waiting = [0] * n
28+
completed = 0
29+
t = 0
30+
last_executed = -1
31+
completion_time = [0] * n
32+
33+
while completed < n:
34+
# Find process with smallest remaining time that has arrived
35+
idx = -1
36+
min_remaining = float('inf')
37+
for i in range(n):
38+
if arrival[i] <= t and remaining[i] > 0 and remaining[i] < min_remaining:
39+
min_remaining = remaining[i]
40+
idx = i
41+
42+
if idx == -1:
43+
t += 1 # No process ready, advance time
44+
continue
45+
46+
# Execute process for 1 unit of time
47+
remaining[idx] -= 1
48+
t += 1
49+
50+
# If process finished, record completion and calculate waiting
51+
if remaining[idx] == 0:
52+
completed += 1
53+
completion_time[idx] = t
54+
waiting[idx] = completion_time[idx] - arrival[idx] - burst[idx]
55+
56+
return waiting
57+
58+
def calculate_srtf_turnaround_time(arrival: List[int], burst: List[int], waiting: List[int]) -> List[int]:
59+
"""
60+
Calculate turnaround time for each process using waiting time.
61+
62+
Args:
63+
arrival: List of arrival times.
64+
burst: List of burst times.
65+
waiting: List of waiting times.
66+
67+
Returns:
68+
List of turnaround times.
69+
70+
>>> calculate_srtf_turnaround_time([0,1,2,3],[8,4,9,5],[6,0,12,0])
71+
[14, 4, 21, 5]
72+
"""
73+
return [burst[i] + waiting[i] for i in range(len(burst))]
74+
75+
if __name__ == "__main__":
76+
arrival_time = [0, 1, 2, 3]
77+
burst_time = [8, 4, 9, 5]
78+
79+
waiting_time = calculate_srtf_waiting_time(arrival_time, burst_time)
80+
turnaround_time = calculate_srtf_turnaround_time(arrival_time, burst_time, waiting_time)
81+
82+
print("PID\tArrival\tBurst\tWaiting\tTurnaround")
83+
for i in range(len(arrival_time)):
84+
print(f"P{i+1}\t{arrival_time[i]}\t{burst_time[i]}\t{waiting_time[i]}\t{turnaround_time[i]}")
85+
86+
print(f"Average Waiting Time: {mean(waiting_time):.2f}")
87+
print(f"Average Turnaround Time: {mean(turnaround_time):.2f}")

scheduling/priority_scheduling.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
Priority Scheduling assigns a priority to each process. The CPU is allocated
3+
to the process with the highest priority (lowest number). It can be preemptive
4+
or non-preemptive.
5+
https://en.wikipedia.org/wiki/Priority_scheduling
6+
"""
7+
8+
from statistics import mean
9+
10+
def calculate_priority_waiting_time(arrival: list, burst: list, priority: list) -> list:
11+
"""
12+
Calculate waiting time for each process using preemptive priority scheduling.
13+
14+
>>> calculate_priority_waiting_time([0, 1, 2, 3], [10, 1, 2, 1], [3, 1, 4, 2])
15+
[2, 0, 10, 0]
16+
"""
17+
n = len(arrival)
18+
remaining = burst.copy()
19+
waiting = [0] * n
20+
complete = 0
21+
t = 0
22+
23+
while complete < n:
24+
idx = -1
25+
highest_pri = float('inf')
26+
for i in range(n):
27+
if arrival[i] <= t and remaining[i] > 0 and priority[i] < highest_pri:
28+
highest_pri = priority[i]
29+
idx = i
30+
31+
if idx == -1:
32+
t += 1
33+
continue
34+
35+
remaining[idx] -= 1
36+
t += 1
37+
38+
if remaining[idx] == 0:
39+
complete += 1
40+
waiting[idx] = t - arrival[idx] - burst[idx]
41+
42+
return waiting
43+
44+
45+
def calculate_priority_turnaround_time(burst: list, waiting: list) -> list:
46+
"""
47+
Calculate turn around time for each process using waiting time.
48+
49+
>>> calculate_priority_turnaround_time([10, 1, 2, 1], [3, 0, 5, 1])
50+
[13, 1, 7, 2]
51+
"""
52+
return [burst[i] + waiting[i] for i in range(len(burst))]
53+
54+
55+
if __name__ == "__main__":
56+
arrival_time = [0, 1, 2, 3]
57+
burst_time = [10, 1, 2, 1]
58+
priority = [3, 1, 4, 2]
59+
60+
waiting_time = calculate_priority_waiting_time(arrival_time, burst_time, priority)
61+
turnaround_time = calculate_priority_turnaround_time(burst_time, waiting_time)
62+
63+
print("PID\tArrival\tBurst\tPriority\tWaiting\tTurnaround")
64+
for i in range(len(arrival_time)):
65+
print(f"P{i+1}\t{arrival_time[i]}\t{burst_time[i]}\t{priority[i]}\t\t{waiting_time[i]}\t{turnaround_time[i]}")
66+
67+
print(f"Average Waiting Time: {mean(waiting_time):.2f}")
68+
print(f"Average Turnaround Time: {mean(turnaround_time):.2f}")

0 commit comments

Comments
 (0)