You are on page 1of 16

Q)Implement following Disk Scheduling

Algorithms 1. FCFS 2. SSTF 3. SCAN 4. CSCAN


5. LOOK 6. CLOOK

1.FCFS:
#include<stdio.h>

#include<math.h>

int main()

int queue[20],n,head,i,j,k,seek=0,max,diff;

float avg;

printf("Enter 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("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:
Enter the max range of disk
50
Enter the size of queue request
7
Enter the queue of disk positions to be read
5
25
18
3
39
8
35
Enter the initial head position
20
Disk head moves from 20 to 5 with seek 15
Disk head moves from 5 to 25 with seek 20
Disk head moves from 25 to 18 with seek 7
Disk head moves from 18 to 3 with seek 15
Disk head moves from 3 to 39 with seek 36
Disk head moves from 39 to 8 with seek 31
Disk head moves from 8 to 35 with seek 27
Total seek time is 151
Average seek time is 21.571428

2.SSTF:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int queue[100],t[100],head,seek=0,n,i,j,temp;
float avg;
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

Enter the size of Queue 7


Enter the Queue 5
25
18
3
39
8
35
Enter the initial head position20

Total Seek Time is85


Average Seek Time is 12.142858
3.SCAN:
#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;
for(i=temp1+2,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[i]=0;
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
Enter the max range of disk
50
Enter the initial head position
20
Enter the size of queue request
7
Enter the queue of disk positions to be read
5
25
18
3
39
8
35
Disk head moves from 20 to 25 with seek 5
Disk head moves from 25 to 35 with seek 10
Disk head moves from 35 to 39 with seek 4
Disk head moves from 39 to 50 with seek 11
Disk head moves from 50 to 18 with seek 32
Disk head moves from 18 to 8 with seek 10
Disk head moves from 8 to 5 with seek 3
Disk head moves from 5 to 3 with seek 2
Disk head moves from 3 to 0 with seek 3
Total seek time is 80
Average seek time is 11.428572
4.CSCAN:
#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
Enter the max range of disk
50
Enter the initial head position
20
Enter the size of queue request
7
Enter the queue of disk positions to be read
5
26 5
18
3
39
8
35
Disk head moves from 20 to 25 with seek 5
Disk head moves from 25 to 35 with seek 10
Disk head moves from 35 to 39 with seek 4
Disk head moves from 39 to 50 with seek 11
Disk head moves from 50 to 0 with seek 50
Disk head moves from 0 to 3 with seek 3
Disk head moves from 3 to 5 with seek 2
Disk head moves from 5 to 8 with seek 3
Disk head moves from 8 to 18 with seek 10
Total seek time is 98
Average seek time is 14.000000

5.LOOK:
#include<bits/stdc++.h>
using namespace std;
int main(){
int i,j,k,n,m,sum=0,x,y,h;
cout<<"Enter the max range of disk\n";
cin>>m;
cout<<"Enter the size of queue request\n";
cin>>n;
cout<<"Enter the queue of disk positions to be read\n";
vector <int> a(n),l;
for(i=0;i<n;i++){
cin>>a[i];
}
for(i=0;i<n;i++){
if(a[i]>m){
cout<<"Error, Unknown position\n";
return 0;
}
}
cout<<"Enter the initial head position\n";
cin>>h;
a.push_back(h);
sort(a.begin(),a.end());
for(i=0;i<a.size();i++){
if(h==a[i])
break;
}
k=i;
if(k<n/2){
for(i=k;i<a.size();i++){
l.push_back(a[i]);
}
for(i=k-1;i>=0;i--){
l.push_back(a[i]);
}
}
else{
for(i=k;i>=0;i--){
l.push_back(a[i]);
}
for(i=k+1;i<a.size();i++){
l.push_back(a[i]);
}
}
int temp=l[0];
cout<<temp;
for(i=1;i<l.size();i++){
cout<<" -> "<<l[i]<<' ';
sum+=abs(l[i]-temp);
temp=a[i];
}
cout<<'\n';
cout<<"Total seek time = "<< sum<<'\n';
return 0;
}
Output
Enter the max range of disk
50
Enter the size of queue request
7
Enter the queue of disk positions to be read
5
25
18
3
39
8
35
Enter the initial head position
20
20 -> 18 -> 8 -> 5 -> 3 -> 25 -> 35 -> 39
Total seek time= 42

6.CLOOK:

#include<bits/stdc++.h>
using namespace std;
int main(){
int i,j,k,n,m,sum=0,x,y,h;
cout<<"Enter the max range of disk\n";
cin>>m;
cout<<"Enter the size of queue request\n";
cin>>n;
cout<<"Enter the queue of disk positions to be read\n";
vector <int> a(n),l;
for(i=0;i<n;i++){
cin>>a[i];
}
for(i=0;i<n;i++){
if(a[i]>m){
cout<<"Error, Unknown position\n";
return 0;
}
}
cout<<"Enter the initial head position\n";
cin>>h;
a.push_back(h);
sort(a.begin(),a.end());
for(i=0;i<a.size();i++){
if(h==a[i])
break;
}
k=i;
if(k<n/2){
for(i=k;i<a.size();i++){
l.push_back(a[i]);
}
for(i=0;i<k;i++){
l.push_back(a[i]);
}
}
else{
for(i=k;i>=0;i--){
l.push_back(a[i]);
}
for(i=a.size()-1;i>k;i--){
l.push_back(a[i]);
}
}
int temp=l[0];
cout<<temp;
for(i=1;i<l.size();i++){
cout<<" -> "<<l[i]<<' ';
sum+=abs(l[i]-temp);
temp=a[i];
}
cout<<'\n';
cout<<"Total seek time = "<< sum<<'\n';
return 0;
}
Output
Enter the max range of disk
50
Enter the size of queue request
7
Enter the queue of disk positions to be read
5
25
18
3
39
8
35
Enter the initial head position
20
20 -> 18 -> 8 -> 5 -> 3 -> 39 -> 35 -> 25
Total seek time = 62

You might also like