Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Assignment: Project 4
- ## Date: 11/18/2017
- ## Course: CSC 431
- ## Professor: Dr. Bhattacharya
- ## Programmer: Josh Adams
- LOWER_CYLINDER = 0
- UPPER_CYLINDER = 4999
- SPEED = 1 # distance per unit time, where unit of distance = distance between cylinders of hard drive
- requests = [2069, 1212, 2296, 2800, 544, 1618, 356, 1523, 4965, 3681]
- deadlines = [2300, 2000, 80, 10, 1800, 7000, 3600, 8900, 97, 8800]
- initialPosition = 2500
- def SSTF_RT(requests, deadlines, initialPosition):
- localRequests = list(requests)
- request_deadline = dict(zip(requests, deadlines)) # combining the list of requests with deadlines into a dictionary
- position = initialPosition
- movement = 0
- time = 0
- while localRequests:
- #find the closest request to the current cylinder
- closest = abs(position - localRequests[0])
- closestIndex = 0
- for x in range(1, len(localRequests)):
- if abs(position - localRequests[x]) < closest:
- closest = abs(position - localRequests[x])
- closestIndex = x
- distance = abs(position - localRequests[closestIndex]) #how much to move
- if (time > request_deadline[localRequests[closestIndex]] or (distance > request_deadline[localRequests[closestIndex]]) or ((time+distance)>request_deadline[localRequests[closestIndex]])):
- print("Dropping service request ", localRequests[closestIndex], ",time =", time, sep="")
- del localRequests[closestIndex] # delete the element in localRequests that correspondes to closestIndex
- continue
- else:
- movement += distance # increment movement by itself and distance
- time += distance / SPEED # increment time by itself and (distance / SPEED)
- position = localRequests[closestIndex] # set position to the request that is processed
- print ("Servicing request " + str(position) + ",time = " + str(time), sep="")
- del localRequests[closestIndex] # delete the element in localRequests that correspondes to closestIndex
- return movement
- def EDF(requests, deadlines, initialPositions):
- localRequests = list(requests)
- request_deadline = dict(zip(requests, deadlines)) # combining the list of requests with deadlines into a dictionary
- position = initialPosition
- movement = 0
- time = 0
- sortedByValueDict = sorted(request_deadline.items(), key = lambda t: t[1]) # "sort" the dictionary by value and creating a new array of the "sorted" key:value combinations
- for i in sortedByValueDict: # loop through the elements in the sorted array of key:values
- distance = abs(position - i[0]) # set distance equal to the absolute value of the current position - the request
- if(i[0] in range(LOWER_CYLINDER,UPPER_CYLINDER)):
- if((distance > i[1]) or (time > i[1]) or ((time + distance) > i[1])): # check if the distance is greater than the deadline or time greater than the deadline, or time + distance greater than the deadline
- print("Dropping service request ", i[0], ",time = ", time, sep="")
- else:
- movement += distance # increment movement by itself and distance
- time += distance / SPEED # increment time by itself and (distance / SPEED)
- position = i[0] # set the current position to the request processed
- print("Servicing request ", i[0], ",time = ", time, sep ="")
- else:
- print("The given cylinder number of: ", i[0], " was greater than our cylinder range of: ",
- LOWER_CYLINDER, " : ", UPPER_CYLINDER, sep="")
- return movement
- print ("Total movement in SSTF_RT = " + str(SSTF_RT(requests, deadlines, initialPosition)), "\n")
- print ("Total movement in EDF = " + str(EDF(requests, deadlines, initialPosition)), "\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement