磁盘调度算法C语言实现

最短寻道时间优先(SSTF)算法。要求访问的磁道,与当前磁头所在的磁道距离最近,以使每次的寻道时间最短。

扫描调度(SCAN)算法。该算法不仅考虑到欲访问的磁道与当前磁道间的距离,更优先考虑的是磁头当前的移动方向。例如,当磁头正在自里向外移动时,SCAN算法所考虑的下一个访问对象,应是其欲访问的磁道,既在当前磁道之外,又是距离最近的。这样自里向外的访问,直至再无更外的磁道需要访问时,才将磁道换向自外向里移动。

#include<iostream>
#include<ctime>
using namespace std;
void SSTF(int a[],int n);
void CSCAN(int a[],int n);
int main()
{  
   int n;  //磁道的个数

   int s;   //功能号

   cout<<"请输入当前磁道的个数,按Enter键显示生成的随机磁道号:"<<endl;

   cin>>n;

   int *a=new int[n];//申请一个整型变量空间,赋初值为n,并定义一个整型指针a指向该地址空间 

   cout<<"生成的随机磁道号为:";

   srand((unsigned)time(NULL));

   for(int i=0;i<n;i++)
   {
      a[i]=(rand()%100)+1;
      cout<<a[i]<<" ";
   }
   cout<<endl;

	while(1)
 { 
	cout<<endl;
	cout<<"             ┏━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl;
	cout<<"             ┃        磁盘调度算法功能列表          ┃"<<endl;
	cout<<"             ┠───────────────────────┨"<<endl;
	cout<<"             ┃     1、最短寻道时间算法(SSTF)              ┃"<<endl;
	cout<<"             ┠───────────────────────┨"<<endl;
	cout<<"             ┃     2、循环扫描算法(CSCAN)                 ┃"<<endl;
	cout<<"             ┠───────────────────────┨"<<endl;
	cout<<"             ┃     0、退出                                  ┃"<<endl;
	cout<<"             ┗━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;
	cout<<endl;

   cout<<"请选择所需功能的前置编号:";

   cin>>s;

   if(s>3)
   {
	cout<<"数据输入有误!请重新输入:"<<endl;
   } 
   else
	{
     switch(s){
		case 0: exit(0);break ;         
        case 1:SSTF(a, n);break;
        case 2:CSCAN(a,n);break;}
	}
}
	 return 0; 
}

//最短寻道时间算法(SSTF)
void SSTF(int a[],int n)
{
	int temp;
	int k=1;
	int now,l,r;
	int i,j;
	double sum=0;
//将磁道号按递增排序
	for(i=0;i<n;i++)
	
		for(j=i+1;j<n;j++){
			if(a[i]>a[j]){
				temp=a[i];
				a[i]=a[j];
				a[j]=temp;
  }
}
	cout<<"按递增顺序排好的磁道显示为:"<<endl;

	for(i=0;i<n;i++)
	{
	
		cout<<a[i]<<" ";//输出排好的磁道顺序
	}
	
	cout<<endl;

	cout<<"请输入当前的磁道号:";

	cin>>now;//确定当前磁头所在位置

	cout<<"磁盘调度顺序为:"<<endl;

	if(a[n-1]<=now){//当前磁头位置大于最外围欲访问磁道(比如21,90,当前的磁道为91,顺序为91,1)
		
		for(i=n-1;i>=0;i--)
		
			cout<<a[i]<<" ";
			sum=now-a[0];
	}

	else 
		
		if(a[0]>=now)//当前磁头位置小于最里欲访问磁道(比如21,90,当前的磁道为91,顺序为21,91)
  {
		for(i=0;i<n;i++)
			cout<<a[i]<<" ";
			sum=a[n-1]-now;
  }
	else//不大也不小,在范围之内
  {
    while(a[k]<now)//确定当前磁道在已排的序列中的位置
	{
     k++;
	}
     l=k-1;//在磁头位置的前一个欲访问磁道
     r=k;//磁头欲访问磁道

    while((l>=0)&&(r<n))
{
     if((now-a[l])<=(a[r]-now))//选择离磁头近的磁道
	 {
		cout<<a[l]<<" ";//(比如37,44,49,64,84,93,当前的磁道为65,则调度后的循序为:64(为a[l]),49,44,37,84 93)
		sum+=now-a[l];
		now=a[l];
		l=l-1;
	 }
     else
     {
		cout<<a[r]<<" ";//(比如22 26 62 67 88 而当前的磁道为87,则磁盘调度的顺序为88 67 62 23 22 )

		sum+=a[r]-now;

		now=a[r];

		r=r+1;
     }
}
  if(l=-1)//磁头位置里侧的磁道已访问完,这是针对(now-a[l]<a[r]-now的情况)
  {
  for(j=r;j<n;j++)//访问磁头位置外侧的磁道
  {
    cout<<a[j]<<" ";
  }
  sum+=a[n-1]-a[0];
  }
	if(r==n)//磁头位置外侧的磁道已访问完,这是针对(else的情况)
	{
	for(j=k-1;j>-1;j--) //访问磁头位置里侧的磁道
           {
               cout<<a[j]<<" ";
           }
           sum+=a[n-1]-a[0];
	}
  }
	cout<<endl;
	cout<<"移动的总道数为:"<<sum<<endl;
	cout<<"移动的平均道数为:"<<(sum/n)<<endl;
}

//循环扫描算法(CSCAN)
void CSCAN(int a[],int n)
{
	int temp;
	int now,l,r;
	int i,j;
	double sum=0;
	int k=1;
	for(i=0;i<n;i++)//对访问磁道按由小到大顺序排列输出
		for(j=i+1;j<n;j++){
    if(a[i]>a[j])
	{
     temp=a[i];
    a[i]=a[j];
    a[j]=temp;
	}
  }
  cout<<"按递增顺序排好的磁道为:"<<endl;
  for( i=0;i<n;i++)
  {
		cout<<a[i]<<" ";
  }
		cout<<endl;
		cout<<"请输入当前的磁道号:";
		cin>>now;//确定当前磁道号
	if(a[n-1]<=now)//磁头位置大于最外围欲访问磁道
{
  for(i=0;i<n;i++)
        cout<<a[i]<<" ";
  sum=now-2*a[0]+a[n-1];
}
    else
        if(a[0]>=now)//磁头位置小于最里欲访问磁道
        {
            for(i=0;i<n;i++)
            cout<<a[i]<<" ";
            sum=a[n-1]-now;
        }
        else //磁头位置在最里侧磁道与最外侧磁道之间
        {    int d;
            while(a[k]<now)
            {
                k++;
            }
           l=k-1;//在磁头位置的前一个欲访问磁道
           r=k;   //磁头欲访问磁道
		   cout<<"请输入当前磁头移动的方向 (0 表示向内 ,1表示向外) : ";
           cin>>d;    //确定磁头访问的方向
		   cout<<"磁盘调度顺序为:";
           if(d==0||d==1)
		   {
               if(d==1)    //磁头向外侧访问
			   {
                for(j=r;j<n;j++)//先访问外侧磁道再转向最里欲访问磁道
				{
                 cout<<a[j]<<" ";
				}
                for(j=0;j<r;j++)
				{
                 cout<<a[j]<<" ";
				}
                 sum=2*a[n-1]-now-2*a[0]+a[l];
			   }
              if(d==0)                //磁头向内侧访问
			  { 
				 for(j=r-1;j>=0;j--)
				 {
                 cout<<a[j]<<" ";
				 }
			     for(j=n-1;j>=r;j--)
				 {
                 cout<<a[j]<<" ";
				 }
				 sum=2*a[n-1]-2*a[0]+now-a[r];
			  }
		   }
		  else
		  cout<<"请输入0或1!";
       }
	cout<<endl;
	cout<<"移动的总道数为:"<<sum<<endl;
	cout<<"移动的平均道数为:"<<(sum/n)<<endl;

}

以下是几种常见磁盘调度算法C语言实现: 1. 先来先服务(FCFS)算法 ```c #include <stdio.h> #include <stdlib.h> #include <math.h> int main() { int queue[1000], head, n, i, j, seek_time = 0; printf("Enter the number of requests: "); scanf("%d", &n); printf("Enter the request queue: "); for(i=0; i<n; i++) scanf("%d", &queue[i]); printf("Enter the initial head position: "); scanf("%d", &head); for(i=0; i<n; i++) { seek_time += abs(head - queue[i]); head = queue[i]; } printf("Total seek time: %d\n", seek_time); return 0; } ``` 2. 最短寻道时间优先(SSTF)算法 ```c #include <stdio.h> #include <stdlib.h> #include <math.h> int find_closest(int queue[], int n, int head, int visited[]) { int min_dist = 100000, index = -1, i; for(i=0; i<n; i++) { if(!visited[i]) { int dist = abs(head - queue[i]); if(dist < min_dist) { index = i; min_dist = dist; } } } return index; } int main() { int queue[1000], head, n, i, j, seek_time = 0, visited[1000] = {0}; printf("Enter the number of requests: "); scanf("%d", &n); printf("Enter the request queue: "); for(i=0; i<n; i++) scanf("%d", &queue[i]); printf("Enter the initial head position: "); scanf("%d", &head); for(i=0; i<n; i++) { int index = find_closest(queue, n, head, visited); visited[index] = 1; seek_time += abs(head - queue[index]); head = queue[index]; } printf("Total seek time: %d\n", seek_time); return 0; } ``` 3. 扫描(SCAN)算法 ```c #include <stdio.h> #include <stdlib.h> #include <math.h> int main() { int queue[1000], head, n, i, j, seek_time = 0; printf("Enter the number of requests: "); scanf("%d", &n); printf("Enter the request queue: "); for(i=0; i<n; i++) scanf("%d", &queue[i]); printf("Enter the initial head position: "); scanf("%d", &head); int direction = 1; // 1 for up, -1 for down for(i=0; i<n; i++) { if(direction == 1) // scanning up { int closest_index = -1, min_dist = 100000; for(j=0; j<n; j++) { if(queue[j] >= head && abs(queue[j] - head) < min_dist) { closest_index = j; min_dist = abs(queue[j] - head); } } if(closest_index == -1) // no requests above head { direction = -1; seek_time += abs(queue[n-1] - head); // go to the end head = queue[n-1]; } else { seek_time += abs(queue[closest_index] - head); head = queue[closest_index]; } } else // scanning down { int closest_index = -1, min_dist = 100000; for(j=0; j<n; j++) { if(queue[j] <= head && abs(queue[j] - head) < min_dist) { closest_index = j; min_dist = abs(queue[j] - head); } } if(closest_index == -1) // no requests below head { direction = 1; seek_time += abs(queue[0] - head); // go to the beginning head = queue[0]; } else { seek_time += abs(queue[closest_index] - head); head = queue[closest_index]; } } } printf("Total seek time: %d\n", seek_time); return 0; } ``` 4. 循环扫描(C-SCAN)算法 ```c #include <stdio.h> #include <stdlib.h> #include <math.h> int main() { int queue[1000], head, n, i, j, seek_time = 0; printf("Enter the number of requests: "); scanf("%d", &n); printf("Enter the request queue: "); for(i=0; i<n; i++) scanf("%d", &queue[i]); printf("Enter the initial head position: "); scanf("%d", &head); int direction = 1; // 1 for up, -1 for down for(i=0; i<n; i++) { if(direction == 1) // scanning up { int closest_index = -1, min_dist = 100000; for(j=0; j<n; j++) { if(queue[j] >= head && abs(queue[j] - head) < min_dist) { closest_index = j; min_dist = abs(queue[j] - head); } } if(closest_index == -1) // no requests above head { direction = -1; seek_time += abs(queue[n-1] - head) + abs(queue[n-1] - queue[0]); // go to the end and then to the beginning head = queue[0]; } else { seek_time += abs(queue[closest_index] - head); head = queue[closest_index]; } } else // scanning down { int closest_index = -1, min_dist = 100000; for(j=0; j<n; j++) { if(queue[j] <= head && abs(queue[j] - head) < min_dist) { closest_index = j; min_dist = abs(queue[j] - head); } } if(closest_index == -1) // no requests below head { direction = 1; seek_time += abs(queue[n-1] - head) + abs(queue[n-1] - queue[0]); // go to the end and then to the beginning head = queue[0]; } else { seek_time += abs(queue[closest_index] - head); head = queue[closest_index]; } } } printf("Total seek time: %d\n", seek_time); return 0; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值