首页 > 编程语言 > 详细

磁盘管理Scan算法

时间:2019-05-30 00:20:15      阅读:273      评论:0      收藏:0      [点我收藏+]

import java.util.Random;
import java.lang.Math;
import java.util.LinkedList;
public class Scan {
    public static void main(String[] args) {
        int flag = 1;                //设置磁头移动方向
        int[] arr = new int[2];
        int count = 0;                //存储移动次数
        int sum = 0;                //存储移动总距离
        int current = 100;            //初始化当前磁头位置
        LinkedList<Integer> l = new LinkedList<Integer>();   
        Scan.init(l);                //初始化进程队列  
        System.out.println("请求表中已存在待访问磁道:");
        for(Integer i : l) {
            System.out.println(i+"\t");
        }
        System.out.println("=======================================");
        
        while(!l.isEmpty()) {        //队列非空
            if(flag == 1) {            //如果flag为1,
                int max = l.get(0);
                for(Integer i : l){
                    if(max < i) {
                        max = i;
                    }
                }
                if(max < current) {//但所有磁道都小于当前磁道,
                    flag = 0;       //将flag置0
                }
            }else {                    //如果flag为0,
                int min = l.get(0);
                for(Integer i : l){
                    if(min > i) {
                        min = i;
                    }
                }
                if(min > current) {//但所有磁道都大于当前磁道,
                    flag = 1;       //将flag置1
                }
            }
            if(Math.random() < 0.5) {//随机生成0-1之间的浮点数,小于0.5则接受请求,大于0则进行磁盘调度
                Scan.request(l);
            }else if(flag ==1){        //flag为1,磁头向内移动
                arr = Scan.dispatch1(current, l);
                current = arr[0];    //获取当前磁道
                sum += arr[1];        //获取移动距离
                count++;            //移动次数加1
            }else if(flag == 0) {    //flag为0,磁头向外移动
                arr = Scan.dispatch2(current, l);
                current = arr[0];
                sum += arr[1];
                count++;
            }
        }
        System.out.println("访问次数: "+count);
        System.out.println("磁头移动总距离: "+sum);
        float ave = (float)sum/count;
        System.out.println("平均寻道长度: "+ave);
    }
    public static void init(LinkedList<Integer> l) {//初始化请求进程表
        Random ran = new Random();
        for(int i = 0;i < 4;i++) {
            l.add(ran.nextInt(200));
        }
    }
    public static void request(LinkedList<Integer> l) {//接受请求
        Random ran = new Random();
        int a = ran.nextInt(200);
        l.add(a);    //随机加入一个待访问磁道
        System.out.println("增加待访问磁道"+a);
        System.out.println("当前需访问的磁道号:");
        for(Integer i : l){
            System.out.print(i+"\t");
        }
        System.out.println();
        System.out.println("=======================================");
    }
    public static int[] dispatch1(int current,LinkedList<Integer> l) {
        int[] arr = new int[2];                            //存储当前磁道和移动距离
        int current2 = 0;                                //获取访问后的磁道
        int index = 0;                                    //进程在队列中的下标
        int length = 0;                                    //磁道移动长度
        for(Integer i : l){
           if(i > current) {
               index = l.indexOf(i);
               break;
           }
        }
        for(Integer i : l) {
            if(i > current && i-current <= l.get(index)-current) {//如果该磁道大于当前磁道并且移动距离较小
                index = l.indexOf(i);                //重置要访问的磁道
                length = i - current;                //更改移动距离
                current2 = i;                        //访问后的磁道
            }else {
                continue;
            }
        }
        
        arr[0] = current2;                            //存储移动后的磁道
        arr[1] = length;                            //存储移动距离
        System.out.println("当前磁头移向: "+l.get(index));
        System.out.println("=======================================");
        l.remove(index);                            //移除进程
        
        return arr;
    }
    
    //dispatch2作用同dispatch1,磁头移动方向与dispatch1相反  
    public static int[] dispatch2(int current,LinkedList<Integer> l) {
        int[] arr = new int[2];
        int current2 = 0;
        int index = 0;
        int length = 0;
        for(Integer i : l){
            if(i < current) {
                index = l.indexOf(i);
                break;
            }
         }
        for(Integer i : l) {
            if(i < current && current - i <= current-l.get(index)) {
                index = l.indexOf(i);
                length = current - i;
                current2 = i;
            }else {
                continue;
            }
        }
        
        arr[0] = current2;
        arr[1] = length;
        System.out.println("当前磁头移向: "+l.get(index));
        System.out.println("=======================================");
        l.remove(index);
        return arr;
    }
}

磁盘管理Scan算法

原文:https://www.cnblogs.com/cky-2907183182/p/10946523.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!