FCFS,python 3 我的代码有什么问题?
FCFS, python 3 what's wrong with my code?
我正在尝试“先到先得”的调度算法,但我所有的进程等待时间最终都为 0,即使其他等待时间看起来是正确的,或者至少取了一个不同于0.
如果有人对发生这种情况的原因有任何想法,请告诉我。
代码
class process:
Pnum = 0
burst = 0
arrival = 0
waitTime = 0
turnAroundTime = 0
responseTime = 0
completionTime = 0
first = False
last = False
def __init__(self,Pnum,burst,arrival):
self.Pnum = Pnum
self.burst = burst
self.arrival = arrival
# (Process#, Burst time, Arrival time)
P1 = process(1, 5, 0)
P2 = process(2, 2, 5)
P3 = process(3, 4, 10)
P4 = process(4, 2, 18)
P5 = process(5, 4, 20)
list = [P1,P2,P3,P4,P5]
P1.first = True
P5.last = True
def run(list):
counter = 0
for i in range(len(list)):
#If this is the first process
if i == 0:
list[i].completionTime = list[i].arrival + list[i].burst
# If this process came before the previous process finished execution
elif list[i].arrival < list[i-1].completionTime:
list[i].completionTime = list[i-1].completionTime + list[i].burst
# If there is a gap between this process adn the previous one
else:
list[i].completionTime = list[i].arrival + list[i].burst
list[i].turnAroundTime = list[i].completionTime - list[i].arrival
print(list[i].burst)
list[i].waitTime = list[i].turnAroundTime - list[i].burst
list[i].responseTime = list[i].waitTime
run(list)
print("\n")
print("Process\t Burst Time\tArrival\t Complete\t Wait Time\t Turn Around Time\t")
for k in range(0,len(list)):
print(str(list[k].Pnum)+"\t\t"+str(list[k].burst)+"\t\t"+
str(list[k].arrival)+"\t\t"+str(list[k].completionTime)+"\t\t"+
str(list[k].waitTime)+"\t\t"+str(list[k].turnAroundTime))
输出table:
Process Burst Time Arrival Complete Wait Time Turn Around Time
1 5 0 5 0 5
2 2 5 7 0 2
3 4 10 14 0 4
4 2 18 20 0 2
5 4 20 24 0 4
原来代码是正确的,我以为进程在等待,但实际上不是。
我认为您的代码逻辑不正确,您“首先服务”那些具有最低 Pnum
的进程。顺便说一句,我认为惯例是你应该用小写字母命名变量。
我正在尝试“先到先得”的调度算法,但我所有的进程等待时间最终都为 0,即使其他等待时间看起来是正确的,或者至少取了一个不同于0.
如果有人对发生这种情况的原因有任何想法,请告诉我。
代码
class process:
Pnum = 0
burst = 0
arrival = 0
waitTime = 0
turnAroundTime = 0
responseTime = 0
completionTime = 0
first = False
last = False
def __init__(self,Pnum,burst,arrival):
self.Pnum = Pnum
self.burst = burst
self.arrival = arrival
# (Process#, Burst time, Arrival time)
P1 = process(1, 5, 0)
P2 = process(2, 2, 5)
P3 = process(3, 4, 10)
P4 = process(4, 2, 18)
P5 = process(5, 4, 20)
list = [P1,P2,P3,P4,P5]
P1.first = True
P5.last = True
def run(list):
counter = 0
for i in range(len(list)):
#If this is the first process
if i == 0:
list[i].completionTime = list[i].arrival + list[i].burst
# If this process came before the previous process finished execution
elif list[i].arrival < list[i-1].completionTime:
list[i].completionTime = list[i-1].completionTime + list[i].burst
# If there is a gap between this process adn the previous one
else:
list[i].completionTime = list[i].arrival + list[i].burst
list[i].turnAroundTime = list[i].completionTime - list[i].arrival
print(list[i].burst)
list[i].waitTime = list[i].turnAroundTime - list[i].burst
list[i].responseTime = list[i].waitTime
run(list)
print("\n")
print("Process\t Burst Time\tArrival\t Complete\t Wait Time\t Turn Around Time\t")
for k in range(0,len(list)):
print(str(list[k].Pnum)+"\t\t"+str(list[k].burst)+"\t\t"+
str(list[k].arrival)+"\t\t"+str(list[k].completionTime)+"\t\t"+
str(list[k].waitTime)+"\t\t"+str(list[k].turnAroundTime))
输出table:
Process Burst Time Arrival Complete Wait Time Turn Around Time
1 5 0 5 0 5
2 2 5 7 0 2
3 4 10 14 0 4
4 2 18 20 0 2
5 4 20 24 0 4
原来代码是正确的,我以为进程在等待,但实际上不是。
我认为您的代码逻辑不正确,您“首先服务”那些具有最低 Pnum
的进程。顺便说一句,我认为惯例是你应该用小写字母命名变量。