Question: Can you fix the errors? It isn't working when I try to run python3 batchSchedulingComparison.py batchfile.txt ShortestRemaining or python3 batchSchedulingComparison.py batchfile.txt RoundRobin import sys from
Can you fix the errors? It isn't working when I try to run
python3 batchSchedulingComparison.py batchfile.txt ShortestRemaining or
python3 batchSchedulingComparison.py batchfile.txt RoundRobin
import sys
from os import path
# main() function
def main():
order = []
completion = []
if len(sys.argv) == 3:
filename = sys.argv[1]
status = path.isfile(filename)
if (status == False):
print("Input batchfile not found!")
return 1
elif (status == True):
with open(filename) as f:
array = []
for line in f: # read rest of lines
array.append([int(x) for x in line.split(', ')])
f.close()
n = len(array)
burst = []
for i in range(n):
burst.append(array[i][2])
arrival = []
for i in range(n):
arrival.append(array[i][1])
avgw = 0
avgt = 0
turnaround = []
wait = []
if (sys.argv[2] == "ShortestRemaining"):
order,completion = ShortestRemainingSort(array)
print("PID ORDER OF EXECUTION: ")
for g in range(len(order)):
print(order[g][0])
avgw , avgt , wait , turnaround = ComputeStat(completion , arrival, burst)
print(" Average Process Turnaround Time:", avgt)
print("Average Process Wait Time:", avgw)
elif (sys.argv[2] == "RoundRobin"):
order,completion = RoundRobinSort(array)
print("PID ORDER OF EXECUTION: ")
for g in range(len(order)):
print(order[g][0])
avgw , avgt , wait , turnaround = ComputeStat(completion , arrival, burst)
print(" Average Process Turnaround Time:", avgt)
print("Average Process Wait Time:" , avgw)
else :
print("Unidentified sorting algorithm. Please input either ShortestRemaining or RoundRobinSort.")
return 1
else:
print("Please provide command line arguments when running. python3 batchSchedulingComparison.py batchfile.txt RoundRobinSort")
return 1
def ComputeStat(processCompletionTimes, processArrivalTimes, processBurstTimes):
n = len(processBurstTimes)
add1 = 0
add2 = 0
averagewait = 0
averagturnaround = 0
turnaround = []
wait = []
for i in range(n):
turnaround.append(processCompletionTimes[i] - processArrivalTimes[i])
add1 += turnaround[i]
wait.append(turnaround[i] - processBurstTimes[i])
add2 += wait[i]
averagturnaround = add1 / n
averagewait = add2 / n
return averagewait , averagturnaround , wait , turnaround
if __name__=="__main__":
main()
def ShortestRemainingSort(batchFileData):
current_time = 0
completed = 0
complete = []
n = len(batchFileData)
burst = []
for i in range(n):
burst.append(batchFileData[i][2])
completion_time = [0] * n
prev = 0
is_completed = [0] * n
while (completed != n):
idx = -1
mn = 10000000
for i in range(n):
if(batchFileData[i][1] <= current_time and is_completed[i] == 0):
if(burst[i] < mn):
mn = burst[i]
idx = i
if (burst[i] == mn):
if(batchFileData[i][1] < batchFileData[idx][1]):
mn = burst[i]
idx = i
if(idx != -1):
complete.append(batchFileData[idx])
burst[idx] -= 1
current_time += 1
if(burst[idx] == 0):
completion_time[idx] = current_time
is_completed[idx] = 1
completed +=1
else:
current_time +=1
PID = []
prev = 0;
for i in range(len(complete)):
if(complete[i][0] != prev):
prev = complete[i][0]
PID.append(complete[i])
return PID, completion_time
def RoundRobinSort(batchFileData):
queue = []
current_time = 0
waiting_times = {}
completion_times = {}
time_quantum = 10
for process in batchFileData:
waiting_times[process[0]] = 0
completion_times[process[0]] = 0
while len(batchFileData) > 0 or len(queue) > 0:
for process in batchFileData:
if process[1] <= current_time:
queue.append(process)
batchFileData.remove(process)
if len(queue) > 0:
process = queue.pop(0)
if process[2] > time_quantum:
current_time += time_quantum
process[2] -= time_quantum
queue.append(process)
else:
current_time += process[2]
process[2] = 0
completion_times[process[0]] = current_time
waiting_times[process[0]] = current_time - process[1]
else:
current_time += 1
return waiting_times, completion_times
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
