磁盘调度 FCFS、SSTF、SCAN 算法c++实现(大学生专用)

前言

大家放实验报告里面的时候,好歹要把变量名什么的改改吧。。。一模一样的给老师发现就不好了。。。


代码

#include 
#include 

#define MAX 10000
using namespace std;

//求平均寻道长度
double average(int *length, int n)
{
    double sum=0;
    for(int i= 0;i<n;i++)
        sum+=length[i];
    return sum/n;
}

//求移动赤道总数
double sum(int* length, int n)
{
    double sum_=0;
    for(int i= 0;i<n;i++)
        sum_+=length[i];
    return sum_;
}

void FCFS(int *a, int n, int now)
{
    int t=n;
    int start = now;
    int k=0;
    int next[MAX], length[MAX]; //next数组存放访问序列,length存放每次访问所移动的磁道数
    int visit[MAX]={0}; //标记某个赤道是否已被访问过

    for(int i = 0;i < n;i++) {
        length[k] = abs(a[i] - now);
        next[k++] = a[i];
        now = a[i];
     }

    cout <<"\n磁道移动轨迹\n";
    cout << start <<" ";
    for(int i=0;i<n;i++)
       cout<<next[i]<<" ";
    cout <<endl;
    cout <<"移动磁道数 ="<<sum(length,n)<<endl;
    cout <<"平均寻道长度 = "<<average(length,n)<<endl;
}

void SSTF(int *a, int n, int now)
{
    
    int k = 0;
    int t = n;
    int start = now;
    int flag = 0; //最短路径下标
    int next[MAX], length[MAX], visit[MAX]={0};

    while(t--)
    {
        int min=999999; //最短路径
        for(int i=0;i<n;i++)
        {
            if(visit[i])
                continue;
            if(abs(a[i]-now)<min)
            {
                min = abs(a[i]-now);
                flag = i;
            }
        }
        length[k]=min;
        next[k++] = a[flag];
        visit[flag]=1;
        now = a[flag];
    }
    cout <<"\n磁道移动轨迹\n";
    cout<<start<<" ";
    for(int i=0;i<n;i++)
       cout<<next[i]<<" ";
    cout <<endl;
    cout <<"移动磁道数 ="<<sum(length,n)<<endl;
    cout <<"平均寻道长度 = "<<average(length,n)<<endl;
}

void SCAN(int *a, int n, int now)
{
    int flag=0,k=0;
    int start = now;
    int next[MAX], length[MAX];
    int b[MAX] = {0}; //替代a数组,否则下面的sort()会改变数组a
    for(int i=0;i<n;i++)
        b[i] = a[i];
    sort(b,b+n); //数组内部排序

    for(int i=0;i<n;i++)
    {
        if(b[i]>now)
        {
            flag = i;
            break;
        }
    }
    //往递增方向
    for(int i = flag; i<n;i++)
    {
        next[k] = b[i];
        length[k++] = abs(b[i] - now);
        now = b[i];
    }
    //返回往递减方向
    for(int i=flag-1;i>=0;i--)
    {
        next[k] = b[i];
        length[k++] = abs(b[i] - now);
        now = b[i];
    }
    cout <<"\n磁道移动轨迹\n";
    cout<<start<<" ";
    for(int i=0;i<n;i++)
        cout<<next[i]<<" ";
    cout <<endl;
    cout <<"移动磁道数 ="<<sum(length,n)<<endl;
    cout <<"平均寻道长度 = "<<average(length,n)<<endl;
}

int main()
{
    int a[MAX]; //存放要用到的磁道号
    int start; //开始磁道
    int n,flag =1 ;
    cout << "磁道数:";
    cin >> n;
    cout <<"磁道序列:";
    srand((int)time(0));
    for(int i=0;i<n;i++)
    {
        a[i] = rand()%1001; //磁道序列序号0~1000
        cout << a[i]<<" ";
    }
    cout <<endl;
    cout <<"开始时的磁道:";
    cin >>start;

    //磁盘调度目录
	while(flag){
		cout<<"请选一种磁盘调度算法:"<<endl;;
		cout<<"1.先来先服务寻道算法"<<endl;
		cout<<"2.最短寻道时间优先"<<endl;
		cout<<"3.扫描算法"<<endl;
		cout<<"0.退出"<<endl;
		cin>>flag;
		switch(flag){
		    case 1:
		        FCFS(a,n,start);break;
		    case 2:
		        SSTF(a, n, start);break;
            case 3:
                SCAN(a, n, start);break;
            case 0:
			    break;
		}
	}
	return 0;
}

代码比较简单,只是能体现这三个算法的原理罢了。

你可能感兴趣的:(操作系统)