Advertisement
Guest User

Project 4

a guest
Nov 20th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. ## Assignment: Project 4
  2. ## Date: 11/18/2017
  3. ## Course: CSC 431
  4. ## Professor: Dr. Bhattacharya
  5. ## Programmer: Josh Adams
  6.  
  7.  
  8. LOWER_CYLINDER = 0
  9. UPPER_CYLINDER = 4999
  10. SPEED = 1 # distance per unit time, where unit of distance = distance between cylinders of hard drive
  11.  
  12. requests = [2069, 1212, 2296, 2800, 544, 1618, 356, 1523, 4965, 3681]
  13. deadlines = [2300, 2000, 80, 10, 1800, 7000, 3600, 8900, 97, 8800]
  14. initialPosition = 2500
  15.  
  16. def SSTF_RT(requests, deadlines, initialPosition):
  17. localRequests = list(requests)
  18. request_deadline = dict(zip(requests, deadlines)) # combining the list of requests with deadlines into a dictionary
  19. position = initialPosition
  20. movement = 0
  21. time = 0
  22. while localRequests:
  23. #find the closest request to the current cylinder
  24. closest = abs(position - localRequests[0])
  25. closestIndex = 0
  26. for x in range(1, len(localRequests)):
  27. if abs(position - localRequests[x]) < closest:
  28. closest = abs(position - localRequests[x])
  29. closestIndex = x
  30.  
  31. distance = abs(position - localRequests[closestIndex]) #how much to move
  32.  
  33. if (time > request_deadline[localRequests[closestIndex]] or (distance > request_deadline[localRequests[closestIndex]]) or ((time+distance)>request_deadline[localRequests[closestIndex]])):
  34. print("Dropping service request ", localRequests[closestIndex], ",time =", time, sep="")
  35. del localRequests[closestIndex] # delete the element in localRequests that correspondes to closestIndex
  36. continue
  37. else:
  38. movement += distance # increment movement by itself and distance
  39. time += distance / SPEED # increment time by itself and (distance / SPEED)
  40. position = localRequests[closestIndex] # set position to the request that is processed
  41. print ("Servicing request " + str(position) + ",time = " + str(time), sep="")
  42. del localRequests[closestIndex] # delete the element in localRequests that correspondes to closestIndex
  43.  
  44.  
  45. return movement
  46.  
  47.  
  48. def EDF(requests, deadlines, initialPositions):
  49. localRequests = list(requests)
  50. request_deadline = dict(zip(requests, deadlines)) # combining the list of requests with deadlines into a dictionary
  51. position = initialPosition
  52. movement = 0
  53. time = 0
  54. 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
  55. for i in sortedByValueDict: # loop through the elements in the sorted array of key:values
  56. distance = abs(position - i[0]) # set distance equal to the absolute value of the current position - the request
  57. if(i[0] in range(LOWER_CYLINDER,UPPER_CYLINDER)):
  58. 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
  59. print("Dropping service request ", i[0], ",time = ", time, sep="")
  60. else:
  61. movement += distance # increment movement by itself and distance
  62. time += distance / SPEED # increment time by itself and (distance / SPEED)
  63. position = i[0] # set the current position to the request processed
  64. print("Servicing request ", i[0], ",time = ", time, sep ="")
  65. else:
  66. print("The given cylinder number of: ", i[0], " was greater than our cylinder range of: ",
  67. LOWER_CYLINDER, " : ", UPPER_CYLINDER, sep="")
  68. return movement
  69.  
  70.  
  71.  
  72. print ("Total movement in SSTF_RT = " + str(SSTF_RT(requests, deadlines, initialPosition)), "\n")
  73. print ("Total movement in EDF = " + str(EDF(requests, deadlines, initialPosition)), "\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement