OS实验-模拟磁盘调度算法

磁盘调度算法:
1、最短寻道时间优先调度算法
2、电梯调度算法

最短寻道时间优先调度算法
电梯调度算法

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const ll maxn=1e18;
const ll minn=-1e18;
const ll mod=1000000007;
const ll MAX=100005;
bool cmp(ll a,ll b){return a>b;}
int a[10]={100,55,58,39,18,90,160,150,38,184};
int  ans=0;
const int n=10;


//最短寻道时间优先调度算法:
void SSTF(){
    for(int i=0 ; i<n-1 ; i++){
        int pos=i+1;
        for(int j=i+1; j<n ; j++){
            if(abs(a[j]-a[i])<abs(a[pos]-a[i])){
                pos=j;
            }
        }
        int temp=a[pos];
        a[pos]=a[i+1];
        a[i+1]=temp;

    }
    for(int i=1 ; i<n ; i++){
        ans+=(abs(a[i]-a[i-1]));
    }
}

//电梯调度算法
void SCAN()
{
    int fir=a[0];
    sort(a,a+10);
    int pos=0;
    while(a[pos]!=fir){pos++;}
    if(pos==0){ //升序扫描
        cout<<"调度顺序是:"<<endl;
        cout<<a[pos]<<" ";
        for(int i=1 ; i<n ; i++){
            ans+=(abs(a[i]-a[i-1]));
            cout<<a[i]<<" ";
        }
        cout<<" 平均移动长度是:";
        printf("%.1f\n",(1.0*ans)/(n-1));
    }
    else if(pos==n-1){
        cout<<"调度顺序是:"<<endl;
        cout<<a[pos]<<" ";
        for(int i=n-2 ; i>=0 ; i--){
            ans+=(abs(a[i]-a[i+1]));
            cout<<a[i]<<" ";
        }
        cout<<" 平均移动长度是:";
        printf("%.1f\n",(1.0*ans)/(n-1));

    }
    else{
            cout<<"先左后右:"<<a[pos]<<" ";
            int ans1=0;
            for(int i=pos+1 ;i<n; i++){
                ans1+=(abs(a[i]-a[i-1]));
                cout<<a[i]<<" ";
            }
            ans1+=(a[n-1]-a[pos-1]);
            for(int i=pos-1 ;i>0; i--){
                ans1+=(abs(a[i]-a[i-1]));
                cout<<a[i]<<" ";
            }
            cout<<a[0]<<" 平均移动长度是:";
            printf("%.1f\n",(1.0*ans1)/(n-1));



            cout<<"先右后左:";
            int ans2=0;
            for(int i=pos ;i>0; i--){
                ans2+=(abs(a[i]-a[i-1]));
                cout<<a[i]<<" ";
            }
            cout<<a[0]<<" ";
            ans2+=(abs(a[pos+1]-a[0]));
            for(int i=pos+1 ; i<n-1 ; i++){
                ans2+=(abs(a[i]-a[i+1]));
                cout<<a[i]<<" ";
            }
            cout<<a[n-1]<<" ";
            cout<<"平均移动长度是:";
            printf("%.1f\n",(1.0*ans2)/(n-1));
    }
}
void print(){
    cout<<"调度顺序是:"<<endl;
    for(int i=0 ; i<10 ; i++){
            cout<<a[i]<<" ";
    }
}

void init(){
    int type;
    cout<<"磁盘调度算法:"<<endl;
    cout<<"初始默认顺序是:"<<endl;
    print();
    cout<<endl<<"请输入所需要调度的算法:(1-最短寻道时间优先调度算法,2-电梯调度算法"<<endl;
    cin>>type;
    if(type==1){
        SSTF();
        print();
        printf("平均移动长度是:%.1f\n",(1.0*ans)/(n-1));
    }
    else{
        SCAN();
    }
}

int main()
{
    init();
    return 0;
}

//100,55,58,39,18,90,160,150,38,184

//18 38 39 55 58 90 100 150 160 184
//184 160 150 100 90 58 55 39 38 18


//先左后右:100 150 160 184 90 58 55 39 38 18
//50 10 24 94 32 3 16 1 20=250

//平均移动长度是:250


//先右后左:100 90 58 55 39 38 18 150 160 184
//10 32 3 16 1 20 132 10 24=249
// 平均移动长度是:298


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ROY_1997

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值