操作系统实验四:磁盘调度算法(scan电梯算法)c++实现

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

#define max_num 9

int sort_cidao[max_num]={0};//磁道请求序列
int flag1=0,flag2=0;//记录磁道移动的距离
int count1=0;

void init_cidao(){

    for(int i=0;i<max_num;i++){
        sort_cidao[i]=rand()%200;
    }
    for(int i=0;i<max_num;i++){
        cout<<sort_cidao[i]<<' ';
    }
    cout<<endl;
    sort(sort_cidao,sort_cidao+max_num);
    for(int i=0;i<max_num;i++){
        cout<<sort_cidao[i]<<' ';

    }
    cout<<endl;
}


int main(){
    init_cidao();
    int t_flag=100;//用来标记磁道初始的位置 假设为100
    int index_flag=0;//磁道号增加方向上最近的一个磁道
    int i_flag=0;//记录最近磁道序列所在的下标
	int flag=0;
    for(int i=0;i<max_num;i++){
        if(t_flag<=sort_cidao[i]){
            index_flag=sort_cidao[i];
            i_flag=i;
            flag=1;
            break;
        }
    }
   
   for(int i=i_flag;i<max_num-1;i++){
   		flag1+=sort_cidao[i+1]-sort_cidao[i];
   }
   flag1+=sort_cidao[i_flag]-t_flag;
   flag2+=sort_cidao[max_num-1]-sort_cidao[i_flag-1];
   for(int i=i_flag-1;i>=0;i--){
   		flag2+=sort_cidao[i]-sort_cidao[i-1];
   }
   	
    //计算平均寻道长度
    int total=0;
    double re=0;
    total=flag1+flag2;
    re=total/(max_num*1.0);
    cout<<"平均寻道长度:"<<re<<endl;
    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是几种常见磁盘调度算法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、付费专栏及课程。

余额充值