 # FCFS/SJF

Jun 17th, 2021 (edited)
777
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import copy
3. time =   #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: "))
13.         #0 is name, 1 is arrival, 2 is burst
14.         #3 is wait, 4 is turnaround
15. except:
16.     print("invalid input")
17.
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 > Largest:
28.             Largest = x
29.     for x in range(Largest+1)#ordering the array from smallest to highest arrival time
31.             if y == x:
33.
34.     gant = "|0" #gant chart, changed during processing
36.         if time < x#incase all arrival times are above 0
37.             gant = gant+("_"*(x-time))+"|"+str(x)
38.             time = x
39.         x = time-x #waittime = current time - arrival time
40.         time += x #current time += burst time to change time and finish
41.         gant = gant+("_"*int((x/2)))+x+("_"*int((x/2)))+"|"+str(time)   #Edit the gant chart
42.         x = time - x   #turn around time = current time - arrival time after burst
43.         print(x,time-x,"->",x+x) #print out the before and after
44.     print()
45.     print(gant) #print the gant chart
46.     print()
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
57.         atat = atat+x
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
66.         if x > Largest:
67.             Largest = x
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):
78.             if y == x:
79.                 check = True
80.                 SJF.append(y)
81.                 if time < y:
82.                     gant = gant+("_"*(y-time))+"|"+str(y)
83.                     time = y
84.         if check:
85.             SmallBurst = 1e+15
86.             for y in SJF:
87.                 if y < SmallBurst:
88.                     SmallBurst = y
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,time-count,"->",time)
96.                     gant = gant+("_"*int(count/2)+Temp+"_"*int(count/2)+"|"+str(time))
97.                     count = 0
98.                     Temp = Target
99.             except:
100.                 brr = 0
101.         if Target != None:
102.             Target -= 1
103.             Target -= 1
104.             time += 1
105.             count += 1
106.
107.     Largest = 0
109.         if x > Largest:
110.             Largest = x
111.
112.     for x in range(Largest+1):
114.             if y == x:
116.
118.         if time < x:
119.             gant = gant+("_"*(x-time))+"|"+str(x)
120.             time = x
121.         if Target == x:
122.             temptime = time
123.             x += (time-x)
124.             time += x
125.             gant = gant+("_"*int(((x+count)/2)))+x+("_"*int(((x+count)/2)))+"|"+str(time)
126.             x = time - x
127.             print(x,time-x-count,"->",x+x)
128.         else:
129.             temptime = time
130.             x += (time-x)
131.             time += x
132.             gant = gant+("_"*int((x/2)))+x+("_"*int((x/2)))+"|"+str(time)
133.             x = time - x
134.             print(x,time-x,"->",x+x)
135.
136.     print()
137.     print(gant)
138.     print()
139.
144.         for y in x:
145.             print(y,end=("\t"))
146.         print()
147.     awt = 0
148.     atat = 0
149.     num = 0
151.         awt = awt+x
152.         atat = atat+x
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