-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriority Scheduling Algorithm.py
72 lines (59 loc) · 1.93 KB
/
Priority Scheduling Algorithm.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
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 10 11:34:59 2019
@author: USER
"""
#Process Burst Time Priority
#P1 11 5
#P2 10 2
#P3 15 3
#P4 14 4
#P5 13 1
#bt,wt,tat stands for burst time, waiting time, turn around time respectively
print("Enter the number of processess: ")
n=int(input())
processes=[]
for i in range(0,n):
processes.insert(i,i+1)
#Input Burst time of every process
print("\nEnter the burst time of the processes: \n")
bt=list(map(int, input().split()))
#Input Priority of every process
print("\nEnter the priority of the processes: \n")
priority=list(map(int, input().split()))
tat=[]
wt=[]
#Sorting processes burst time, on the basis of their priority
for i in range(0,len(priority)-1):
for j in range(0,len(priority)-i-1):
if(priority[j]>priority[j+1]):
swap=priority[j]
priority[j]=priority[j+1]
priority[j+1]=swap
swap=bt[j]
bt[j]=bt[j+1]
bt[j+1]=swap
swap=processes[j]
processes[j]=processes[j+1]
processes[j+1]=swap
wt.insert(0,0)
tat.insert(0,bt[0])
#Calculating of waiting time and Turn Around Time of each process
for i in range(1,len(processes)):
wt.insert(i,wt[i-1]+bt[i-1])
tat.insert(i,wt[i]+bt[i])
#calculating average waiting time and average turn around time
avgtat=0
avgwt=0
for i in range(0,len(processes)):
avgwt=avgwt+wt[i]
avgtat=avgtat+tat[i]
avgwt=float(avgwt)/n
avgwt=float(avgtat)/n
print("\n")
print("Process\t Burst Time\t Waiting Time\t Turn Around Time")
for i in range(0,n):
print(str(processes[i])+"\t\t"+str(bt[i])+"\t\t"+str(wt[i])+"\t\t"+str(tat[i]))
print("\n")
print("Average Waiting time is: "+str(avgwt))
print("Average Turn Around Time is: "+str(avgtat))