2

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
Community
  • 1
  • 1
R00
  • 73
  • 1
  • 9
  • Did you try to check whether the `elif list[i].arrival < list[i-1].completionTime` condition is ever satisfied? – Karl Knechtel May 11 '20 at 22:38
  • " the values for the Completion time and the burst time are all correct and the waiting time values should be correct then no?" Well, are you sure these values *aren't* correct? Why are you *expecting* wait times other than zero? – Karl Knechtel May 11 '20 at 22:39
  • wait you're right no process has to wait, my code is correct – R00 May 11 '20 at 22:47

2 Answers2

1

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

R00
  • 73
  • 1
  • 9
-1

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.