Advertisement
Guest User

Disk Scheduling Algorithms - Jonh Alexis Buot (LaplaceXD)

a guest
Dec 5th, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.13 KB | Source Code | 0 0
  1. # ===========================================================
  2. # Authors:  Jonh Alexis Buot
  3. # Github:   https://github.com/LaplaceXD
  4. # Date:     December 03, 2023
  5. # Course:   CS 3104 - Operating Systems
  6. # Activity: Disk Scheduling Algorithms
  7. # ===========================================================
  8.  
  9. def fcfs(tracks, track_bounds):
  10.     return tracks.copy()
  11.  
  12. def sstf(tracks, track_bounds):
  13.     tracks_buffer = tracks.copy()
  14.     sorted_tracks = [tracks_buffer.pop(0)]
  15.  
  16.     while len(tracks_buffer) > 0:
  17.         min_sstf_track_idx = min((i for i in range(len(tracks_buffer))), key=lambda i : abs(tracks_buffer[i] - sorted_tracks[-1]))
  18.         track = tracks_buffer.pop(min_sstf_track_idx)
  19.         sorted_tracks.append(track)
  20.  
  21.     return sorted_tracks
  22.  
  23. def scan(tracks, track_bounds):
  24.     higher_tracks = sorted(track for track in tracks if track >= tracks[0])
  25.     lower_tracks = sorted((track for track in tracks[1:] if track < tracks[0]), reverse=True)
  26.  
  27.     if len(lower_tracks) > 0 and track_bounds[1] not in higher_tracks:
  28.         higher_tracks.append(track_bounds[1])
  29.  
  30.     return higher_tracks + lower_tracks
  31.  
  32. def cscan(tracks, track_bounds):
  33.     higher_tracks = sorted(track for track in tracks if track >= tracks[0])
  34.     lower_tracks = sorted(track for track in tracks[1:] if track < tracks[0])
  35.  
  36.     if len(lower_tracks) > 0:
  37.         if track_bounds[1] not in higher_tracks:
  38.             higher_tracks.append(track_bounds[1])
  39.         if track_bounds[0] not in lower_tracks:
  40.             lower_tracks.insert(0, track_bounds[0])
  41.  
  42.     return higher_tracks + lower_tracks
  43.  
  44. def look(tracks, track_bounds):
  45.     higher_tracks = sorted(track for track in tracks if track >= tracks[0])
  46.     lower_tracks = sorted((track for track in tracks[1:] if track < tracks[0]), reverse=True)
  47.  
  48.     return higher_tracks + lower_tracks
  49.  
  50. def clook(tracks, track_bounds):
  51.     higher_tracks = sorted(track for track in tracks if track >= tracks[0])
  52.     lower_tracks = sorted(track for track in tracks[1:] if track < tracks[0])
  53.  
  54.     return higher_tracks + lower_tracks
  55.  
  56. def graph_it(tracks, track_bounds, title = "", print_calcs_to_console = False):
  57.     import matplotlib.pyplot as plt
  58.    
  59.     time = list(range(len(tracks)))
  60.     _, ax = plt.subplots()
  61.  
  62.     plt.plot(time, tracks, color="lightblue", linestyle="-", linewidth=2)
  63.     plt.scatter(time, tracks, color="blue", marker="o", clip_on=False, zorder=2)
  64.    
  65.     for i, track in enumerate(tracks):
  66.         ax.text(i, track + 7.5, track, color="blue", ha="center")
  67.    
  68.     plt.title(title)
  69.    
  70.     plt.xlabel("Time")
  71.     plt.xticks(time)
  72.     plt.xlim(0, time[-1])
  73.  
  74.     plt.ylabel("Track Number")
  75.     y_ticks = [i for i in range(0, track_bounds[1], 20)]
  76.     if track_bounds[1] not in y_ticks:
  77.         y_ticks.append(track_bounds[1])
  78.     plt.yticks(y_ticks)
  79.     plt.ylim(track_bounds[0], track_bounds[1])
  80.    
  81.     seek_pairs = [(max(curr_track, next_track), min(curr_track, next_track)) for curr_track, next_track in zip(tracks[:-1], tracks[1:])]
  82.     calculations = " + ".join(f"({curr} - {next})" for curr, next in seek_pairs)
  83.     total_seek_time = sum(curr - next for curr, next in seek_pairs)
  84.  
  85.     if print_calcs_to_console:
  86.         print(f"Total Seek Time = {calculations}")
  87.         print(f"Total Seek Time = {total_seek_time}")
  88.     else:
  89.         plt.figtext(0.125, -0.05, f"Total Seek Time = {calculations}", fontsize = 10, ha="left", va="bottom")
  90.         plt.figtext(0.125, -0.1, f"Total Seek Time = {total_seek_time}", fontsize = 10, ha="left", va="bottom")
  91.    
  92.     plt.grid(True)
  93.     plt.show()
  94.  
  95. # Configurations
  96. num_cylinders = 200
  97. starting_point = 55
  98. tracks = [96, 120, 156, 80, 130, 9, 48, 32, 50, 12, 2]
  99.  
  100. # Other Configurations, can leave as is
  101. track_bounds = (0, num_cylinders - 1)
  102. tracks.insert(0, starting_point)
  103.  
  104. disk_schedulers = [
  105.     ("First-Come-First-Serve (FCFS)", fcfs),
  106.     ("Shortest Seek Time First", sstf),
  107.     ("SCAN", scan),
  108.     ("C-SCAN", cscan),
  109.     ("LOOK", look),
  110.     ("C-LOOK", clook),
  111. ]
  112.  
  113. for name, fn in disk_schedulers:
  114.     sorted_tracks = fn(tracks, track_bounds)
  115.     graph_it(sorted_tracks, track_bounds, title=name, print_calcs_to_console=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement