计算机操作系统实验1

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;

class PCB {
    String name;
    int priority,
            needtime,
            cputime;

    PCB(String name,int priority,int needtime,int cputime){
        this.name = name;
        this.priority = priority;
        this.needtime = needtime;
        this.cputime = cputime;
    }
}
public class Scheduling {
    private boolean cpu_state = true;
    //true 空闲   , false 繁忙
    private int CPUTIME = 0;

    private LinkedList<PCB> initBox = new LinkedList<>();//记录初始进程
    private LinkedList<PCB> readyBox = new LinkedList<>();//就绪队列
    private LinkedList<PCB> cpuBox = new LinkedList<>();//cpu
    private LinkedList<PCB> completedBox = new LinkedList<>();//已完成队列

    private HashMap<String, Integer> RunTime = new HashMap<>();//创建的进程完成时的CPUTIME
    private HashMap<String, Integer> WaitingTime = new HashMap<>();//创建的进程的空等待时间(RunTime - needtime)

    //创建一个进程
    public boolean creat(int num) {
        if (num <= 0) return false;

        boolean num_state = true;
        while (num_state) {
            Scanner input = new Scanner(System.in);
            System.out.println("输入进程名(一个字符串),优先级(一个整数),所需时间(一个整数):");
            String name = input.nextLine();
            int priority = Integer.parseInt(input.nextLine());
            int needtime = Integer.parseInt(input.nextLine());
            readyBox.offer(new PCB(name, priority, needtime, 0));
            initBox.offer(new PCB(name, priority, needtime, 0));
            num--;
            if (num == 0)
                num_state = false;
        }

        System.out.println("NAME  NEEDTIME  PRIORITY");
        for (int i = 0; i < readyBox.size(); i++) {
            System.out.println(readyBox.get(i).name + "  " + "  " + readyBox.get(i).needtime + "  " + readyBox.get(i).priority);
        }

        while (readyBox.size() > 0) {
            dispatch();
            System.out.println("CPUTIME:" + CPUTIME);
            System.out.println("NAME  CPUTIME  NEEDTIME  PRIORITY  STATE");
            System.out.println(cpuBox.get(0).name + "  " + cpuBox.get(0).cputime + "  " + cpuBox.get(0).needtime + "  " + cpuBox.get(0).priority + "  working");
            for (int i = 0; i < readyBox.size(); i++) {
                if (readyBox.get(i).cputime != 0 && readyBox.get(i).needtime != 0) {
                    readyBox.get(i).cputime++;
                }
                System.out.println(readyBox.get(i).name + "  " + readyBox.get(i).cputime + "  " + readyBox.get(i).needtime + "  " + readyBox.get(i).priority + "  ready");
            }
            if (completedBox.size() > 0) {
                for (int i = 0; i < completedBox.size(); i++) {
//                    System.out.println(completedBox.get(i).name + "  " + completedBox.get(i).cputime + "  " + completedBox.get(i).needtime + "  " + completedBox.get(i).priority + "  finish");
                }
            }
            timeout();
        }
        System.out.println("CPUTIME:" + CPUTIME);
        System.out.println("NAME  CPUTIME  NEEDTIME  PRIORITY  STATE");
        for (int i = 0; i < completedBox.size(); i++) {
            System.out.println(completedBox.get(i).name + "  " + completedBox.get(i).cputime + "  " + completedBox.get(i).needtime + "  " + completedBox.get(i).priority + "  finish");
        }
        System.out.println("NAME  RunTime  WaitingTime"); //RunTime为进程运行完成时的CPUTIME
        for (int i = 0; i < initBox.size(); i++) {
            PCB t = initBox.get(i);
            System.out.println(t.name + "  " + RunTime.get(t.name) + "  " + WaitingTime.get(t.name));
        }
        return true;
    }

    //执行
    private boolean dispatch() {

        if (cpu_state) {
            if (!readyBox.isEmpty()) {
                sort();
                PCB ready = readyBox.poll();
                cpuBox.offer(new PCB(ready.name, ready.priority, ready.needtime, ready.cputime));
                cpu_state = false;
                cpuBox.peek().priority--;
                cpuBox.peek().needtime--;
                cpuBox.peek().cputime++;
                CPUTIME++;
                return true;
            }
        }
        return false;
    }

    //就绪队列将优先级最高的替换到队首
    private boolean sort() {
        PCB t = null;
        for (int j = 1; j < readyBox.size(); j++) {
            if (readyBox.get(0).priority < readyBox.get(j).priority) {
                t = readyBox.get(0);
                readyBox.set(0, readyBox.get(j));
                readyBox.set(j, t);
            }
        }
        return true;
    }

    //时间片完
    private boolean timeout() {
        if (!cpu_state) {//cpu繁忙
            PCB cpu = cpuBox.poll();
            if (cpu.needtime == 0) {
                completedBox.offer(new PCB(cpu.name, cpu.priority, cpu.needtime, cpu.cputime));
                RunTime.put(cpu.name, CPUTIME - 1);
                int t = 0;
                for (int i = 0; i < initBox.size(); i++) {
                    if (initBox.get(i).name.equals(cpu.name)) {
                        t = initBox.get(i).needtime;
                    }
                }
                WaitingTime.put(cpu.name, CPUTIME - 1 - t);
            } else {
                readyBox.offer(new PCB(cpu.name, cpu.priority, cpu.needtime, cpu.cputime));
            }
            cpu_state = true;
        }
        return true;
    }
    public static void main(String[] args) {
        Scheduling demo = new Scheduling();
        int num = 5;//创建的进程数
        demo.creat(num);


    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值