DISK SCHEDULING

DATE: 15/11/2020

AIM

To implement disk scheduling using a menu driven program.

Algorithm

  1. Start
  2. While
  3. Display
    1. FCFS
    2. SCAN
    3. C-SCAN
  4. Read choice
  5. If choice = 1
    1. read maximum range, max
    2. read initial head position, head
    3. read size of queue request, n
    4. read queue of disk positions, queue[i]
    5. diff=abs(queue[j+1]-queue[j])
    6. seek+=diff
    7. print total seek time, seek
    8. print average seek time, avg
  6. else If choice=2
    1. read maximum range, max

    2. read initial head position, head

    3. read size of queue request, n

    4. read queue of disk positions, temp

    5. if(temp>=head)

      1. queue1[temp1]=temp
      2. temp1++
    6. else

      1. queue2[temp2]=temp
      2. temp2++
    7. if(queue[i]>queue1[j])

      1. temp=queue1[i]
      2. queue1[i]=queue1[j]
      3. queue1[j]=temp
    8. if(queue2[i]<queue2[j])

      1. temp=queue2[i]
      2. queue2[i]=queue2[j]
      3. queue2[j]=temp
    9. print total seek time, seek

    10. print average seek time, avg

  7. else if choice=3
    1. read maximum range, max
    2. read initial head position, head
    3. read size of queue, n
    4. read queue of disk positions, temp
    5. if(temp>=head)
      1. queue1[temp1]=temp
      2. temp1++
    6. else
      1. queue2[temp2]=temp
      2. temp2++
    7. if(queue1[i]>queue1[j])
      1. temp=queue1[i]
      2. queue1[i]=queue1[j]
      3. queue1[j]=temp
    8. if(queue2[i]>queue2[j])
      1. temp=queue2[i]
      2. queue2[i]=queue2[j]
      3. queue2[j]=temp
    9. print total seek time, seek
    10. print average seek time, avg
  8. default display invalid choice
  9. stop

Program

#include <stdio.h>
#include<stdlib.h>
int fcfs();
int scan();
int cscan();
int main() {
  int n;
  while (1) {
    printf("\\n\\nwhich algo do u want to use?");
    printf("\\n1) FCFS\\n2) SCAN\\n3) CSCAN\\n4) EXIT\\n");
    scanf("%d", & n);
    switch (n) {
    case 1:
      fcfs();
      break;
    case 2:
      scan();
      break;
    case 3:
      cscan();
      break;
    case 4:
      exit(0);
      break;
    default:
      printf("Error!\\n");
      printf("\\n");
    }
  }
  return 0;
}
int fcfs() {
  int queue[20], n, head, i, j, k, seek = 0, max, diff;
  float avg;
  printf("Enter the max range of disk\\n");
  scanf("%d", & max);
  printf("Enter the initial head position\\n");
  scanf("%d", & head);
  printf("Enter the size of queue request\\n");
  scanf("%d", & n);
  printf("Enter the queue of disk positions to be read\\n");
  for (i = 1; i <= n; i++)
    scanf("%d", & queue[i]);
  queue[0] = head;
  for (j = 0; j <= n - 1; j++) {
    diff = abs(queue[j + 1] - queue[j]);
    seek += diff;

    printf("Disk head moves from %d to %d with seek %d\\n", queue[j], queue[j + 1], diff);
  }
  printf("Total seek time is %d\\n", seek);
  avg = seek / (float) n;
  printf("Average seek time is %f\\n", avg);
  printf("\\n");
  return 0;
}
int scan() {
    int queue[20], n, head, i, j, k, seek = 0, max, diff, temp, queue1[20], queue2[2 0],
      temp1 = 0, temp2 = 0;
    float avg;
    printf("Enter the max range of disk\\n");
    scanf("%d", & max);
    printf("Enter the initial head position\\n");
    scanf("%d", & head);
    printf("Enter the size of queue request\\n");
    scanf("%d", & n);
    printf("Enter the queue of disk positions to be read\\n");
    for (i = 1; i <= n; i++) {
      scanf("%d", & temp);
      if (temp >= head) {
        queue1[temp1] = temp;
        temp1++;

      } else {
        temp2++;
      }
    }
    for (i = 0; i < temp1 - 1; i++) {
      for (j = i + 1; j < temp1; j++) {
        if (queue1[i] > queue1[j]) {}
      }
    }
    for (i = 0; i < temp2 - 1; i++) {
      queue2[temp2] = temp;
      for (j = i + 1; j < temp2; j++) {
        if (queue2[i] < queue2[j]) {}
      }
      temp = queue1[i];
      queue1[i] = queue1[j];
      queue1[j] = temp;
      temp = queue2[i];
      queue2[i] = queue2[j];
      queue2[j] = temp;

    }
    for (i = 1, j = 0; j < temp1; i++, j++) queue[i] = queue1[j];
    queue[i] = max - 1;
    for (i = temp1 + 2, j = 0; j < temp2; i++, j++) queue[i] = queue2[j];
    queue[i] = 0;
    queue[0] = head;
    for (j = 0; j <= n; j++) {
      diff = abs(queue[j + 1] - queue[j]);
      seek += diff;
      printf("Disk head moves from %d to %d with seek %
        d\\ n ",queue[j],queue[j+1],diff); }
        printf("Total seek time is %d\\n", seek); avg = seek / (float) n; printf("Average seek time is %f\\n", avg); printf("\\n");
        return 0;
      }
      int cscan() {
          int queue[20], n, head, i, j, k, seek = 0, max, diff, temp, queue1[20], queue2[2 0],
            temp1 = 0, temp2 = 0;
          float avg;
          printf("Enter the max range of disk\\n");
          scanf("%d", & max);

          printf("Enter the initial head position\\n");
          scanf("%d", & head);
          printf("Enter the size of queue request\\n");
          scanf("%d", & n);
          printf("Enter the queue of disk positions to be read\\n");
          for (i = 1; i <= n; i++) {
            scanf("%d", & temp);
            if (temp >= head) {
              queue1[temp1] = temp;
              temp1++;
            } else {
              temp2++;
            }
          }
          for (i = 0; i < temp1 - 1; i++) {
            for (j = i + 1; j < temp1; j++) {
              if (queue1[i] > queue1[j]) {}
            }
            queue2[temp2] = temp;
            temp = queue1[i];
            queue1[i] = queue1[j];
            queue1[j] = temp;

          }
          for (i = 0; i < temp2 - 1; i++) {
            for (j = i + 1; j < temp2; j++) {
              if (queue2[i] > queue2[j]) {
                temp = queue2[i];
                queue2[i] = queue2[j];
                queue2[j] = temp;
              }
            }
          }
          for (i = 1, j = 0; j < temp1; i++, j++) queue[i] = queue1[j];
          queue[i] = max - 1;
          queue[i + 1] = 0;
          for (i = temp1 + 3, j = 0; j < temp2; i++, j++) queue[i] = queue2[j];
          queue[0] = head;
          for (j = 0; j <= n + 1; j++) {
            diff = abs(queue[j + 1] - queue[j]);
            seek += diff;
            printf("Disk head moves from %d to %d with seek %
              d\\ n ",queue[j],queue[j+1],diff); }
              printf("Total seek time is %d\\n", seek); avg = seek / (float) n; printf("Average seek time is %f\\n", avg);

              printf("\\n");
              return 0;
            }

Output

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/cd7d4f06-03a8-4f2a-8429-7132983569d6/Screen_Shot_2020-12-04_at_11.37.23_AM.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/037d1c92-3259-456d-8ef2-9ddf837bd44c/Screen_Shot_2020-12-04_at_11.38.03_AM.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/4f0c5c68-ef0b-41b1-9999-bde6ec1a5bc7/Screen_Shot_2020-12-04_at_11.38.13_AM.png