Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import copy
- Tasks = [] #where the tasks are stored
- time = [0] #the current time
- NewTasks = [] #where I put the modified tasks from arrival time
- try:
- val = int(input("How Many Tasks? "))
- for i in range(val):
- print("\nValues for T"+str(i+1))
- A = int(input("Arrival Time: "))
- B = int(input("Burst Time: "))
- Tasks.append(["T"+str(i+1),A,B,0,0])
- #0 is name, 1 is arrival, 2 is burst
- #3 is wait, 4 is turnaround
- except:
- print("invalid input")
- OldTasks = copy.deepcopy(Tasks)
- print()
- #Above is the inputs
- #Start Doing stuff here.
- def FCFS():
- Largest = 0
- for x in Tasks: #finding the largest number
- if x[1] > Largest:
- Largest = x[1]
- for x in range(Largest+1): #ordering the array from smallest to highest arrival time
- for y in Tasks:
- if y[1] == x:
- NewTasks.append(y)
- gant = "|0" #gant chart, changed during processing
- for x in NewTasks:
- if time[0] < x[1]: #incase all arrival times are above 0
- gant = gant+("_"*(x[1]-time[0]))+"|"+str(x[1])
- time[0] = x[1]
- x[3] = time[0]-x[1] #waittime = current time - arrival time
- time[0] += x[2] #current time += burst time to change time and finish
- gant = gant+("_"*int((x[2]/2)))+x[0]+("_"*int((x[2]/2)))+"|"+str(time[0]) #Edit the gant chart
- x[4] = time[0] - x[1] #turn around time = current time - arrival time after burst
- print(x[0],time[0]-x[2],"->",x[4]+x[1]) #print out the before and after
- print()
- print(gant) #print the gant chart
- print()
- print("Task\tArrival\tBurst\tWait\tTurnAround")#print columns using \t
- for x in Tasks: #print all values of the real task to keep the ordering from 1-n
- for y in x:
- print(y,end=("\t"))
- print()
- awt = 0
- atat = 0
- num = 0
- for x in Tasks: #gets total values for wait and turn around
- awt = awt+x[3]
- atat = atat+x[4]
- awt /= val #divides by number of tasks
- atat /= val
- print("Average Wait Time: " + str(awt)) #prints the average
- print("Average Turn Around Time: " + str(atat))
- def SJF():
- Largest = 0
- for x in Tasks:
- if x[1] > Largest:
- Largest = x[1]
- gant = "|0"
- SJF = []
- count = 0
- check = False
- Target = None
- Temp = None
- for x in range(Largest+1):
- for y in Tasks:
- if y[1] == x:
- check = True
- SJF.append(y)
- if time[0] < y[1]:
- gant = gant+("_"*(y[1]-time[0]))+"|"+str(y[1])
- time[0] = y[1]
- if check:
- SmallBurst = 1e+15
- for y in SJF:
- if y[2] < SmallBurst:
- SmallBurst = y[2]
- Target = y
- check = False
- if Temp == None:
- Temp = Target
- try:
- if Target != Temp and count > 0:
- print(Temp[0],time[0]-count,"->",time[0])
- gant = gant+("_"*int(count/2)+Temp[0]+"_"*int(count/2)+"|"+str(time[0]))
- count = 0
- Temp = Target
- except:
- brr = 0
- if Target != None:
- Target[2] -= 1
- Target[3] -= 1
- time[0] += 1
- count += 1
- Largest = 0
- for x in Tasks:
- if x[2] > Largest:
- Largest = x[2]
- for x in range(Largest+1):
- for y in Tasks:
- if y[2] == x:
- NewTasks.append(y)
- for x in NewTasks:
- if time[0] < x[1]:
- gant = gant+("_"*(x[1]-time[0]))+"|"+str(x[1])
- time[0] = x[1]
- if Target[0] == x[0]:
- temptime = time[0]
- x[3] += (time[0]-x[1])
- time[0] += x[2]
- gant = gant+("_"*int(((x[2]+count)/2)))+x[0]+("_"*int(((x[2]+count)/2)))+"|"+str(time[0])
- x[4] = time[0] - x[1]
- print(x[0],time[0]-x[2]-count,"->",x[4]+x[1])
- else:
- temptime = time[0]
- x[3] += (time[0]-x[1])
- time[0] += x[2]
- gant = gant+("_"*int((x[2]/2)))+x[0]+("_"*int((x[2]/2)))+"|"+str(time[0])
- x[4] = time[0] - x[1]
- print(x[0],time[0]-x[2],"->",x[4]+x[1])
- print()
- print(gant)
- print()
- print("Task\tArrival\tBurst\tWait\tTurnAround")
- for i,x in enumerate(Tasks):
- x[1] = OldTasks[i][1]
- x[2] = OldTasks[i][2]
- for y in x:
- print(y,end=("\t"))
- print()
- awt = 0
- atat = 0
- num = 0
- for x in Tasks:
- awt = awt+x[3]
- atat = atat+x[4]
- num += 1
- awt /= num
- atat /= num
- print("Average Wait Time: " + str(awt))
- print("Average Turn Around Time: " + str(atat))
- choice = input("What Process? \n 1. First Come First Serve \n 2. Shortest Job First\n")
- if choice == "1":
- FCFS()
- elif choice == "2":
- SJF()
- else:
- print("Invalid Option. Aborting Program")
Add Comment
Please, Sign In to add comment