java操作系统进程调度模拟-------先来先服务

2019-07-14 10:45发布

通过对进程调度算法的模拟,进一步理解进程的基本概念,加深对进程运行状态和进程调度过程、调度算法的理解。 github下载地址:https://github.com/musuixin/Operating-system-process-simulation 流程图: /** * 功能描述:PCB的父类,包含PCB的主要信息,以及方法。 * @date 2018.11.5 * @version 2.0 */ class PCB { int id; String pname; int daodatime; int fuwutime; int runtime=0; String state="w"; int priority=0; int startblock; int blocktime; PCB(){ } /** * 功能描述:用于非优先权进程调度的PCB构造函数,不包含优先权的构造,默认其为0 */ PCB(int id,String pname, int daodatime, int fuwutime,int startblock,int blocktime) { this.id=id; this.pname = pname; this.daodatime = daodatime; this.fuwutime = fuwutime; this.startblock=startblock; this.blocktime=blocktime; } /** * 功能描述:用于优先权调度的构造函数。 */ PCB(int id,String pname, int daodatime, int fuwutime,int priority,int startblock,int blocktime) { this.id=id; this.pname = pname; this.daodatime = daodatime; this.startblock=startblock; this.blocktime=blocktime; this.fuwutime = fuwutime; this.priority = priority; } /** * @return PCB原始信息 */ String information(){ return id+" "+pname+" "+daodatime+" "+priority+" "+fuwutime+" "+startblock+" "+blocktime; } /** * @return PCB当前信息 */ String messages(){ return id+" "+pname+" "+runtime+" "+priority+" "+state; } } import java.util.LinkedList; import java.util.Scanner; /*** * 功能描述:继承父类PCB,实现接口Comparable,先来先服务PCB * @date 2018.11.05 * @version 2.0 */ class FcFcPCB extends PCB implements Comparable{ FcFcPCB(int id,String pname, int daodatime, int fuwutime,int startblock,int blocktime) { super(id,pname,daodatime,fuwutime,startblock,blocktime); this.id=id; this.pname = pname; this.daodatime = daodatime; this.fuwutime = fuwutime; } /** * 功能描述 : 按到达时间进行排序 * @date 2018.11.05 */ @Override public int compareTo(Object o) { FcFcPCB st=(FcFcPCB)o; return this.daodatime-st.daodatime; } } public class FcFs { public static void main(String[] args){ System.out.println("+++++++++++++++++++++++++++++++++++++++++++++先来先服务+++++++++++++++++++++++++++++++++++++++++++"); Scanner input = new Scanner(System.in); LinkedList fcFcPCBS= new LinkedList<>(); //用于存放录入的进程 LinkedList block=new LinkedList<>(); //堵塞队列 LinkedList beReadyPCB = new LinkedList<>(); //就绪队列 LinkedList strings=new LinkedList<>(); //结束队列 while (true) { //循环录入进程信息 System.out.println("请依次输入进程ID,进程名,到达时间,服务时间,运行n时间后进入堵塞队列的时间,堵塞时间"); try { int id = input.nextInt(); String pname = input.next(); int dadatime = input.nextInt(); int fuwutime = input.nextInt(); int startblock=input.nextInt(); int blocktime=input.nextInt(); fcFcPCBS.add(new FcFcPCB(id,pname, dadatime, fuwutime,startblock,blocktime)); System.out.println("是否继续?(1继续,0结束)"); int i = input.nextInt(); if (i == 0) { fcFcPCBS.sort(FcFcPCB::compareTo); break; } } catch (Exception e){ System.out.println("输入有误!程序非正常运行!请程序运行程序"); break; } } //显示进程运行之前的信息 System.out.println("*******************************************进程运行初信息************************************************"); System.out.println("* 进程ID 进程名 到达时间 优先权 服务时间 nS进行后堵塞 堵塞时间 *"); for(int i=0;i 结果,显示各个时间片的运行情况: