Disk Sheduling Algorithms

Disk sheduling algorithms are about how the disk (Hard disk) handles the concurrency requests that correspond to various cylinders of the disk.In layman's language, there will be in hard disk data will be stored at different parts and since like main memory, since random access is not possible, it has to handle the requests accordingly using the algorithms.


All the programmes below are in "C" and use file handling.There will be sub headings related to the disk sheduling algorithms and below them, the corresponding code snippets.The code once compiled runs automatically using the data from text file and displays the output.Tested for DevCPP and codeblocks in Windows.Just be careful to keep the input text file in the same folder as programmes and change the name of the input file in the programme to correspond to the text file.If you have doubts, comment, will clarify them, with in at max two days.

Input file(Input.txt – same for all programs)

8
98 183 37 122 14 124 65 67
53

1.FCFS

#include<stdio.h>
int main()
{int n,a[20],i,rwp,rwc,j;
rwc=0;
int f[20]={0};
int flag=1;
    FILE *fp;
fp=fopen("Input.txt","r");
fscanf(fp,"%d",&n);
for(i=0;i<n;i++)
fscanf(fp,"%d",&a[i]);
fscanf(fp,"%d",&rwp);
fclose(fp);
printf("\n\nThe size of the disk sheduling queue is %d",n);
printf("\n\nThe elements in the disk sheduling queue are\n\n");
for(i=0;i<n;i++)
printf("%d  ",a[i]);
i=0;
printf("\n\nThe order of scanning is \n\n");
while(flag)
{

    if(rwp>a[i])
    rwc=rwc+rwp-a[i];
    else
    rwc=rwc+a[i]-rwp;
    rwp=a[i];
    f[i]=1;
    printf("%d\n\n",a[i]);
    i++;
    
    flag=0;
    for(j=0;j<n;j++)
      if(f[j]==0)
 {   flag=1;
    break;
}
}
printf("\n\nThe number of movements of read write head are %d",rwc);
getche();
}

2.SSTF

#include<stdio.h>
int n,a[20],rwp,rwc,j;
rwc=0;
int f[20]={0};
int SSTF()
{int ss[20];
int min;
for(j=0;j<n;j++)
     if(rwp>a[j])
ss[j]=rwp-a[j];
    else
 ss[j]=a[j]-rwp;
 for(j=0;j<n;j++)
 if(f[j]==0)
 {min=j;
 break;
}
 for(j=1;j<n;j++)
 if(ss[j]<ss[min]&&f[j]!=1)
 min=j;
  return min;   
}
int main()
{int i;
int flag=1;
    FILE *fp;
fp=fopen("Input.txt","r");
fscanf(fp,"%d",&n);
for(i=0;i<n;i++)
fscanf(fp,"%d",&a[i]);
fscanf(fp,"%d",&rwp);
fclose(fp);
printf("\n\nThe size of the disk sheduling queue is %d",n);
printf("\n\nThe elements in the disk sheduling queue are\n\n");

for(i=0;i<n;i++)
printf("%d  ",a[i]);
printf("\n\nThe order of scanning is \n\n");
i=0;
while(flag)
{
i=SSTF();
    if(rwp>a[i])
    rwc=rwc+rwp-a[i];
    else
    rwc=rwc+a[i]-rwp;
    rwp=a[i];
    f[i]=1;

    printf("%d\n\n",a[i]);
    flag=0;
    for(j=0;j<n;j++)
      if(f[j]==0)
 {   flag=1;
    break;
}
}
printf("\n\nThe number of movements of read write head are %d",rwc);
getche();
}

3.SCAN

#include<stdio.h>
int n,a[20],rwp,rwc,j,leftflag=1;
int rwc=0;
int f[20]={0};
int SCAN()
{if(leftflag)
    {for(j=0;j<n;j++)
    if(rwp<=a[j])
    break;
j=j-1;
    return j;}
    else
     {for(j=0;j<n;j++)
     if(rwp<=a[j]&&f[j]!=1)
     break;

     return j;
     }  
}
int main()
{
int i,flag=1,temp;

    FILE *fp;
fp=fopen("Input.txt","r");
fscanf(fp,"%d",&n);
for(i=0;i<n;i++)
fscanf(fp,"%d",&a[i]);
fscanf(fp,"%d",&rwp);
fclose(fp);
printf("\n\nThe size of the disk sheduling queue is %d",n);
printf("\n\nThe elements in the disk sheduling queue are\n\n");

for(i=0;i<n;i++)
printf("%d  ",a[i]);
i=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{if(a[i]<a[j])
{temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("\n\nThe order of scanning is \n\n");
while(flag)
{
i=SCAN();
    if(rwp>a[i])
    rwc=rwc+rwp-a[i];
    else
    rwc=rwc+a[i]-rwp;
    rwp=a[i];
    f[i]=1;
printf("%d\n\n",a[i]);

    if(i==0)
 {   leftflag=0;
 rwc=rwc+a[i];

 rwp=0;
 printf("%d\n\n",rwp);
}
    flag=0;
    for(j=0;j<n;j++)
      if(f[j]==0)
 {   flag=1;
    break;
}
}
printf("\n\nThe number of movements of read write head are %d",rwc);
getche();
}

4.C-SCAN

#include<stdio.h>
int n,a[20],rwp,rwc,j,leftflag=1;
int rwc=0;
int f[20]={0};
int SCAN()
{if(leftflag)
    {for(j=0;j<n;j++)
    if(rwp<=a[j])
    break;
j=j-1;
    return j;}
    else
     {for(j=n-1;j>0;j--)
     if(rwp>=a[j]&&f[j]!=1)
     break;

     return j;
     }  
}
int main()
{
int i,flag=1,temp;

    FILE *fp;
fp=fopen("Input.txt","r");
fscanf(fp,"%d",&n);
for(i=0;i<n;i++)
fscanf(fp,"%d",&a[i]);
fscanf(fp,"%d",&rwp);
fclose(fp);
printf("\n\nThe size of the disk sheduling queue is %d",n);
printf("\n\nThe elements in the disk sheduling queue are\n\n");

for(i=0;i<n;i++)
printf("%d  ",a[i]);
i=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{if(a[i]<a[j])
{temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("\n\nThe order of scanning is \n\n");
while(flag)
{
i=SCAN();
    if(rwp>a[i])
    rwc=rwc+rwp-a[i];
    else
    rwc=rwc+a[i]-rwp;
    rwp=a[i];
    f[i]=1;
printf("%d\n\n",a[i]);

    if(i==0)
 {   leftflag=0;
 rwc=rwc+a[i];
 rwp=199;
 printf("%d\n\n",rwp);
}
    flag=0;
    for(j=0;j<n;j++)
      if(f[j]==0)
 {   flag=1;
    break;
}
}
printf("\n\nThe number of movements of read write head are %d",rwc);
getche();
}

5.Look

#include<stdio.h>
int n,a[20],rwp,rwc,j,leftflag=1;
int rwc=0;
int f[20]={0};
int SCAN()
{if(leftflag)
    {for(j=0;j<n;j++)
    if(rwp<=a[j])
    break;
j=j-1;
    return j;}
    else
     {for(j=0;j<n;j++)
     if(rwp<=a[j]&&f[j]!=1)
     break;

     return j;
     }  
}
int main()
{
int i,flag=1,temp;

    FILE *fp;
fp=fopen("Input.txt","r");
fscanf(fp,"%d",&n);
for(i=0;i<n;i++)
fscanf(fp,"%d",&a[i]);
fscanf(fp,"%d",&rwp);
fclose(fp);
printf("\n\nThe size of the disk sheduling queue is %d",n);
printf("\n\nThe elements in the disk sheduling queue are\n\n");

for(i=0;i<n;i++)
printf("%d  ",a[i]);
i=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{if(a[i]<a[j])
{temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("\n\nThe order of scanning is \n\n");
while(flag)
{
i=SCAN();
    if(rwp>a[i])
    rwc=rwc+rwp-a[i];
    else
    rwc=rwc+a[i]-rwp;
    rwp=a[i];
    f[i]=1;
printf("%d\n\n",a[i]);

    if(i==0)
 {   leftflag=0;
}
    flag=0;
    for(j=0;j<n;j++)
      if(f[j]==0)
 {   flag=1;
    break;
}
}
printf("\n\nThe number of movements of read write head are %d",rwc);
getche();
}

6.C – Look

#include<stdio.h>
int n,a[20],rwp,rwc,j,leftflag=1;
int rwc=0;
int f[20]={0};
int SCAN()
{if(leftflag)
    {for(j=0;j<n;j++)
    if(rwp<=a[j])
    break;
j=j-1;
    return j;}
    else
     {for(j=n-1;j>0;j--)
     if(rwp>=a[j]&&f[j]!=1)
     break;

     return j;
     }  
}
int main()
{
int i,flag=1,temp;

    FILE *fp;
fp=fopen("Input.txt","r");
fscanf(fp,"%d",&n);
for(i=0;i<n;i++)
fscanf(fp,"%d",&a[i]);
fscanf(fp,"%d",&rwp);
fclose(fp);
printf("\n\nThe size of the disk sheduling queue is %d",n);
printf("\n\nThe elements in the disk sheduling queue are\n\n");

for(i=0;i<n;i++)
printf("%d  ",a[i]);
i=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{if(a[i]<a[j])
{temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("\n\nThe order of scanning is \n\n");
while(flag)
{
i=SCAN();
    if(rwp>a[i])
    rwc=rwc+rwp-a[i];
    else
    rwc=rwc+a[i]-rwp;
    rwp=a[i];
    f[i]=1;
printf("%d\n\n",a[i]);

    if(i==0)
 {   leftflag=0;
 rwp=a[n-1];
}
    flag=0;
    for(j=0;j<n;j++)
      if(f[j]==0)
 {   flag=1;
    break;
}
}
printf("\n\nThe number of movements of read write head are %d",rwc);
getche();
}    


No comments:

Post a Comment