-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprioritysched.py
32 lines (26 loc) · 1.2 KB
/
prioritysched.py
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
import time
# Processes with their priorities and burst times
processes = [{'id': 'P1', 'priority': 2, 'burst': 10},
{'id': 'P2', 'priority': 1, 'burst': 4},
{'id': 'P3', 'priority': 3, 'burst': 6}]
# Function to simulate priority scheduling
def priority_scheduling(processes):
processes.sort(key=lambda x: x['priority'])
time_passed = 0
turnaround_times = {}
response_times = {}
first_response = {}
for process in processes:
print(f"Process {process['id']} starts at time {time_passed}")
response_times[process['id']] = time_passed
if process['id'] not in first_response:
first_response[process['id']] = time_passed
time_passed += process['burst']
turnaround_times[process['id']] = time_passed
print(f"Process {process['id']} finishes at time {time_passed}")
time.sleep(process['burst'] * 0.1)
avg_turnaround_time = sum(turnaround_times.values()) / len(turnaround_times)
avg_response_time = sum(response_times.values()) / len(response_times)
print(f"Average turnaround time: {avg_turnaround_time}")
print(f"Average response time: {avg_response_time}")
priority_scheduling(processes)