进程调度三种算法的实现(FCFS,SJF,RR)

操作系统的又一次作业,模拟进程调度。这次用JAVA 实现了三种调度算法,包括FCFS(先到先服务),SJF(短作业优先),RR(轮转调度算法)。
eclipse导入了阿里的一个jar包,用于实现josn数据的转换。
jar包
废话就不多说了,直接上代码吧。

no.1

package com.box.process;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.box.process.tools.PCB;


public class MainRun {

    static int allTime=0;

    public static void main(String[] args) {
        while(true)
        {
        Scanner scanner=new Scanner(System.in);

        String processMessage=loadProcess();
        List pcbs=new ArrayList<>();
        JSONArray jsonArray=JSONArray.parseArray(processMessage);
        pcbs=JSONArray2ObjectList(jsonArray);

        System.out.println("请选择调度方法");
        System.out.println("1.优先数算法                    2.时间片轮转算法                   3.短作业优先算法                 4.退出");
        int choose = Integer.parseInt(scanner.next());
        if(choose==4)
            break;
        allTime = getAllTime(pcbs);
        switch (choose) {
        case 1:
            Cls();
            priority(pcbs);
            System.out.println();
            System.out.println();
            break;
        case 2:
            Cls();
            changeByTime(pcbs);
            System.out.println();
            System.out.println();
            break;
        case 3:
            Cls();
            shortTaskFrist(pcbs);
            System.out.println();
            System.out.println();
            break;
        default:
            return;
        }

        }

    }   



//返回所有进程运行时间的总和
    private static int getAllTime(List pcbs) {
        int allTime=0;

        for(PCB pcb:pcbs){
            allTime+=pcb.getRunTime();
        }
        return allTime;
    }




    private static void changeByTime(List pcbs) {
        List queueList=new ArrayList<>(); 
        List oldPcbs=new ArrayList<>();
        oldPcbs=pcbs;
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入时间片大小:");
        int time=scanner.nextInt();
        //原始信息
        System.out.println("原始信息:");
        System.out.println("\n进程名:\t"+"到达时间:\t"+"运行时间:\t"+"优先级:\t");
        for(int i=0;i
            System.out.print(pcbs.get(i).getName()+"\t");
            System.out.print(pcbs.get(i).getStartTime()+"\t");
            System.out.print(pcbs.get(i).getRunTime()+"\t");
            System.out.print(pcbs.get(i).getPriority()+"\t");
            System.out.println();
        }

        System.out.println();
        System.out.println("一个周期内,调度队列中每秒运行的进程如下:");
        //以一个时间片时间到或者在时间片内process运行完成,为一次循环进行。
        //直至所有process皆运行结束
        for(int i=0;i
            PCB current=null;
            int k=0;        
            for(int j=0;j
                PCB pcb=pcbs.get(j);
                if(pcb.getIsOver()==false){
                    if(pcb.getStartTime()<=i){
                        if(current==null){
                            current=pcb;
                            k=j;
                            break;
                        }
                    }
                }
            }
            pcbs.get(k).decrease();//当前进程的运行时间-1
            queueList.add(current.getName());
            System.out.print(current.getName());
            i++;//当前时间增加
            //该时间片内,该进程的运转情况,若提前结束,则跳出。时间片到,但未结束,则中止当前进程,进行下一进程。
            int j=1;    
            for(j=1;j
                if(pcbs.get(k).getIsOver()==false){
                    pcbs.get(k).decrease();
                    queueList.add(current.getName());
                    System.out.print(current.getName());
                    i++;//时间增加
                }
            }
            //当前进程未结束,但时间片到时,重新调整所有进程的就绪队列。
            //各个进程按照原来队列依次向前,中止进程放于队列最后(创建一个新的队列list,并赋值给pcbs)
            if(j==time&&pcbs.get(k).getIsOver()==false){    

                List newPcbs=new ArrayList<>();

                for(int n=0;n
                    if(n!=k&&pcbs.get(n).getIsOver()==false){
                        newPcbs.add(pcbs.get(n));
                    }
                }
                newPcbs.add(pcbs.get(k));
                pcbs=newPcbs;
            }

        }

        //等待时间=进程开始运行时间-进程到达时间
        //周转时间=进程最后一次运行时间-开始运行时间+1
        pcbs=oldPcbs;
        System.out.println();
        System.out.println("\n进程名:\t"+"等待时间:\t"+"周转时间:\t");
        for(int i=0;i
            System.out.print(pcbs.get(i).getName()+"\t");
            for(int k=0;k
                if(pcbs.get(i).getName().equals(queueList.get(k))){
                    System.out.print(k-pcbs.get(i).getStartTime());
                    System.out.print("\t");
                    break;
                }
            }

            for(int k=queueList.size()-1;k>=0;k--){
                if(pcbs.get(i).getName().equals(queueList.get(k))){
                    System.out.print((k-pcbs.get(i).getStartTime())+1);
                    System.out.print("\t");
                    break;
                }
            }
            System.out.println();
        }
    }




    private static void shortTaskFrist(List pcbs) {
        List queueList=new ArrayList<>(); 
        System.out.println("调度队列中每秒运行的进程如下:");

        //原始信息
                System.out.println("原始信息:");
                System.out.println("\n进程名:\t"+"到达时间:\t"+"运行时间:\t"+"优先级:\t");
                for(int i=0;i
                    System.out.print(pcbs.get(i).getName()+"\t");
                    System.out.print(pcbs.get(i).getStartTime()+"\t");
                    System.out.print(pcbs.get(i).getRunTime()+"\t");
                    System.out.print(pcbs.get(i).getPriority()+"\t");
                    System.out.println();
                }

        System.out.println();
        //1秒循环一次,每次执行运行时间最短的process,且该process的RunTime-1
        for(int i=0;i
            PCB current=null;
            int k=0;
            //find the shortest process
            //i表示当前时间
            for(int j=0;j
                PCB pcb=pcbs.get(j);

                if(pcb.getIsOver()==false){
                    if(pcb.getStartTime()<=i){
                        if(current==null){
                            current=pcb;
                        }else{
                            if (current.getRunTime()>pcb.getRunTime()){
                                current=pcb;
                                k=j;
                            }
                        }
                    }
                }
            }
            pcbs.get(k).decrease();
            queueList.add(current.getName());
            System.out.print(current.getName());
        }

        System.out.println();
        System.out.println("\n进程名:\t"+"等待时间:\t"+"周转时间:\t");
        for(int i=0;i
            System.out.print(pcbs.get(i).getName()+"\t");
            for(int k=0;k
                if(pcbs.get(i).getName().equals(queueList.get(k))){
                    System.out.print(k-pcbs.get(i).getStartTime());
                    System.out.print("\t");
                    break;
                }
            }

            for(int k=queueList.size()-1;k>=0;k--){
                if(pcbs.get(i).getName().equals(queueList.get(k))){
                    System.out.print((k-pcbs.get(i).getStartTime())+1);
                    System.out.print("\t");
                    break;
                }
            }
            System.out.println();
        }
    }




    private static void priority(List pcbs) {
        List queueList=new ArrayList<>(); 

        //原始信息
                System.out.println("原始信息:");
                System.out.println("\n进程名:\t"+"到达时间:\t"+"运行时间:\t"+"优先级:\t");
                for(int i=0;i
                    System.out.print(pcbs.get(i).getName()+"\t");
                    System.out.print(pcbs.get(i).getStartTime()+"\t");
                    System.out.print(pcbs.get(i).getRunTime()+"\t");
                    System.out.print(pcbs.get(i).getPriority()+"\t");
                    System.out.println();
                }

        System.out.println();
        System.out.println("调度队列中每秒运行的进程如下:");
        //1秒循环一次,每次执行优先级最高的process,且该process的RunTime-1
        for(int i=0;i
            PCB current=null;
            int k=0;
            //find the highest Priority process
            //i表示当前时间
            for(int j=0;j
                PCB pcb=pcbs.get(j);

                if(pcb.getIsOver()==false){
                    if(pcb.getStartTime()<=i){
                        if(current==null){
                            current=pcb;
                        }else{
                            if (current.getPriority()>pcb.getPriority()){
                                current=pcb;
                                k=j;
                            }
                        }
                    }
                }
            }
            pcbs.get(k).decrease();
            queueList.add(current.getName());
            System.out.print(current.getName());
        }

        System.out.println();
        System.out.println("\n进程名:\t"+"等待时间:\t"+"周转时间:\t");
        for(int i=0;i
            System.out.print(pcbs.get(i).getName()+"\t");
            for(int k=0;k
                if(pcbs.get(i).getName().equals(queueList.get(k))){
                    System.out.print(k-pcbs.get(i).getStartTime());
                    System.out.print("\t");
                    break;
                }
            }

            for(int k=queueList.size()-1;k>=0;k--){
                if(pcbs.get(i).getName().equals(queueList.get(k))){
                    System.out.print((k-pcbs.get(i).getStartTime())+1);
                    System.out.print("\t");
                    break;
                }
            }
            System.out.println();
        }
    }



    //利用阿里的jar包,将JSONArray内容复制给List,并返回
    private static List JSONArray2ObjectList(JSONArray jsonArray) {
        List pcbs=new ArrayList<>();
        for(int i=0;i
            JSONObject jsonObject=jsonArray.getJSONObject(i);
            PCB pcb=new PCB();
            pcb.Json2Object(jsonObject);
            pcbs.add(pcb);
        }
        return pcbs;
    }

    //导入JOB1文件到String字符串,即将各个process的内容导入
    private  static String loadProcess() {
        URL xmlpath=MainRun.class.getClassLoader().getResource("");

        String encoding="utf-8";

        try {
            File file=new File(xmlpath.toString().replace("file:/", "")+"com/box/process/JOB1.txt");
            if(file.isFile() && file.exists()){ 
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file),encoding);
                BufferedReader bufferedReader = new BufferedReader(read);
                StringBuffer buffer=new StringBuffer();
                String lineTxt = null;
                while((lineTxt = bufferedReader.readLine()) != null){
                    buffer.append(lineTxt);
                }
                String message=buffer.toString();
                read.close();
                return message;
            }else{
                System.out.println("打开文件失败");
            }
        } catch (Exception e) {
            System.out.println("加载文件失败");
            e.printStackTrace();
        }

        return null;
    }
    //清屏
    public static void Cls()
    {
        for(int i=0;i<30;i++)
        {
            System.out.println();
        }
    }

}

no.2

package com.box.process.tools;

import com.alibaba.fastjson.JSONObject;

public class PCB {
    private int runTime;  //运行时间
    private  int priority;   //优先级
    private  String name; //进程名称
    private boolean isOver; //是否运行结束
    private int startTime;  //开始运行时间


    public int getRunTime() {
        return runTime;
    }

    public void setRunTime(int runTime) {
        this.runTime = runTime;
    }


    public int getPriority() {
        return priority;
    }



    public void setPriority(int priority) {
        this.priority = priority;
    }



    public String getName() {
        return name;
    }



    public void setName(String name) {
        this.name = name;
    }



    public boolean getIsOver() {
        return isOver;
    }


    public void setIsOver(boolean isOver) {
        this.isOver = isOver;
    }


    public int getStartTime() {
        return startTime;
    }

    public void setStartTime(int startTime) {
        this.startTime = startTime;
    }

    public void Json2Object(JSONObject jsonObject) {
        setName(jsonObject.getString("name"));
        setStartTime(jsonObject.getInteger("startTime"));
        setIsOver(jsonObject.getBooleanValue("isOver"));
        setPriority(jsonObject.getIntValue("priority"));
        setRunTime(jsonObject.getIntValue("runTime"));
    }


    public void decrease() {
         runTime--;
         if(runTime<=0){
             isOver=true;
         }
    }


}

文本内容:

 [
        {
            "name": "P1",
            "startTime": 0,
            "runTime": 7,
            "priority": 5,
            "isOver":false
        },
        {
            "name": "P2",
            "startTime": 1,
            "runTime": 1,
            "priority": 1,
             "isOver":false
        },
        {
            "name": "P3",
            "startTime": 1,
            "runTime": 3,
            "priority": 4,
            "is_over":false
        },
        {
            "name": "P4",
            "startTime": 2,
            "runTime": 5,
            "priority": 3,
             "isOver":false
        },
        {
            "name": "P5",
            "startTime": 4,
            "runTime": 4,
            "priority": 2,
            "isOver":false
        }
    ]

注:所参考的博客资料,因为当时没有记录,现在写的时候返回去怎么也找不见了,所以前辈看见了,直接私聊我。

你可能感兴趣的:(操作系统)