Note: All solution are to written to by C Language 1. Write a program that imple
ID: 3590623 • Letter: N
Question
Note: All solution are to written to by C Language
1. Write a program that implements the following disk scheduling algorithms FCFS (F) . SSTF (T) · SCAN (S) .C-SCAN (C) LOOK (L) .C-LOOK (O) Your program will service a disk with 10,000 cylinders numbered 0 to 9999. The program will read a series of cylinder requests (no maximum) and service them according to each of the algorithms listed above. The program will be passed two parameters on the command line: i) a letter indicating the algorithm to use; and ii) the initial position of the disk head. After execution the program will report the total amount of head movement required.Explanation / Answer
#include<stdio.h>
int absolute(int a,int b)
{int c;
c=a-b;
if(c<0)
return -c;
else
return c;
}
int main()
{int m,n,x,start,i,j,pos,min,a[15],count;
count=0;
m=10000;
char choice,ch;
printf(" Enter the number of requests :");
scanf("%d",&n);
printf(" Enter current position :");
scanf("%d",&start);
printf(" Enter the request queue :");
for(i=0;i<n;i++)
{scanf("%d",&a[i]);
if(a[i]>=m)
{printf(" invalid input");
scanf("%d",&a[i]);
}
}
do
{printf(" DISK SCHEDULING ALGORITHMS");
printf(" 1. FCFS (F) 2. SSTF (T) 3. SCAN (S) 4. C-SCAN (C) 5. LOOK (L) 6. C-LOOK (O)");
printf(" Enter choice :");
scanf("%c",&choice);
count=0;
x=start;
switch(choice)
{case F:printf(" FCFS : ");
printf("Scheduling services the request in the order that follows: %d ",start);
for(i=0;i<n;i++)
{x-=a[i];
if(x<0)
x=-x;
count+=x;
x=a[i];
printf("%d ",x);
}
printf(" Total Head Movement :%d Cylinders",count);
break;
case T:printf(" SSTF : ");
printf("Scheduling services the request in the order that follows: %d ",start);
for(i=0;i<n;i++)
{min=absolute(a[i],x);
pos=i;
for(j=i;j<n;j++)
if(min>absolute(x,a[j]))
{pos=j;
min=absolute(x,a[j]);
}
count+=absolute(x,a[pos]);
x=a[pos];
a[pos]=a[i];
a[i]=x;
printf("%d ",x);
}
printf(" Total Head Movement: %d Cylinders",count);
break;
case S:printf(" SCAN : ");
printf("Scheduling services the request in the order that follows: ");
count=0;
pos=0;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
for(i=0;i<n;i++)
if(a[i]<start)
pos++;
for(i=0;i<pos;i++)
for(j=0;j<pos-i-1;j++)
if(a[j]<a[j+1])
{x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
x=start;
printf("%d ",x);
for(i=0;i<pos;i++)
{count+=absolute(a[i],x);
x=a[i];
printf("%d ",x);
}
count+=absolute(x,0);
x=0;
printf("%d ",x);
for(i=pos;i<n;i++)
{count+=absolute(a[i],x);
x=a[i];
printf("%d ",x);
}
printf(" Requests serviced in the order ");
for(i=0;i<n;i++){
printf("%d ",a[i]);}
printf(" Total Head Movement: %d Cylinders",count);
break;
case C:printf(" C-SCAN : ");
printf("Scheduling Services the request in the order that follows: %d ",start);
count=0;
pos=0;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
for(i=0;i<n;i++)
if(a[i]<start)
pos++;
x=start;
for(i=pos;i<n;i++)
{count+=absolute(x,a[i]);
x=a[i];
printf("%d ",x);
}
count+=absolute(m-1,x);
x=0;
printf("%d %d ",m-1,0);
for(i=0;i<pos;i++)
{count+=absolute(x,a[i]);
x=a[i];
printf("%d ",x);
}
printf(" Requests serviced in the order ");
for(i=0;i<n;i++){
printf("%d ",a[i]);}
printf(" Total Head movement: %d Cylinders",count);
break;
case L:printf(" LOOK : ");
printf(" Scheduling services the request in the order as follows : %d ",start);
count=0;
pos=0;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
for(i=0;i<n;i++)
if(a[i]<start)
pos++;
for(i=0;i<pos;i++)
for(j=0;j<pos-i-1;j++)
if(a[j]<a[j+1])
{x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
x=start;
for(i=0;i<pos;i++)
{count+=absolute(a[i],x);
x=a[i];
printf("%d ",x);
}
for(i=pos;i<n;i++)
{count+=absolute(a[i],x);
x=a[i];
printf("%d ",x);
}
printf(" Requests serviced in the order "); }
for(i=0;i<n;i++){
printf("%d ",a[i]);}
printf(" Toal Head Movement: %d Cylinders",count);
break;
case O:printf(" C-LOOK : ");
printf("Scheduling Services the request in the order that follows: %d ",start);
count=0;
pos=0;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
for(i=0;i<n;i++)
if(a[i]<start)
pos++;
x=start;
for(i=pos;i<n;i++)
{count+=absolute(x,a[i]);
x=a[i];
printf("%d ",x);
}
for(i=0;i<pos;i++)
{count+=absolute(x,a[i]);
x=a[i];
printf("%d ",x);
printf(" Requests serviced in the order "); }
for(i=0;i<n;i++){
printf("%d ",a[i]);}
printf(" Total Head movement: %d Cylinders",count);
break;
}
printf(" Do you want to continue(y to continue) :");
scanf("%c",&ch);
}while(ch=='y');
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.