美文网首页
[操作系统]磁盘调度

[操作系统]磁盘调度

作者: vouv | 来源:发表于2018-06-17 17:35 被阅读0次

    problem

    实验内容:
      编写一个磁盘调度程序,模拟操作系统对磁盘的调度。

    实验目的:
    本实验要求学生独立设计并实现磁盘调度模拟程序,以加深对磁盘调度特点和各种磁盘调度算法的理解。

    实验要求:
      可以随机输入磁道请求序列,当前磁头位置和磁头移动方向,支持先来先服务、最短寻道时间优先、扫描、循环扫描调度算法,能够输出磁头移动经过的磁道序列。具体信息见测试用例格式说明。

    测试用例格式如下:
    输入:

    磁盘调度算法

    当前磁头位置

    磁头移动方向

    磁道请求序列(磁道1,磁道2,磁道3,...)

    其中:

    (1) 调度算法选项为:

                   1----先来先服务
    
                   2----最短寻道时间优先
    
                   3----扫描法(SCAN)
    
                   4----循环扫描法(C-SCAN)
    

    (2) 磁头移动方向选项为:

    1----向磁头号增大方向移动

    0----向磁头号减小方向移动

    输出:

    磁头移动经过的磁道序列(磁道1,磁道2,磁道3)

    磁头移动经过的总磁道数

    测试用例

    测试输入 期待的输出
    1
    53
    1
    98,183,37,122,14,124,65,67
    53,98,183,37,122,14,124,65,67
    640

    ac code

    import java.util.*;
    
    
    
    public class Main {
    
        private static int method;
        private static int way;
        private static int now;
        private static int count = 0;
        private static int len;
        private static Integer[] inps;
    
        public static void main(String[] args) {
        // write your code here
            Scanner scanner = new Scanner(System.in);
            method = scanner.nextInt();
            now = scanner.nextInt();
            way = scanner.nextInt();
            scanner.nextLine();
            String str = scanner.nextLine();
            len = str.split(",").length;
            inps = new Integer[len];
            for (int i = 0;i < len; i++){
                inps[i] = new Integer(str.split(",")[i]);
            }
            handle();
        }
    
        private static void handle() {
            switch (method) {
                case 1:
                    fcfs();
                    break;
                case 2:
                    stf();
                    break;
                case 3:
                    scan();
                    break;
                case 4:
                    cscan();
                    break;
            }
        }
    
        private static void fcfs() {
            System.out.print(now);
            for (int i : inps) {
                count += Math.abs(i - now);
                now = i;
                System.out.print("," + now);
            }
            System.out.println();
            System.out.println(count);
        }
    
        private static void stf() {
            System.out.print(now);
            List<Integer> pages = getList();
            for (int i = 0 ;i < len;i++) {
                int max = Math.abs(pages.get(0) - now);
                int idx = 0;
                for(Integer ech : pages){
                    if (Math.abs(ech - now) < max) {
                        max = Math.abs(ech - now);
                        idx = pages.indexOf(ech);
                    }
                }
                System.out.print("," + pages.get(idx));
                count += Math.abs(now - pages.get(idx));
                now = pages.get(idx);
                pages.remove(idx);
            }
            System.out.println();
            System.out.println(count);
    
        }
    
        private static void scan() {
            System.out.print(now);
            Arrays.sort(inps);
            List<Integer> pages = getList();
            if (way == 1) {
                int idx = 0;
                for (Integer ech : pages) {
                    if (ech > now) {
                        idx = pages.indexOf(ech);
                        break;
                    }
                }
                for (int i = idx; i < len; i++) {
                    int ech = pages.get(i);
                    count += Math.abs(ech - now);
                    now = ech;
                    System.out.print("," + now);
                }
                if (idx != 0) {
                    for (int i = idx - 1; i >= 0; i--) {
                        int ech = pages.get(i);
                        count += Math.abs(ech - now);
                        now = ech;
                        System.out.print("," + now);
                    }
                }
            }else {
                int idx = 0;
                for (Integer ech : pages) {
                    if (now > ech) {
                        idx = pages.indexOf(ech);
                    }else {
                        break;
                    }
                }
                for (int i = idx; i >= 0; i--) {
                    int ech = pages.get(i);
                    count += Math.abs(ech - now);
                    now = ech;
                    System.out.print("," + now);
                }
                if (idx != 0) {
                    for (int i = idx + 1; i < len; i++) {
                        int ech = pages.get(i);
                        count += Math.abs(ech - now);
                        now = ech;
                        System.out.print("," + now);
                    }
                }
            }
            System.out.println();
            System.out.println(count);
        }
        private static void cscan() {
            System.out.print(now);
            Arrays.sort(inps);
            List<Integer> pages = getList();
            if (way == 1) {
                int idx = 0;
                for (Integer ech : pages) {
                    if (ech > now) {
                        idx = pages.indexOf(ech);
                        break;
                    }
                }
                for (int i = idx; i < len; i++) {
                    int ech = pages.get(i);
                    count += Math.abs(ech - now);
                    now = ech;
                    System.out.print("," + now);
                }
                if (idx != 0) {
                    for (int i = 0; i < idx; i++) {
                        int ech = pages.get(i);
                        count += Math.abs(ech - now);
                        now = ech;
                        System.out.print("," + now);
                    }
                }
            }else {
                int idx = 0;
                for (Integer ech : pages) {
                    if (now > ech) {
                        idx = pages.indexOf(ech);
                    }else {
                        break;
                    }
                }
                for (int i = idx; i >= 0; i--) {
                    int ech = pages.get(i);
                    count += Math.abs(ech - now);
                    now = ech;
                    System.out.print("," + now);
                }
                if (idx != 0) {
                    for (int i = len - 1; i > idx; i--) {
                        int ech = pages.get(i);
                        count += Math.abs(ech - now);
                        now = ech;
                        System.out.print("," + now);
                    }
                }
            }
            System.out.println();
            System.out.println(count);
        }
    
        private static LinkedList<Integer> getList(){
            return new LinkedList<>(Arrays.asList(inps));
        }
    
    
    }
    

    相关文章

      网友评论

          本文标题:[操作系统]磁盘调度

          本文链接:https://www.haomeiwen.com/subject/lfiweftx.html