简体   繁体   中英

FCFS, python 3 what's wrong with my code?

I am attempting a "First come, first serve" scheduling algorithm, and all my process wait times end up being 0, even though the other wait times appear to be correct, or at least take on a value other than 0.

If anybody has any ideas as to why this is happening, please let me know.

Code

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)) 

Output 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

turns out the code is correct, i thought that processes are waiting but they are not.

I think that the logic of your code is incorrect, you "first serve" those processess that have the lowest Pnum . By the way i think that the convention is that you should name variables by lowercase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM