Operating Systems-II
Assignment No.3: Disk Scheduling Algorithms
Slot-I
i. Write an OS program to implement FCFS Disk Scheduling algorithm.
Solution:-
#include<stdio.h>
#include<math.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff;
float avg;
printf("\nEnter the max range of disk : \n");
scanf("%d",&max);
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",&queue[i]);
printf("Enter the initial head position : \n");
scanf("%d",&head);
queue[0]=head;
for(j=0;j<=n-1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("\nDisk head moves from %d to %d with seek %d",queue[j],queue[j+1],diff);
}
printf("\n\nTotal seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %0.2f\n",avg);
return 0;
}
/*
o/p
Enter the max range of disk :
200
Enter the size of queue request :
8
Enter the queue of disk positions to be read :
98 183 41 122 14 124 65 67
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 41 with seek 142
Disk head moves from 41 to 122 with seek 81
Disk head moves from 122 to 14 with seek 108
Disk head moves from 14 to 124 with seek 110
Disk head moves from 124 to 65 with seek 59
Disk head moves from 65 to 67 with seek 2
Total seek time is 632
Average seek time is 79.00
*/
ii. Write an OS program to implement SSTF algorithm Disk Scheduling algorithm.
Solution :-
#include<stdio.h>
#include<math.h>
int main()
{
int queue[100],i,n,seek=0,head,count=0;
float avg;
printf("Enter the number of Requests : \n");
scanf("%d",&n);
printf("Enter the Requests sequence : \n");
for(i=0;i<n;i++)
scanf("%d",&queue[i]);
printf("Enter head head position : \n");
scanf("%d",&head);
while(count!=n)
{
int min=1000,d,index;
for(i=0;i<n;i++)
{
d=abs(queue[i]-head);
if(min>d)
{
min=d;
index=i;
}
}
seek=seek+min;
printf("\nDisk head moves from %d to %d with seek %d",head,queue[index],min);
head=queue[index];
queue[index]=1000;
count++;
}
printf("\n\nTotal head movement is %d \n",seek);
avg=seek/(float)n;
printf("Average seek time is %0.2f\n",avg);
return 0;
}
/*
o/p
Enter the number of Requests :
5
Enter the Requests sequence :
23 89 42 132 187
Enter head head position :
100
Disk head moves from 100 to 89 with seek 11
Disk head moves from 89 to 132 with seek 43
Disk head moves from 132 to 187 with seek 55
Disk head moves from 187 to 42 with seek 145
Disk head moves from 42 to 23 with seek 19
Total head movement is 273
Average seek time is 54.59
*/
Slot-II
i. Write an OS program to implement SCAN Disk Scheduling algorithm.
Solution:-
#include<stdio.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;
for(i=temp1+2,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[i]=0;
queue[0]=head;
printf("\n queue[] :");
for(j=0;j<=n+1;j++)
printf(" %d ",queue[j]);
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("\nDisk head moves from %d to %d with %d",queue[j],queue[j+1],diff);
}
printf("\n\nTotal seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %0.2f\n",avg);
return 0;
}
/*
O/p
Enter the max range of disk :
200
Enter the initial head position :
100
Enter the size of queue request :
5
Enter the queue of disk positions to be read :
23 89 132 42 187
queue[] : 100 132 187 200 89 42 23
Disk head moves from 100 to 132 with 32
Disk head moves from 132 to 187 with 55
Disk head moves from 187 to 200 with 13
Disk head moves from 200 to 89 with 111
Disk head moves from 89 to 42 with 47
Disk head moves from 42 to 23 with 19
Disk head moves from 23 to 0 with 23
Total seek time is 300
Average seek time is 60.00
*/
ii. Write an OS program to implement C-SCAN algorithm Disk Scheduling algorithm.
No comments:
Post a Comment