0 votes
84 views
in Operating System by (420 points)
edited
Write a program to implement Disk Scheduling algorithms  like FCFS, SCAN and C- SCAN.

1 Answer

0 votes
by (98.9k points)
selected by
 
Best answer

FCFS Disk Scheduling Algorithm: 

 

#include<stdio.h>
#include<stdlib.h>
 
int main()
{
    int queue[20], n, head, i, seek = 0;
    float avg;
 
    printf("Enter the size of Queue : ");
    scanf("%d", &n);
 
    printf("Enter the Queue : ");
    for(i=1;i<=n;i++)
        scanf("%d",&queue[i]);
 
    printf("Enter the initial head position : ");
    scanf("%d", &head);
 
    queue[0] = head;
 
    for(i=0;i<n;i++)
    {
        seek += abs(queue[i+1] - queue[i]);
        printf("Disk head moves from %d to %d with seek %d\n",queue[i],queue[i+1],abs(queue[i+1] - queue[i]));
    }
 
    printf("Total seek time = %d\n", seek);
    avg = seek/(float)n;
    printf("Average seek time = %f\n", avg);
 
    return 0;
}

 

 

 

 

SCAN Disk Scheduling Algorithm:

 

 

 



#include<stdio.h>
#include<stdlib.h>
 
int main()
{
    int queue[20], n, head, i, j, k, seek = 0, max, diff, temp, queue1[20], queue2[20], temp1 = 0, temp2 = 0;
    float avg;
 
    printf("Enter the size of Queue : ");
    scanf("%d", &n);
 
    printf("Enter the Queue : ");
    for(i=1;i<=n;i++)
        scanf("%d",&queue[i]);
 
    printf("Enter the initial head position : ");
    scanf("%d", &head);
 
    queue[0] = head;
 
    for(i=0;i<n;i++)
    {
        max = 0;
        for(j=i+1;j<=n;j++)
        {
            if(queue[i]>=queue[j])
                diff = queue[i]-queue[j];
            else
                diff = queue[j]-queue[i];
            if(diff>max)
            {
                max = diff;
                temp = j;
            }
        }
        queue1[temp1] = queue[temp];
        temp1++;
        for(k=temp;k<=n-1;k++)
        {
            queue[k] = queue[k+1];
        }
        n--;
    }
 
    queue2[temp2] = head;
    for(i=0;i<temp1-1;i++)
    {
        queue2[i+1] = queue1[i];
    }
 
    for(i=0;i<temp1;i++)
    {
        seek += abs(queue2[i+1] - queue2[i]);
        printf("Disk head moves from %d to %d with seek %d\n",queue2[i],queue2[i+1],abs(queue2[i+1] - queue2[i]));
    }
 
    printf("Total seek time = %d\n", seek);
    avg = seek/(float)temp1;
    printf("Average seek time = %f\n", avg);
 
    return 0;
}

 

 

 

 

 

 

 

C-SCAN Disk Scheduling Algorithm:  

 







#include<stdio.h>
#include<stdlib.h>
 
int main()
{
    int queue[20], n, head, i, j, k, seek = 0, max, diff, temp, queue1[20], queue2[20], temp1 = 0, temp2 = 0;
    float avg;
 
    printf("Enter the size of Queue : ");
    scanf("%d", &n);
 
    printf("Enter the Queue : ");
    for(i=1;i<=n;i++)
        scanf("%d",&queue[i]);
 
    printf("Enter the initial head position : ");
    scanf("%d", &head);
 
    queue[0] = head;
 
    for(i=0;i<=n;i++)
    {
        for(j=i+1;j<=n;j++)
        {
            if(queue[i]>queue[j])
            {
                temp = queue[i];
                queue[i] = queue[j];
                queue[j] = temp;
            }
        }
    }
 
    max = queue[n];
 
    for(i=head;i<=max;i++)
    {
        queue1[temp1] = queue[i];
        temp1++;
    }
 
    queue1[temp1] = max;
    temp1++;
 
    for(i=max-1;i>=0;i--)
    {
        queue2[temp2] = queue[i];
        temp2++;
    }
 
    queue2[temp2] = 0;
    temp2++;
 
    seek = head + max;
 
    for(i=0;i<temp1-1;i++)
    {
        seek += abs(queue1[i+1] - queue1[i]);
        printf("Disk head moves from %d to %d with seek %d\n",queue1[i],queue1[i+1],abs(queue1[i+1] - queue1[i]));
    }
 
    for(i=0;i<temp2-1;i++)
    {
        seek += abs(queue2[i+1] - queue2[i]);
        printf("Disk head moves from %d to %d with seek %d\n",queue2[i],queue2[i+1],abs(queue2[i+1] - queue2[i]));
    }
 
    printf("Total seek time = %d\n", seek);
    avg = seek/(float)(temp1+temp2-2);
    printf("Average seek time = %f\n", avg);
 
    return 0;
}
Outputs :: 

 
FCFS Disk Scheduling Algorithm: 

Enter the size of Queue : 4
Enter the Queue : 98 183 37 122
Enter the initial head position : 53
Disk head moves from 53 to 98 with seek 45
Disk head moves from 98 to 183 with seek 85
Disk head moves from 183 to 37 with seek 146
Disk head moves from 37 to 122 with seek 85
Total seek time = 361
Average seek time = 90.250000

SCAN Disk Scheduling Algorithm: 

Enter the size of Queue : 8
Enter the Queue : 98 183 37 122 14 124 65 67
Enter the initial head position : 53
Disk head moves from 53 to 65 with seek 12
Disk head moves from 65 to 67 with seek 2
Disk head moves from 67 to 98 with seek 31
Disk head moves from 98 to 122 with seek 24
Disk head moves from 122 to 124 with seek 2
Disk head moves from 124 to 183 with seek 59
Disk head moves from 183 to 14 with seek 169
Disk head moves from 14 to 37 with seek 23
Total seek time = 322
Average seek time = 40.250000
C-SCAN Disk Scheduling Algorithm: 
Enter the size of Queue : 8
Enter the Queue : 98 183 37 122 14 124 65 67
Enter the initial head position : 53
Disk head moves from 53 to 65 with seek 12
Disk head moves from 65 to 98 with seek 33
Disk head moves from 98 to 122 with seek 24
Disk head moves from 122 to 124 with seek 2
Disk head moves from 124 to 183 with seek 59
Disk head moves from 183 to 0 with seek 183
Disk head moves from 0 to 14 with seek 14
Disk head moves from 14 to 37 with seek 23
Disk head moves from 37 to 65 with seek 28
Total seek time = 398
Average seek time = 49.750000

Related questions

0 votes
1 answer 89 views
0 votes
1 answer 80 views

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

535 users

...