Advertisement
Bewin

Disk Scheduling(L/R)

Jul 13th, 2024 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.30 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.     char arg; // arg -> To choose left or Right Seeking
  56.  
  57.     printf("\nTraverse Left or Right (l/r):");
  58.     scanf(" %c", &arg);
  59.  
  60.     int sum = 0, new, current; /* sum -> Total seek time
  61.                                 * current -> to track the previously processed request
  62.                                 * new -> absolute value of difference b/w previous and current request
  63.                                 */
  64.  
  65.     tracks[n] = head; // insert head, upper_bound and lower_bound to the sequence
  66.     tracks[n + 1] = low_bound;
  67.     tracks[n + 2] = up_bound;
  68.  
  69.     sort(tracks, n + 3); // sort array including the newly inserted values
  70.     int head_pos = findHead(tracks); // find the index of head from the sorted array
  71.  
  72.     printf("\nTraversed Order : { %d =>", tracks[head_pos]);
  73.     current = tracks[head_pos];
  74.  
  75.     if (arg == 'l') { //Moves in Left Direction
  76.         for (int i = head_pos - 1; i >= 0; i--) { // Loop to calculate disk movement from head to lower_bound
  77.             printf(" %d =>", tracks[i]);
  78.             new = current - tracks[i];
  79.             current = tracks[i];
  80.             sum += new;
  81.         }
  82.         for (int i = head_pos + 1;
  83.              i < n + 2; ++i) { //Loop to calculate disk movement from lower_bound to greater request
  84.             if (i < n + 1)
  85.                 printf(" %d =>", tracks[i]);
  86.             else
  87.                 printf(" %d }", tracks[i]);
  88.             new = tracks[i] - current;
  89.             current = tracks[i];
  90.             sum += new;
  91.         }
  92.     } else if (arg == 'r') { // Moves in Right Direction
  93.         for (int i = head_pos + 1; i < n + 3; ++i) { // Loop to calculate disk movement from head to upper_bound
  94.             printf(" %d =>", tracks[i]);
  95.             new = tracks[i] - current;
  96.             current = tracks[i];
  97.             sum += new;
  98.         }
  99.         for (int i = head_pos - 1; i > 0; i--) { //Loop to calculate disk movement from upper_bound to the least request
  100.             if (i > 1)
  101.                 printf(" %d =>", tracks[i]);
  102.             else
  103.                 printf(" %d }", tracks[i]);
  104.             new = current - tracks[i];
  105.             current = tracks[i];
  106.             sum += new;
  107.         }
  108.     }
  109.     printf("\nTotal Head Movement = %d\n", sum);
  110.  
  111. }
  112.  
  113. void C_SCAN(int tracks[]) { /*Moves the disk arm in one direction (either left or right) until it reaches the
  114.                             end of the disk, As soon as it reaches the other end, it immediately returns to the
  115.                             beginning without servicing any requests, It then starts servicing requests again once
  116.                             it reaches the beginning.*/
  117.     char arg;
  118.     printf("\nTraverse Left or Right (l/r):");
  119.     scanf(" %c", &arg);
  120.     int sum = 0, new, current;
  121.     tracks[n] = head;
  122.     tracks[n + 1] = low_bound;
  123.     tracks[n + 2] = up_bound;
  124.     sort(tracks, n + 3);
  125.     int head_pos = findHead(tracks);
  126.  
  127.     printf("\nTraversed Order: { %d =>", tracks[head_pos]);
  128.     current = tracks[head_pos];
  129.     if (arg == 'l') {
  130.         for (int i = head_pos - 1; i >= 0; i--) {
  131.             printf(" %d =>", tracks[i]);
  132.             new = current - tracks[i];
  133.             current = tracks[i];
  134.             sum += new;
  135.         }
  136.         current = tracks[n + 2];
  137.         sum += current;
  138.         for (int i = n + 2; i > head_pos; i--) {
  139.             if (i > head_pos + 1)
  140.                 printf(" %d =>", tracks[i]);
  141.             else
  142.                 printf(" %d }", tracks[i]);
  143.             new = current - tracks[i];
  144.             current = tracks[i];
  145.             sum += new;
  146.         }
  147.     } else if (arg == 'r') {
  148.         for (int i = head_pos + 1; i < n + 3; ++i) {
  149.             printf(" %d =>", tracks[i]);
  150.             new = tracks[i] - current;
  151.             current = tracks[i];
  152.             sum += new;
  153.         }
  154.         sum += current;
  155.         current = tracks[0];
  156.         for (int i = 0; i < head_pos; ++i) {
  157.             if (i < head_pos - 1)
  158.                 printf(" %d =>", tracks[i]);
  159.             else
  160.                 printf(" %d }", tracks[i]);
  161.             new = tracks[i] - current;
  162.             current = tracks[i];
  163.             sum += new;
  164.         }
  165.     }
  166.     printf("\nTotal Head Movement = %d\n", sum);
  167.  
  168. }
  169.  
  170. int main() {
  171.     while (1) {
  172.         /////////////////////////////////////////////////
  173.         printf("\nEnter number of tracks:");
  174.         scanf("%d", &n);
  175.         printf("Enter head position:");
  176.         scanf("%d", &head);
  177.         int tracks[n + 3]; // 3 index extra for inserting head, lower_bound and upper-bound
  178.         printf("Enter %d sequence of tracks: ", n);
  179.         for (int i = 0; i < n; ++i) {
  180.             scanf("%d", &tracks[i]);
  181.         }
  182.         /////////////////////////////////////////////////
  183.         int ch;
  184.         printf("\nEnter your choice:\n1. FCFS\n2. SCAN\n3. C-Scan\n0. Exit\n");
  185.         scanf("%d", &ch);
  186.         switch (ch) {
  187.             case 1:
  188.                 FCFS(tracks);
  189.                 break;
  190.             case 2:
  191.                 SCAN(tracks);
  192.                 break;
  193.             case 3:
  194.                 C_SCAN(tracks);
  195.                 break;
  196.             case 0:
  197.                 exit(0);
  198.             default:
  199.                 printf("Invalid Argument!");
  200.         }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement