Advertisement
Bewin

disk

Nov 13th, 2024 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define up_bound 200 // Upper Bound
  5. #define low_bound 0 // Lower Bound
  6.  
  7. int n, head; //n -> number of tracks, head -> starting position
  8.  
  9. void sort(int arr[], int size) { // Bubble Sort -> Ascending order
  10.     for (int i = 0; i < size - 1; ++i) {
  11.         for (int j = 0; j < size - i - 1; ++j) {
  12.             if (arr[j] > arr[j + 1]) {
  13.                 int temp = arr[j];
  14.                 arr[j] = arr[j + 1];
  15.                 arr[j + 1] = temp;
  16.             }
  17.         }
  18.     }
  19. }
  20.  
  21. int findHead(int tracks[]) { // Find index of head from sorted array tracks[n+3]
  22.     int position = 1;
  23.     for (int i = 0; i < n + 3; ++i) {
  24.         if (tracks[i] == head) {
  25.             position = i;
  26.             break;
  27.         }
  28.     }
  29.     return position;
  30. }
  31.  
  32. void FCFS(int tracks[]) { //Requests are processed in the order they arrive
  33.     int sum = 0, current = head, new;  /* sum -> Total seek time
  34.                                         * current -> to track the previously processed request
  35.                                         * new -> absolute value of difference b/w previous and current request
  36.                                         */
  37.     printf("\nTraversed Order : { %d =>", head);
  38.     for (int i = 0; i < n; ++i) {
  39.         if (i < n - 1)
  40.             printf(" %d =>", tracks[i]);
  41.         else
  42.             printf(" %d }", tracks[i]);
  43.         new = current > tracks[i] ? current - tracks[i] : tracks[i] - current; /* if(current>head)
  44.                                                                                     current - tracks[i]
  45.                                                                                   else
  46.                                                                                     tracks[i] - current */
  47.         current = tracks[i];
  48.         sum += new; // sum = sum + new
  49.     }
  50.     printf("\nTotal Head Movement = %d\n", sum);
  51. }
  52.  
  53. void SCAN(int tracks[]) { /*moves the disk arm in one direction (either left or right) until it reaches the
  54.                             end of the disk, When it reaches the end, it reverses direction and continues.*/
  55.     int sum = 0, new, current; /* sum -> Total seek time
  56.                                 * current -> to track the previously processed request
  57.                                 * new -> absolute value of difference b/w previous and current request
  58.                                 */
  59.  
  60.     tracks[n] = head; // insert head, upper_bound and lower_bound to the sequence
  61.     tracks[n + 1] = low_bound;
  62.     tracks[n + 2] = up_bound;
  63.  
  64.     sort(tracks, n + 3); // sort array including the newly inserted values
  65.     int head_pos = findHead(tracks); // find the index of head from the sorted array
  66.  
  67.     printf("\nTraversed Order : { %d =>", tracks[head_pos]);
  68.     current = tracks[head_pos];
  69.  
  70.     for (int i = head_pos + 1; i < n + 3; ++i) { // Loop to calculate disk movement from head to upper_bound
  71.         printf(" %d =>", tracks[i]);
  72.         new = tracks[i] - current;
  73.         current = tracks[i];
  74.         sum += new;
  75.     }
  76.     for (int i = head_pos - 1; i > 0; i--) { //Loop to calculate disk movement from upper_bound to the least request
  77.         if (i > 1)
  78.             printf(" %d =>", tracks[i]);
  79.         else
  80.             printf(" %d }", tracks[i]);
  81.         new = current - tracks[i];
  82.         current = tracks[i];
  83.         sum += new;
  84.     }
  85.     printf("\nTotal Head Movement = %d\n", sum);
  86.  
  87. }
  88.  
  89. void C_SCAN(int tracks[]) { /*Moves the disk arm in one direction (either left or right) until it reaches the
  90.                             end of the disk, As soon as it reaches the other end, it immediately returns to the
  91.                             beginning without servicing any requests, It then starts servicing requests again once
  92.                             it reaches the beginning.*/
  93.     int sum = 0, new, current;
  94.     tracks[n] = head;
  95.     tracks[n + 1] = low_bound;
  96.     tracks[n + 2] = up_bound;
  97.     sort(tracks, n + 3);
  98.     int head_pos = findHead(tracks);
  99.  
  100.     printf("\nTraversed Order: { %d =>", tracks[head_pos]);
  101.     current = tracks[head_pos];
  102.  
  103.     for (int i = head_pos + 1; i < n + 3; ++i) {
  104.         printf(" %d =>", tracks[i]);
  105.         new = tracks[i] - current;
  106.         current = tracks[i];
  107.         sum += new;
  108.     }
  109.     sum += current;
  110.     current = tracks[0];
  111.     for (int i = 0; i < head_pos; ++i) {
  112.         if (i < head_pos - 1)
  113.             printf(" %d =>", tracks[i]);
  114.         else
  115.             printf(" %d }", tracks[i]);
  116.         new = tracks[i] - current;
  117.         current = tracks[i];
  118.         sum += new;
  119.     }
  120.     printf("\nTotal Head Movement = %d\n", sum);
  121.  
  122. }
  123.  
  124. int main() {
  125.     while (1) {
  126.         /////////////////////////////////////////////////
  127.         printf("\nEnter number of tracks:");
  128.         scanf("%d", &n);
  129.         printf("Enter head position:");
  130.         scanf("%d", &head);
  131.         int tracks[n + 3]; // 3 index extra for inserting head, lower_bound and upper-bound
  132.         printf("Enter %d sequence of tracks: ", n);
  133.         for (int i = 0; i < n; ++i) {
  134.             scanf("%d", &tracks[i]);
  135.         }
  136.         /////////////////////////////////////////////////
  137.         int ch;
  138.         printf("\nEnter your choice:\n1. FCFS\n2. SCAN\n3. C-Scan\n0. Exit\n");
  139.         scanf("%d", &ch);
  140.         switch (ch) {
  141.             case 1:
  142.                 FCFS(tracks);
  143.                 break;
  144.             case 2:
  145.                 SCAN(tracks);
  146.                 break;
  147.             case 3:
  148.                 C_SCAN(tracks);
  149.                 break;
  150.             case 0:
  151.                 exit(0);
  152.             default:
  153.                 printf("Invalid Argument!");
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement