3
1
SystemsProgrammingAndOperat.../Codes/Python version/Assignment-A5 (CPU Scheduling)/Priority.py

24 lines
892 B
Python
Raw Normal View History

def priority_scheduling(processes, n):
processes.sort(key=lambda x: (x[2], x[1]))
completion_time = 0
waiting_time = []
turnaround_time = []
for process in processes:
pid, arrival_time, priority, burst_time = process
if completion_time < arrival_time:
completion_time = arrival_time
completion_time += burst_time
turnaround_time.append(completion_time - arrival_time)
waiting_time.append(completion_time - arrival_time - burst_time)
print("\nPriority (Non-Preemptive) Scheduling:")
for i, process in enumerate(processes):
print(f"Process {process[0]}: Waiting Time = {waiting_time[i]}, Turnaround Time = {turnaround_time[i]}")
# Input: Process ID, Arrival Time, Priority, Burst Time
processes = [[1, 0, 1, 5], [2, 1, 3, 3], [3, 2, 2, 8], [4, 3, 4, 6]]
priority_scheduling(processes, len(processes))