Advertisement
JavierGumin

Disk Scheduling Algorithm

Jun 4th, 2025 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.62 KB | None | 0 0
  1. // CPU-Scheduling-Algorithm-In-C
  2. // Simulation of Disk Scheduling Algorithm -> FCFS, SSTF, SCAN
  3.  
  4. #include<stdio.h>
  5. #include <stdlib.h>
  6. int seektime = 0;
  7.  
  8. void fcfs(int sequence[], int head, int n)
  9. {              
  10.     int temp = head;
  11.     seektime = 0;
  12.     printf("\n The Disk sequence is : \n");
  13.     for(int i=0; i<n; i++)
  14.     {
  15.         printf(" > %d", sequence[i]);
  16.         seektime += abs(sequence[i]-temp);
  17.         temp = sequence[i];
  18.     }
  19.  
  20.     printf("\n\n Seek Time = %d", seektime);
  21. }
  22.  
  23.  
  24. void sstf(int sequence[], int head, int n)
  25. {
  26.     seektime = 0;
  27.     int arr[n], min, temp, i, j, pos;
  28.        
  29.     for(i=0; i<n; i++)
  30.     {
  31.         arr[i] = 0;
  32.     }
  33.    
  34.     while(1)
  35.     {
  36.         min = 999;
  37.         for(i=0; i<n; i++)
  38.         {
  39.             if(arr[i] == 0)
  40.             {
  41.                 if(min > abs(head - sequence[i]))
  42.                 {
  43.                     min = abs(head - sequence[i]);
  44.                     pos = i;
  45.                 }
  46.             }
  47.         }
  48.         if(min == 999)
  49.             break;
  50.         arr[pos] = 1;
  51.         seektime += min;
  52.         head = sequence[pos];
  53.         printf(" > %d", sequence[pos]);
  54.     }
  55.     printf("\n\n Seek Time = %d", seektime);
  56.    
  57. }
  58.  
  59. void scan(int sequence[], int head, int n, int t)
  60. {
  61.     int temp, i, j;
  62.     seektime = 0;
  63.  
  64.     printf("\n The Disk sequence is : \n");
  65.     for(i=0;i<n;i++)
  66.     {
  67.         for(j=i+1;j<n;j++)
  68.         {
  69.             if(sequence[i] > sequence[j])
  70.             {
  71.                 temp = sequence[i];
  72.                 sequence[i] = sequence[j];
  73.                 sequence[j] = temp;
  74.             }
  75.         }
  76.  
  77.     }  
  78.     temp = head;
  79.     for(i=0; i<n; i++)
  80.     {
  81.         if(sequence[i] > head)
  82.         {
  83.             printf(" > %d", sequence[i]);
  84.             seektime += abs(temp - sequence[i]);
  85.             temp = sequence[i];
  86.         }
  87.     }
  88.    
  89.    
  90.     seektime += abs(t-1 - sequence[i-1]);
  91.     temp = t-1;
  92.     for(i=n-1; i>=0; i--)
  93.     {
  94.         if(head > sequence[i])
  95.         {
  96.             printf(" > %d", sequence[i]);
  97.             seektime += abs(temp - sequence[i]);
  98.             temp = sequence[i];
  99.         }
  100.     }
  101.         printf("\n\n Seek Time = %d", seektime);
  102. }
  103.  
  104.  
  105. void main()
  106. {
  107.     int n, t, i, head, temp, choice;
  108.     printf("\n Enter number of disk request in queue : ");
  109.     scanf("%d", &n);
  110.     printf(" Enter total number of tracks : ");
  111.     scanf("%d", &t);
  112.     int sequence[n];
  113.     printf("\n Enter the disk request sequence for a disk with %d tracks : ", t);
  114.     for(i=0; i<n; i++)
  115.     {
  116.         scanf("%d", &sequence[i]);
  117.     }
  118.     printf(" Enter the initial position of the R/W head : ");
  119.     scanf("%d", &head);
  120.  
  121.        
  122.     do {
  123.         printf("\n-------------------------------------------------------------------------\n");
  124.         printf("\n\n 1. FCFS \n 2. SSTF \n 3. SCAN \n 4. EXIT \n");
  125.         printf("\n Enter your choice : ");
  126.         scanf("%d", &choice);
  127.         switch(choice)
  128.         {
  129.             case 1: fcfs(sequence, head, n);
  130.                 break;
  131.             case 2: sstf(sequence, head, n);
  132.                 break;
  133.             case 3: scan(sequence, head, n, t);
  134.                 break;
  135.             case 4: exit(0);
  136.                 break;
  137.         }
  138.     }while(1);
  139.  
  140.     printf("\n\n");
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement