Sie sind auf Seite 1von 17

ASSIGNMENT :- 9

Write a C program to implement the following disk scheduling algorithms:


a)FCFS
b)SSTF
c)SCAN
d)C-SCAN
e)LOOK
f)C-LOOK

Soln).

A).

#include<stdio.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff;
float aver;
printf("enter the max range of disk");
scanf("%d",&max);
printf("enter the size of queue request");
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(j=0;j<=n-1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("move is from %d to %d with seek %d\n",queue[j],queue[j+1],diff);
}
printf("total seek time is%d\n",seek);
aver=seek/(float)n;
printf("avrage seek time is %f\n",aver);
return 0;
}
OUTPUT:
B).
#include<stdio.h>
#include<math.h>
Int main()
{
int queue[100],t[100],head,seek=0,n,i,j,temp;
float avg;

printf("*** SSTF Disk Scheduling Algorithm ***\n");


printf("Enter the size of Queue\t");
scanf("%d",&n);
printf("Enter the Queue\t");
for(i=0;i<n;i++)
{
scanf("%d",&queue[i]);
}
printf("Enter the initial head position\t");
scanf("%d",&head);
for(i=1;i<n;i++)
t[i]=abs(head-queue[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(t[i]>t[j])
{
temp=t[i];
t[i]=t[j];
t[j]=temp;
temp=queue[i];
queue[i]=queue[j];
queue[j]=temp;
}
}
}
for(i=1;i<n-1;i++)
{
seek=seek+abs(head-queue[i]);
head=queue[i];
}
printf("\nTotal Seek Time is%d\t",seek);
avg=seek/(float)n;
printf("\nAverage Seek Time is %f\t",avg);
return 0;
}

OUTPUT:
C).
#include<stdio.h>

Int main()
{
Int ar[100],head,que[102],n,end,i,seek=0,j,poe,temp;
float avseek;

printf("Enter the total no. of track ");


scanf("%d",&end);
end--;
printf("Enter the initial head position ");
scanf("%d",&head);
printf("Enter the no. of request ");
scanf("%d",&n);
printf("Enter the sequence of request ");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}

for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
}
que[0]=head;
que[1]=end;
poe=1;
i=0;
while(i<n)
{
if(ar[i]>head)
{ que[poe+1]=que[poe];
que[poe]=ar[i];
poe++;
}
i++;

i=n-1;
while(i>=0)
{
if(ar[i]<head){
poe++;
que[poe]=ar[i];

}
i--;
}
printf("\nSequence of execution is-");
printf("\n%d->",que[0]);
for(i=1;i<n+1;i++)
{
printf("%d->",que[i]);
}
printf("%d",que[n+1]);
i=0;
while(i<n+1)
{
seek+=abs(que[i+1]-que[i]);
i++;
}
printf("\nTotal Seek time is-%d",seek);
avseek=seek/n;
printf("\nAverage Seek Time is-%f",avseek);

return 0;
}
OUTPUT:
D).

#include<stdio.h>
#include<math.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 max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>=head)
{
queue1[temp1]=temp;
temp1++;
}
else
{
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]>queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
queue[i+1]=0;
for(i=temp1+3,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[0]=head;
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}
OUTPUT:
E).
#include<math.h>
#include<stdio.h>
Int main()
{
Int i,n,j=0,k=0,x=0,l,req[50],mov=0,cp,ub,end, lower[50],upper[50], temp,a[50];
printf("enter the current position\n");
scanf("%d",&cp);
printf("enter the number of requests\n");
scanf("%d",&n);
printf("enter the request order\n");
for(i=0;i<n;i++)
{
scanf("%d",&req[i]);
}
printf("Enter the upper bound\n");
scanf("%d",&ub);

for(i=0;i<n;i++)
{
if(req[i]<cp)
{
lower[j]=req[i];
j++;
}
if(req[i]>cp)
{
upper[k]=req[i];
k++;
}
}

for(i=0;i<j;i++)
{
for(l=0;l<j-1;l++)
{
if(lower[l]<lower[l+1])
{
temp=lower[l];
lower[l]=lower[l+1];
lower[l+1]=temp;
}
}
}

for(i=0;i<=k;i++)
{
for(l=0;l<k-1;l++)
{
if(upper[l]>upper[l+1])
{
temp=upper[l];
upper[l]=upper[l+1];
upper[l+1]=temp;
}
}
}

printf("Enter the end to which the head is moving (0 - for lower end(zero) and 1 - for upper
end\n");
scanf("%d",&end);
switch(end)
{
case 0:
for(i=0;i<j;i++)
{
a[x]=lower[i];
x++;
}

for(i=0;i<k;i++)
{
a[x]=upper[i];
x++;
}
break;
case 1:
for(i=0;i<k;i++)
{
a[x]=upper[i];
x++;
}

for(i=0;i<j;i++)
{
a[x]=lower[i];
x++;
}
break;
}

mov=mov+abs(cp-a[0]);
printf("%d -> %d",cp,a[0]);
for(i=1;i<x;i++)
{
mov=mov+abs(a[i]-a[i-1]);
printf(" -> %d",a[i]);
}
printf("\n");
printf("total head movement = %d\n",mov);
}
OUTPUT:
F).
#include<stdio.h>
#include<math.h>
#define max 20
#define cymax 199

Int i,j,req,ttl_tracks=0,cp,np,cposn,nposn;
Int cyposn[max],temp;

void input()
{
do
{

printf("\n Enter the current header position : ");


scanf("%d",&cposn);
}while(cposn>cymax || cposn<=0);
printf("\n Enter the %d I/O Requests : ",req);
cyposn[0] = cposn;
for(i=1;i<=req;i++)
scanf("%d",&cyposn[i]);
}

void CLOOK()
{
for(i=0;i<=req;i++)
{
for(j=0;j<req-i;j++)
{
if(cyposn[j] >cyposn[j+1])
{
temp = cyposn[j];
cyposn[j] = cyposn[j+1];
cyposn[j+1] = temp;
}
}
}
cp=0;
do
{
if(cyposn[cp] == cposn)
break;
cp++;
}while(cp!=req);
printf("\nS.No. Current Position Next Position Displacement \n");
printf("---------------------------------------------------------- \n\n");
i=0,j=cp;
cposn = cyposn[cp];
do
{
if(cp == req)
{ nposn = cyposn[0]; cp = 0; }
else
nposn = cyposn[++cp];
printf(" %d\t\t%d\t\t%d\t\t%d\n",++i,cposn,nposn,abs(cposn-nposn));
ttl_tracks += (abs(cposn-nposn));
cposn = nposn == cyposn[req] ?cyposn[0] : nposn ;
}while(nposn != cyposn[j-1]);
printf("---------------------------------------------------------- \n\n");
printf(" Total Tracks Displaced : %d",ttl_tracks);
}

Int main()
{
do
{

printf("\n Enter the number of requests : ");


scanf("%d",&req);
}while(req>max || req<=0);
input();
CLOOK();
return 0;
}
OUTPUT: