-
Notifications
You must be signed in to change notification settings - Fork 368
/
priority_scheduling.py
77 lines (60 loc) · 2.4 KB
/
priority_scheduling.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class PriorityScheduling:
def __init__(self):
self.name = ""
self.completionTime = 0
self.arrivalTime = 0
self.burstTime = 0
self.turnAroundTime = 0
self.responseTime = 0
self.waitingTime = 0
self.priority = 0
curr = 0
if __name__ == "__main__":
avgTurnAroundTime = 0
avgWaitingTime = 0
avgResponseTime = 0
print("Enter Number of Processes: ")
n = int(input())
process = []
print("Enter Name of Processes: ")
for i in range(n):
p = PriorityScheduling()
p.name = input()
process.append(p)
print("Enter Arrival Time of Processes: ")
for i in range(n):
process[i].arrivalTime = int(input())
print("Enter Priority of Processes: ")
for i in range(n):
process[i].priority = int(input())
print("Enter Burst Time of Processes: ")
for i in range(n):
process[i].burstTime = int(input())
for i in range(n):
process.sort(key=lambda x: x.priority-curr)
process[i].responseTime = curr - process[i].arrivalTime
process[i].completionTime = curr + process[i].burstTime
curr = process[i].completionTime
process[i].turnAroundTime = process[i].completionTime - \
process[i].arrivalTime
process[i].waitingTime = process[i].turnAroundTime - \
process[i].burstTime
avgTurnAroundTime += process[i].turnAroundTime
avgResponseTime += process[i].responseTime
avgWaitingTime += process[i].waitingTime
print(" AT\tP\tBT\tCT\tTAT\tWT\tRT")
for i in range(n):
print(process[i].name + ": " + str(process[i].arrivalTime) + "\t" + str(process[i].priority) + "\t" + str(process[i].burstTime) + "\t" + str(
process[i].completionTime) + "\t" + str(process[i].turnAroundTime) + "\t" + str(process[i].waitingTime) + "\t" + str(process[i].responseTime))
print()
print("Average TurnAroundTime: " + str(avgTurnAroundTime / n))
print("Average WaitingTime: " + str(avgWaitingTime / n))
print("Average ResponseTime: " + str(avgResponseTime / n))
print()
print("Gantt Chart")
print(str(process[0].arrivalTime) + " |" +
process[0].name + "| ", end="")
for i in range(n - 1):
print(str(process[i].completionTime) + " |" +
process[i + 1].name + "| ", end="")
print(str(process[n - 1].completionTime))