Professional Documents
Culture Documents
1.FCFS:
#include<stdio.h>
#include<math.h>
int main()
int queue[20],n,head,i,j,k,seek=0,max,diff;
float avg;
scanf("%d",&max);
scanf("%d",&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;
avg=seek/(float)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
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
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