常见磁盘调度算法的源代码
时间: 2023-12-25 16:50:54 浏览: 77
以下是常见磁盘调度算法的源代码示例:
1. 先来先服务 (FCFS)
```c
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[10],i,d=0;
printf("Enter the number of requests:");
scanf("%d",&n);
printf("Enter the requests:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Seek Sequence is:");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
d+=abs(a[i]-a[i-1]);
}
printf("\nTotal Seek Time is %d",d);
getch();
}
```
2. 最短寻道时间优先 (SSTF)
```c
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[10],i,j,min,d=0,k=0,b[10],c[10],flag[10]={0};
printf("Enter the number of requests:");
scanf("%d",&n);
printf("Enter the requests:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
min=1000;
for(j=0;j<n;j++)
{
if(flag[j]==0)
{
if(min>abs(a[j]-k))
{
min=abs(a[j]-k);
b[i]=a[j];
c[i]=j;
}
}
}
flag[c[i]]=1;
k=b[i];
d+=min;
}
printf("Seek Sequence is:");
for(i=0;i<n;i++)
printf("%d ",b[i]);
printf("\nTotal Seek Time is %d",d);
getch();
}
```
3. 扫描算法 (SCAN)
```c
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[10],i,j,temp,d=0,k,pos;
printf("Enter the number of requests:");
scanf("%d",&n);
printf("Enter the requests:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the initial position:");
scanf("%d",&k);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
if(a[i]>k)
{
pos=i;
break;
}
}
printf("Seek Sequence is:");
if(k<=50)
{
for(i=pos;i<n;i++)
{
printf("%d ",a[i]);
d+=abs(a[i]-k);
k=a[i];
}
d+=abs(199-k);
k=199;
for(i=pos-1;i>=0;i--)
{
printf("%d ",a[i]);
d+=abs(a[i]-k);
k=a[i];
}
}
else
{
for(i=pos-1;i>=0;i--)
{
printf("%d ",a[i]);
d+=abs(a[i]-k);
k=a[i];
}
d+=abs(0-k);
k=0;
for(i=pos;i<n;i++)
{
printf("%d ",a[i]);
d+=abs(a[i]-k);
k=a[i];
}
}
printf("\nTotal Seek Time is %d",d);
getch();
}
```
4. 循环扫描算法 (C-SCAN)
```c
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[10],i,j,temp,d=0,k,pos;
printf("Enter the number of requests:");
scanf("%d",&n);
printf("Enter the requests:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the initial position:");
scanf("%d",&k);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
if(a[i]>k)
{
pos=i;
break;
}
}
printf("Seek Sequence is:");
if(k<=50)
{
for(i=pos;i<n;i++)
{
printf("%d ",a[i]);
d+=abs(a[i]-k);
k=a[i];
}
d+=abs(199-k);
k=199;
for(i=0;i<pos;i++)
{
printf("%d ",a[i]);
d+=abs(a[i]-k);
k=a[i];
}
d+=abs(199-k);
}
else
{
for(i=pos-1;i>=0;i--)
{
printf("%d ",a[i]);
d+=abs(a[i]-k);
k=a[i];
}
d+=abs(0-k);
k=0;
for(i=n-1;i>=pos;i--)
{
printf("%d ",a[i]);
d+=abs(a[i]-k);
k=a[i];
}
d+=abs(0-k);
}
printf("\nTotal Seek Time is %d",d);
getch();
}
```
5. 最高响应比优先 (HRRN)
```c
#include<stdio.h>
#include<conio.h>
void main()
{
int n,bt[10],wt[10],tat[10],i,j,sum=0,max,p[10];
float avg_wt,avg_tat;
printf("Enter the number of processes:");
scanf("%d",&n);
printf("Enter the burst time of processes:");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
p[i]=i+1;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
sum+=wt[i];
}
avg_wt=(float)sum/n;
sum=0;
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
sum+=tat[i];
printf("\nP%d\t%d\t\t%d\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)sum/n;
printf("\nAverage Waiting Time:%f",avg_wt);
printf("\nAverage Turnaround Time:%f",avg_tat);
max=0;
for(i=0;i<n;i++)
{
if(max<((float)(bt[i]+wt[i])/bt[i]))
{
max=(float)(bt[i]+wt[i])/bt[i];
j=i;
}
}
printf("\nProcess with highest response ratio:P%d",p[j]);
getch();
}
```
阅读全文