python中的调度算法FCFS

时间:2018-11-28 19:38:08

标签: python python-3.x

我需要在Python中实现先来先服务调度算法,因此我必须按升序对到达时间进行排序。我收到错误list index out of range。排序的条件应该是什么?我认为我的情况正确,但是不起作用。

arrival_time=[]

burst_time=[]

ch=int(input("Enter number of processes:"))

i=0

while i<ch:

    a=int(input("Enter arrival time:"))
    b=int(input("Enter burst time:"))
    arrival_time.insert(i,a)
    burst_time.insert(i,b)
    i=i+1

print("Process Arrival Time  Burst Time ")
print(arrival_time,burst_time)

count=0

for j in range(ch-1):
     for l in range(l+1+count,ch):
        if arrival_time[j]>arrival_time[l+1+count]:
            temp=arrival_time[j]
            arrival_time[j]=arrival_time[l+1+count]
            arrival_time[l+1+count]=temp
     count=count+1


print(arrival_time)  
k=0
sum=0
print("Gantt Chart")

while k<ch:
    if k==0:
        sum=sum+burst_time[k]      
        print(arrival_time[k],"------",sum)

    elif k>0:

        sum1=sum+burst_time[k]       
        print("-------",sum1)

    k=k+1

1 个答案:

答案 0 :(得分:0)

bt=[]
print("Enter the number of process: ")
n=int(input())
print("Enter the burst time of the processes: \n")
bt=list(map(int, raw_input().split()))

wt=[]
avgwt=0
tat=[]
avgtat=0

wt.insert(0,0)
tat.insert(0,bt[0])

for i in range(1,len(bt)):
   wt.insert(i,wt[i-1]+bt[i-1])
   tat.insert(i,wt[i]+bt[i])
   avgwt+=wt[i]
   avgtat+=tat[i]

avgwt=float(avgwt)/n
avgtat=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(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 Arount Time is: "+str(avgtat))

运行: enter image description here