iStunniq

FCFS/SJF

Jun 17th, 2021 (edited)
777
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import copy
  2. Tasks = []  #where the tasks are stored
  3. time = [0]  #the current time
  4. NewTasks = []   #where I put the modified tasks from arrival time
  5.  
  6. try:
  7.     val = int(input("How Many Tasks? "))
  8.     for i in range(val):
  9.         print("\nValues for T"+str(i+1))
  10.         A = int(input("Arrival Time: "))
  11.         B = int(input("Burst Time: "))
  12.         Tasks.append(["T"+str(i+1),A,B,0,0])
  13.         #0 is name, 1 is arrival, 2 is burst
  14.         #3 is wait, 4 is turnaround
  15. except:
  16.     print("invalid input")
  17.  
  18. OldTasks = copy.deepcopy(Tasks)
  19. print()
  20.  
  21. #Above is the inputs
  22. #Start Doing stuff here.
  23.  
  24. def FCFS():
  25.     Largest = 0
  26.     for x in Tasks: #finding the largest number
  27.         if x[1] > Largest:
  28.             Largest = x[1]
  29.     for x in range(Largest+1)#ordering the array from smallest to highest arrival time
  30.         for y in Tasks:
  31.             if y[1] == x:
  32.                 NewTasks.append(y)
  33.  
  34.     gant = "|0" #gant chart, changed during processing
  35.     for x in NewTasks:
  36.         if time[0] < x[1]#incase all arrival times are above 0
  37.             gant = gant+("_"*(x[1]-time[0]))+"|"+str(x[1])
  38.             time[0] = x[1]
  39.         x[3] = time[0]-x[1] #waittime = current time - arrival time
  40.         time[0] += x[2] #current time += burst time to change time and finish
  41.         gant = gant+("_"*int((x[2]/2)))+x[0]+("_"*int((x[2]/2)))+"|"+str(time[0])   #Edit the gant chart
  42.         x[4] = time[0] - x[1]   #turn around time = current time - arrival time after burst
  43.         print(x[0],time[0]-x[2],"->",x[4]+x[1]) #print out the before and after
  44.     print()
  45.     print(gant) #print the gant chart
  46.     print()
  47.     print("Task\tArrival\tBurst\tWait\tTurnAround")#print columns using \t
  48.     for x in Tasks: #print all values of the real task to keep the ordering from 1-n
  49.         for y in x:
  50.             print(y,end=("\t"))
  51.         print()
  52.     awt = 0
  53.     atat = 0
  54.     num = 0
  55.     for x in Tasks: #gets total values for wait and turn around
  56.         awt = awt+x[3]
  57.         atat = atat+x[4]
  58.     awt /= val  #divides by number of tasks
  59.     atat /= val
  60.     print("Average Wait Time: " + str(awt)) #prints the average
  61.     print("Average Turn Around Time: " + str(atat))
  62.  
  63. def SJF():
  64.     Largest = 0
  65.     for x in Tasks:
  66.         if x[1] > Largest:
  67.             Largest = x[1]
  68.  
  69.     gant = "|0"
  70.  
  71.     SJF = []
  72.     count = 0
  73.     check = False
  74.     Target = None
  75.     Temp = None
  76.     for x in range(Largest+1):
  77.         for y in Tasks:
  78.             if y[1] == x:
  79.                 check = True
  80.                 SJF.append(y)
  81.                 if time[0] < y[1]:
  82.                     gant = gant+("_"*(y[1]-time[0]))+"|"+str(y[1])
  83.                     time[0] = y[1]
  84.         if check:
  85.             SmallBurst = 1e+15
  86.             for y in SJF:
  87.                 if y[2] < SmallBurst:
  88.                     SmallBurst = y[2]
  89.                     Target = y
  90.                     check = False
  91.                     if Temp == None:
  92.                         Temp = Target
  93.             try:
  94.                 if Target != Temp and count > 0:
  95.                     print(Temp[0],time[0]-count,"->",time[0])
  96.                     gant = gant+("_"*int(count/2)+Temp[0]+"_"*int(count/2)+"|"+str(time[0]))
  97.                     count = 0
  98.                     Temp = Target
  99.             except:
  100.                 brr = 0
  101.         if Target != None:
  102.             Target[2] -= 1
  103.             Target[3] -= 1
  104.             time[0] += 1
  105.             count += 1
  106.  
  107.     Largest = 0
  108.     for x in Tasks:
  109.         if x[2] > Largest:
  110.             Largest = x[2]
  111.  
  112.     for x in range(Largest+1):
  113.         for y in Tasks:
  114.             if y[2] == x:
  115.                 NewTasks.append(y)
  116.  
  117.     for x in NewTasks:
  118.         if time[0] < x[1]:
  119.             gant = gant+("_"*(x[1]-time[0]))+"|"+str(x[1])
  120.             time[0] = x[1]
  121.         if Target[0] == x[0]:
  122.             temptime = time[0]
  123.             x[3] += (time[0]-x[1])
  124.             time[0] += x[2]
  125.             gant = gant+("_"*int(((x[2]+count)/2)))+x[0]+("_"*int(((x[2]+count)/2)))+"|"+str(time[0])
  126.             x[4] = time[0] - x[1]
  127.             print(x[0],time[0]-x[2]-count,"->",x[4]+x[1])
  128.         else:
  129.             temptime = time[0]
  130.             x[3] += (time[0]-x[1])
  131.             time[0] += x[2]
  132.             gant = gant+("_"*int((x[2]/2)))+x[0]+("_"*int((x[2]/2)))+"|"+str(time[0])
  133.             x[4] = time[0] - x[1]
  134.             print(x[0],time[0]-x[2],"->",x[4]+x[1])
  135.  
  136.     print()
  137.     print(gant)
  138.     print()
  139.  
  140.     print("Task\tArrival\tBurst\tWait\tTurnAround")
  141.     for i,x in enumerate(Tasks):
  142.         x[1] = OldTasks[i][1]
  143.         x[2] = OldTasks[i][2]
  144.         for y in x:
  145.             print(y,end=("\t"))
  146.         print()
  147.     awt = 0
  148.     atat = 0
  149.     num = 0
  150.     for x in Tasks:
  151.         awt = awt+x[3]
  152.         atat = atat+x[4]
  153.         num += 1
  154.     awt /= num
  155.     atat /= num
  156.     print("Average Wait Time: " + str(awt))
  157.     print("Average Turn Around Time: " + str(atat))
  158.  
  159. choice = input("What Process? \n 1. First Come First Serve \n 2. Shortest Job First\n")
  160.  
  161. if choice == "1":
  162.     FCFS()
  163. elif choice == "2":
  164.     SJF()
  165. else:
  166.     print("Invalid Option. Aborting Program")
  167.  
RAW Paste Data