0% found this document useful (0 votes)
15 views

Disk scheduling algorithms (3)

The document provides implementations of three disk scheduling algorithms: FCFS, SCAN, and C-SCAN in C programming language. Each algorithm includes code that prompts the user for input, processes track movements, and calculates average head movements. The code snippets are structured with comments and include necessary functions for sorting and calculating differences in track movements.

Uploaded by

24f2002721
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Disk scheduling algorithms (3)

The document provides implementations of three disk scheduling algorithms: FCFS, SCAN, and C-SCAN in C programming language. Each algorithm includes code that prompts the user for input, processes track movements, and calculates average head movements. The code snippets are structured with comments and include necessary functions for sorting and calculating differences in track movements.

Uploaded by

24f2002721
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

8. Write a program to implement disk scheduling algorithms.

a) FCFS b) SCAN c) C-SCAN

a)FCFS DISK SCHEDULINGALGORITHM


#include<stdio.h>
main()
{int t[20], n, I, j, tohm[20], tot=0;
float avhm;
clrscr();
printf(“enter the no.of tracks”); scanf(“%d”,&n);
printf(“enter the tracks to betraversed”); for(i=2;i<n+2;i++)
scanf(“%d”,&t*i+);
for(i=1;i<n+1;i++)
{
tohm[i]=t[i+1]-t[i];
if(tohm[i]<0)
tohm[i]=tohm[i]*(-1);
}
for(i=1;i<n+1;i++)
tot+=tohm[i];
avhm=(float)tot/n;
printf(“Tracks traversed\tDifference between tracks\n”);

for(i=1;i<n+1;i++)
printf(“%d\t\t\t%d\n”,t*i+,tohm*i+);
printf("\nAverage header movements:%f",avhm); getch();
}

1
b)SCAN DISK SCHEDULING ALGORITHM
#include<stdio.h>
main()
{int t[20], d[20], h, i, j, n, temp, k, atr[20], tot, p, sum=0;
clrscr();
printf("enter the no of tracks to be traveresed");
scanf("%d'",&n);
printf("enter the position of head"); scanf("%d",&h);
t[0]=0;
t[1]=h;
printf("enter thetracks");
for(i=2;i<n+2;i++)
scanf("%d",&t[i]);
for(i=0;i<n+2;i++)
{
for(j=0;j<(n+2)-i-1;j++)
{
if(t[j]>t[j+1])
{
temp=t[j];
t[j]=t[j+1];
t[j+1]=temp;
}
}
}
for(i=0;i<n+2;i++)
if(t[i]==h)
j=i;
k=i;
p=0;
while(t[j]!=0)
{
atr[p]=t[j];
j--;p++;
}
atr[p]=t[j];
for(p=k+1;p<n+2;p++,k++)
atr[p]=t[k+1];
for(j=0;j<n+1;j++)
{
if(atr[j]>atr[j+1])
d[j]=atr[j]-atr[j+1];
else
d[j]=atr[j+1]-atr[j];
sum+=d[j];
}
printf("\nAverage header movements:%f",(float)sum/n);
getch(); }

2
C) C-SCAN DISK SCHEDULINGALGORITHM
#include<stdio.h>
main()
{int t[20], d[20], h, i, j, n, temp, k, atr[20], tot, p, sum=0;
clrscr();
printf("enter the no of tracks to be traveresed");
scanf("%d'",&n);
printf("enter the position of head"); scanf("%d",&h);
t[0]=0;
t[1]=h;
printf("enter total tracks");
scanf("%d",&tot);
t[2]=tot-1;
printf("enter thetracks"); for(i=3;i<=n+2;i++)
scanf("%d",&t[i]);
for(i=0;i<=n+2;i++)
for(j=0;j<=(n+2)-i-1;j++)
if(t[j]>t[j+1])
{
temp=t[j];
t[j]=t[j+1];
t[j+1]=temp;
}

for(i=0;i<=n+2;i++)
if(t[i]==h);
j=i;
break;
p=0;

3
while(t[j]!=tot-1)
{
atr[p]=t[j];
j++;
p++;
}
atr[p]=t[j];
p++;
i=0;
while(p!=(n+3) && t[i]!=t[h])
{
atr[p]=t[i];
i++;
p++;
}

for(j=0;j<n+2;j++)
{ if(atr[j]>atr[j+1])
d[j]=atr[j]-atr[j+1];
else
d[j]=atr[j+1]-atr[j];
sum+=d[j];
}

printf("total header movements%d",sum);


printf("avg is %f",(float)sum/n);
getch();
}

You might also like