磁盘调度算法

时间:2021-03-22 14:38:29
#include<iostream>  
#include<cstdio>  
#include<algorithm>  
#include<cmath>
#include <time.h>   
using namespace std;  
const int maxn = 100;  
int n, now, s, sum, nnow, everage, p[maxn], b[maxn], sp[maxn], lenp[maxn];  
//显示结果  
void show()  
{  
    sum = 0;  
	printf("磁盘请求的服务状况:");
	printf("%d",now);
    for(int i = 0;i < n; ++i)  
    {  
		printf("->%d ",sp[i]);
        sum += lenp[i];  
    }printf("\n");
	printf("移动的距离:");
	for(int j = 0;j<n;j++){
		printf("%d ",lenp[j]);
	}
    printf("\n");
    cout <<"平均寻道长度: "<<(double)sum/n <<"\n";  
}  
//先来先服务  
void FCFS()  
{  
    for(int i = 0;i < n; ++i)  
    {  
        sp[i] = p[i];  
        if(i)   lenp[i] = abs(sp[i-1] - sp[i]);  
        else    lenp[i] = abs(now  - sp[i]);  
    }  
}  
//最短寻道优先  
void SSTF()  
{  
    nnow = now;  
    int fl[maxn] = {0};  
    for(int i = 0;i < n; ++i)  
    {  
        int minx = 999999, pp;  
        for(int j = 0;j < n; ++j)  
        {  
            if(!fl[j] && abs(nnow - p[j]) < minx)  
            {  
                minx = abs(nnow - p[j]);  
                pp = j;  
            }  
        }  
        sp[i] = p[pp];  
        lenp[i] = minx;  
        nnow = p[pp];  
        fl[pp] = 1;  
    }  
}  
//扫描算法  
bool cmp(int a, int b)  
{  
    return a > b;  
}  
void SCAN()  
{  
    nnow = now;  
    int aa[maxn], bb[maxn], ak = 0, bk = 0;  
    for(int i = 0;i < n; ++i)  
    {  
        if(p[i] < nnow) aa[ak++] = p[i];  
        else bb[bk++] = p[i];  
    }  
    sort(aa, aa+ak,cmp);  
    sort(bb, bb+bk);  
    int k = 0;  
    for(int j = 0;j < bk; ++j)  
    {  
        sp[k] = bb[j];  
        lenp[k++] =  bb[j] - nnow;  
        nnow = bb[j];  
    }  
    for(int n = 0; n< ak; ++n)  
    {  
        sp[k] = aa[n];  
        lenp[k++] = nnow - aa[n];  
        nnow = aa[n];  
    }  
}  
//循环扫描算法  
void CSCAN()  
{  
    nnow = now;  
    int aa[maxn], bb[maxn], ak = 0, bk = 0;  
    for(int i = 0;i < n; ++i)  
    {  
        if(p[i] < nnow) aa[ak++] = p[i];  
        else bb[bk++] = p[i];  
    }  
    sort(aa, aa+ak);  
    sort(bb, bb+bk);  
    int k = 0;  
    for(int j = 0;j < bk; ++j)  
    {  
        sp[k] = bb[j];  
        lenp[k++] =  bb[j] - nnow;  
        nnow = bb[j];  
    }  
    for(int a = 0;a < ak; ++a)  
    {  
        sp[k] = aa[a];  
        lenp[k++] = abs(aa[a] - nnow);  
        nnow = aa[a];  
    }  
}  
int main()  
{  
    xbegin:  
		srand(time(NULL));     //添加随机数种子  
        n = rand() % 15 + 1;
		printf("总磁道数:%d\n",n);
        if(n <= 0 ||  n > 100)  
        {  
            cout <<"输入不合法,请重新输入!\n";  
            goto xbegin;  
        }  
    nowCD:  
		now = rand() % 100;
		printf("当前磁道:%d\n",now);
        if(now < 0)  
        {  
            cout <<"磁道不存在,请重新输入!";  
            goto nowCD;  
        }  
    pCD:  
		printf("随机磁盘序列:");
        for(int i = 0;i < n; ++i)  
        {  
            b[i] = rand() % 200 + 1;
			printf("%d  ",b[i]);
            if(p[i] < 0)  
            {  
                cout <<"输入中有不存在的磁道,请重新输入!\n";  
                goto pCD;  
            }
			
        } printf("\n"); 
    serve:  
        cout <<"  1、先来先服务算法(FCFS)\n";  
        cout <<"  2、最短寻道优先算法(SSTF)\n";  
        cout <<"  3、扫描算法(SCAN)\n";  
        cout <<"  4、循环扫描算法(CSCAN)\n";  
        cout <<"请输入所用磁盘调度的算法: ";  
        cin >> s;  
        if(s < 1 || s > 4)  
        {  
            cout <<"输入有误,请重新输入!\n";  
            goto serve;  
        }  
    work:  
        for(int k = 0;k < n; ++k) p[k] = b[k];  
        if(s == 1) FCFS();  
        else if(s == 2) SSTF();  
        else if(s == 3) SCAN();  
        else CSCAN();  
        show();  
    xend:  
        char ch;  
        cout <<"重新选择算法或重新输入数据(输入C选择算法,输入R重新输入数据): ";  
        cin >> ch;  
        if(ch == 'C') goto serve;  
        if(ch == 'R') goto xbegin;    
    return 0;  
}