使用动态优先权的进程调度算法模拟

1、实验目的

通过动态优先权算法的模拟加深对进程概念进程调度过程的理解。

2、实验内容

(1)实现对N个进程采用动态优先权优先算法的进程调度。
(2)每个用来标识进程的进程控制块PCB用结构来描述,包括以下字段:

•••• 进程标识数 ID。
•••• 进程优先数 PRIORITY,并规定优先数越大的进程,其优先权越高。
•••• 进程已占用的CPU时间CPUTIME。
•••• 进程还需占用的CPU时间ALLTIME。当进程运行完毕时,ALLTIME变为0。•••• 进程的阻塞时间STARTBLOCK,表示当进程再运行STARTBLOCK个时间片后,将进入阻塞状态。
•••• 进程被阻塞的时间BLOCKTIME,表示已足赛的进程再等待BLOCKTIME个时间片后,将转换成就绪状态。
•••• 进程状态START。
•••• 队列指针NEXT,用来将PCB排成队列。

(3)优先数改变的原则:

•••进程在就绪队列中呆一个时间片,优先数加1。
•••进程每运行一个时间片,优先数减3。

(4)假设在调度前,系统中有5个进程,它们的初始状态如下:

ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
0 9 0 3 2 3 READY
1 38 0 3 -1 0 READY
2 30 0 6 -1 0 READY
3 29 0 3 -1 0 READY
4 0 0 4 -1 0 READY

(5) 为了清楚的观察各进程的调度过程,程序应将每个时间片内的情况显示出来,参照的具体格式如下:

3、思考

(1)实际进程调度中,除了按调度算法选择下一个执行的进程外,还需要处理哪些工作?

(2)为什么对进程的优先数可按上述原则进行修改?


4、我的Java代码

PCBNode

package dynamic_priority;
/** * Time:2020/12/4 20:33 * Author: han1254 * Email: 1254763408@qq.com * Function: */
public class PCBNode {
   
    private int ID;
    private int priority;
    private int cpuTime;
    private int allTime;
    private int startBlock;
    private int blockTime;
    private PState state;

    public PCBNode(int ID,
                   int priority,
                   int cpuTime,
                   int allTime,
                   int startBlock,
                   int blockTime) {
   
        this.ID = ID;
        this.priority = priority;
        this.cpuTime = cpuTime;
        this.allTime = allTime;
        this.startBlock = startBlock;
        this.blockTime = blockTime;
        this.state = PState.READY;
    }

    public int getID() {
   
        return ID;
    }

    public int getPriority() {
   
        return priority;
    }

    public int getCpuTime() {
   
        return cpuTime;
    }

    public int getAllTime() {
   
        return allTime;
    }

    public int getStartBlock() {
   
        return startBlock;
    }

    public int getBlockTime() {
   
        return blockTime;
    }

    public PState getState() {
   
        return state;
    }


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

    public void setCpuTime(int cpuTime) {
   
        this.cpuTime = cpuTime;
    }

    public void setAllTime(int allTime) {
   
        this.allTime = allTime;
    }

    public void setStartBlock(int startBlock) {
   
        this.startBlock = startBlock;
    }

    public void setBlockTime(int blockTime) {
   
        this.blockTime = blockTime;
    }

    public void setState(PState state) {
   
        this.state = state;
    }

}

PState

package dynamic_priority;
/** * Time:2020/12/4 20:34 * Author: han1254 * Email: 1254763408@qq.com * Function: */
public enum PState {
   
    READY,

    BLOCK,

    RUNNING
}

主体部分

package dynamic_priority;
import java.util.ArrayList;
import java.util.List;
/** * Time:2020/12/4 20:35 * Author: han1254 * Email: 1254763408@qq.com * Function: */
public class StartProcess3 {
   
    private PCBNode runningNode;
    private PCBNode tempRunningNode;
    // 我认为应该有一个变量记录运行态的进程的StartBlockTime,
    // 当这个进程进入阻塞态后,把这个值再赋给该进程的PCB,因为这个进程有可能会
    // 再次进入阻塞,如果不恢复的话,这个值在该进程进入阻塞队列后,就会一直为0
    private int recorderStartBlockTime = -1;
    private int timeScale = 0;
    private List<PCBNode> readyList = new ArrayList<>();
    private List<PCBNode> blockList = new ArrayList<>();
    public static void main(String[] args) {
   
        StartProcess3 process = new StartProcess3();
        process.initProcesses();
        process.start();
    }

    private void initProcesses() {
   
        readyList.add(new PCBNode(0, 9, 0, 3, 2, 3));
        readyList.add(new PCBNode(1, 38, 0, 3, -1, 0));
        readyList.add(new PCBNode(2, 30, 0, 6, -1, 0));
        readyList.add(new PCBNode(3, 29, 0, 3, -1, 0));
        readyList.add(new PCBNode(4, 0, 0, 4, -1, 0));
    }

    private void start() {
   
        while (!readyList.isEmpty() || !blockList.isEmpty()) {
   
            timeScale++;
            runningNode = getMaxPriorityNode(readyList);
            runningNode.setState(PState.RUNNING);
            if (runningNode != tempRunningNode) {
   
                recorderStartBlockTime = runningNode.getStartBlock();
            }
            runningOneTimeProcess();
            printCurrentSate();
        }
    }

    /** * 获得就绪队列中优先级最大的项目,并且从就绪队列中移除 * @param list * @return */
    private PCBNode getMaxPriorityNode(List<PCBNode> list) {
   
        PCBNode tempMax = list.get(0);
        for (PCBNode node : list) {
   
            if (node.getPriority() > tempMax.getPriority()) {
   
                tempMax = node;
            }
        }
        return tempMax;
    }

    /** * 运行一个时间片 */
    private void runningOneTimeProcess() {
   

        runningNode.setPriority(runningNode.getPriority() - 3);
        runningNode.setCpuTime(runningNode.getCpuTime() + 1);
        runningNode.setAllTime(runningNode.getAllTime() - 1);
        int tempRunningNodeAllTime = runningNode.getAllTime();
        int tempRunningNodeStartBlockTime = runningNode.getStartBlock();
        if (tempRunningNodeAllTime > 0) {
   //如果运行态的进程的AllTime大于零,则说明他还需要时间片
            if (tempRunningNodeStartBlockTime > 0) {
   //如果运行态的进程的StartBlockTime大于零,需要减一
                runningNode.setStartBlock(tempRunningNodeStartBlockTime - 1);
                if (runningNode.getStartBlock() == 0) {
   //如果StartBlockTime减一后变为0,则说明该进程需要进入阻塞队列
                    readyList.remove(runningNode);
                    blockList.add(runningNode);
                    runningNode.setState(PState.BLOCK);
                    runningNode.setStartBlock(recorderStartBlockTime);//在这里,我将原来记录的StartBlockTime再次赋值给该进程
                }
            }
        } else {
   //如果运行态的AllTime小于等于零,则说明该进程运行完毕,不需要再进入就绪或者阻塞队列
            readyList.remove(runningNode);
        }


        // 对就绪队列中所有的进程进行优先级加一操作(除了runningNode)
        for (PCBNode node :
                readyList) {
   
            if (node.getID() == runningNode.getID()) {
   
                continue;
            }
            node.setPriority(node.getPriority() + 1);
        }

        //对所有阻塞队列中的线程的BlockTime减一
        for (int i = 0; i < blockList.size(); i++) {
   
            PCBNode node = blockList.get(i);
            node.setBlockTime(node.getBlockTime() - 1);
            if (node.getBlockTime() == 0) {
   
                node.setState(PState.READY);
                readyList.add(node);
                blockList.remove(i);
            }
        }
        tempRunningNode = runningNode;
        runningNode = null;
    }


    private void printCurrentSate() {
   
        System.out.println("当前时间片:"+timeScale);
        if (tempRunningNode == null) {
   
            System.out.println("no process is running!");
        } else {
   
            System.out.println("RUNNING PROG: " + tempRunningNode.getID());
        }

        System.out.println("运行完本时间片后的各个进程状态:");

        System.out.print("Ready-Queue:");
        for (PCBNode node :
                readyList) {
   
            // System.out.println(node.getID()+"priority:"+node.getPriority()+" cupTime:"+node.getCpuTime()+" blockTime:"+node.getBlockTime()+" startBlockTime:"+node.getStartBlock());
            System.out.print("--->" + node.getID());
        }

        System.out.print("\nBlock-Queue:");
        for (PCBNode node :
                blockList) {
   
            // System.out.println(node.getID()+"priority:"+node.getPriority()+" cupTime:"+node.getCpuTime()+" blockTime:"+node.getBlockTime()+" startBlockTime:"+node.getStartBlock());
            System.out.println("--->" + node.getID());
        }
        System.out.println();
        for (PCBNode node :
                readyList) {
   
            System.out.println(node.getID()+">>>priority:"+node.getPriority()+" cupTime:" + node.getCpuTime() + " allTime:"+node.getAllTime() + " blockTime:"+node.getBlockTime()+" startBlockTime:"+node.getStartBlock());
        }
        for (PCBNode node :
                blockList) {
   
            System.out.println(node.getID()+">>>priority:"+node.getPriority()+" cupTime:" + node.getCpuTime()+ " allTime:"+node.getAllTime() + " blockTime:"+node.getBlockTime()+" startBlockTime:"+node.getStartBlock());
        }

        System.out.println("*****************************************************************");
        System.out.println();
    }
}

输出的结果

当前时间片:1
RUNNING PROG: 1
运行完本时间片后的各个进程状态:
Ready-Queue:--->0--->1--->2--->3--->4
Block-Queue:
0>>>priority:10 cupTime:0 allTime:3 blockTime:3 startBlockTime:2
1>>>priority:35 cupTime:1 allTime:2 blockTime:0 startBlockTime:-1
2>>>priority:31 cupTime:0 allTime:6 blockTime:0 startBlockTime:-1
3>>>priority:30 cupTime:0 allTime:3 blockTime:0 startBlockTime:-1
4>>>priority:1 cupTime:0 allTime:4 blockTime:0 startBlockTime:-1
*****************************************************************

当前时间片:2
RUNNING PROG: 1
运行完本时间片后的各个进程状态:
Ready-Queue:--->0--->1--->2--->3--->4
Block-Queue:
0>>>priority:11 cupTime:0 allTime:3 blockTime:3 startBlockTime:2
1>>>priority:32 cupTime:2 allTime:1 blockTime:0 startBlockTime:-1
2>>>priority:32 cupTime:0 allTime:6 blockTime:0 startBlockTime:-1
3>>>priority:31 cupTime:0 allTime:3 blockTime:0 startBlockTime:-1
4>>>priority:2 cupTime:0 allTime:4 blockTime:0 startBlockTime:-1
*****************************************************************

当前时间片:3
RUNNING PROG: 1
运行完本时间片后的各个进程状态:
Ready-Queue:--->0--->2--->3--->4
Block-Queue:
0>>>priority:12 cupTime:0 allTime:3 blockTime:3 startBlockTime:2
2>>>priority:33 cupTime:0 allTime:6 blockTime:0 startBlockTime:-1
3>>>priority:32 cupTime:0 allTime:3 blockTime:0 startBlockTime:-1
4>>>priority:3 cupTime:0 allTime:4 blockTime:0 startBlockTime:-1
*****************************************************************

当前时间片:4
RUNNING PROG: 2
运行完本时间片后的各个进程状态:
Ready-Queue:--->0--->2--->3--->4
Block-Queue:
0>>>priority:13 cupTime:0 allTime:3 blockTime:3 startBlockTime:2
2>>>priority:30 cupTime:1 allTime:5 blockTime:0 startBlockTime:-1
3>>>priority:33 cupTime:0 allTime:3 blockTime:0 startBlockTime:-1
4>>>priority:4 cupTime:0 allTime:4 blockTime:0 startBlockTime:-1
*****************************************************************

当前时间片:5
RUNNING PROG: 3
运行完本时间片后的各个进程状态:
Ready-Queue:--->0--->2--->3--->4
Block-Queue:
0>>>priority:14 cupTime:0 allTime:3 blockTime:3 startBlockTime:2
2>>>priority:31 cupTime:1 allTime:5 blockTime:0 startBlockTime:-1
3>>>priority:30 cupTime:1 allTime:2 blockTime:0 startBlockTime:-1
4>>>priority:5 cupTime:0 allTime:4 blockTime:0 startBlockTime:-1
*****************************************************************

当前时间片:6
RUNNING PROG: 2
运行完本时间片后的各个进程状态:
Ready-Queue:--->0--->2--->3--->4
Block-Queue:
0>>>priority:15 cupTime:0 allTime:3 blockTime:3 startBlockTime:2
2>>>priority:28 cupTime:2 allTime:4 blockTime:0 startBlockTime:-1
3>>>priority:31 cupTime:1 allTime:2 blockTime:0 startBlockTime:-1
4>>>priority:6 cupTime:0 allTime:4 blockTime:0 startBlockTime:-1
*****************************************************************

当前时间片:7
RUNNING PROG: 3
运行完本时间片后的各个进程状态:
Ready-Queue:--->0--->2--->3--->4
Block-Queue:
0>>>priority:16 cupTime:0 allTime:3 blockTime:3 startBlockTime:2
2>>>priority:29 cupTime:2 allTime:4 blockTime:0 startBlockTime:-1
3>>>priority:28 cupTime:2 allTime:1 blockTime:0 startBlockTime:-1
4>>>priority:7 cupTime:0 allTime:4 blockTime:0 startBlockTime:-1
*****************************************************************

当前时间片:8
RUNNING PROG: 2
运行完本时间片后的各个进程状态:
Ready-Queue:--->0--->2--->3--->4
Block-Queue:
0>>>priority:17 cupTime:0 allTime:3 blockTime:3 startBlockTime:2
2>>>priority:26 cupTime:3 allTime:3 blockTime:0 startBlockTime:-1
3>>>priority:29 cupTime:2 allTime:1 blockTime:0 startBlockTime:-1
4>>>priority:8 cupTime:0 allTime:4 blockTime:0 startBlockTime:-1
*****************************************************************

当前时间片:9
RUNNING PROG: 3
运行完本时间片后的各个进程状态:
Ready-Queue:--->0--->2--->4
Block-Queue:
0>>>priority:18 cupTime:0 allTime:3 blockTime:3 startBlockTime:2
2>>>priority:27 cupTime:3 allTime:3 blockTime:0 startBlockTime:-1
4>>>priority:9 cupTime:0 allTime:4 blockTime:0 startBlockTime:-1
*****************************************************************

当前时间片:10
RUNNING PROG: 2
运行完本时间片后的各个进程状态:
Ready-Queue:--->0--->2--->4
Block-Queue:
0>>>priority:19 cupTime:0 allTime:3 blockTime:3 startBlockTime:2
2>>>priority:24 cupTime:4 allTime:2 blockTime:0 startBlockTime:-1
4>>>priority:10 cupTime:0 allTime:4 blockTime:0 startBlockTime:-1
*****************************************************************

当前时间片:11
RUNNING PROG: 2
运行完本时间片后的各个进程状态:
Ready-Queue:--->0--->2--->4
Block-Queue:
0>>>priority:20 cupTime:0 allTime:3 blockTime:3 startBlockTime:2
2>>>priority:21 cupTime:5 allTime:1 blockTime:0 startBlockTime:-1
4>>>priority:11 cupTime:0 allTime:4 blockTime:0 startBlockTime:-1
*****************************************************************

当前时间片:12
RUNNING PROG: 2
运行完本时间片后的各个进程状态:
Ready-Queue:--->0--->4
Block-Queue:
0>>>priority:21 cupTime:0 allTime:3 blockTime:3 startBlockTime:2
4>>>priority:12 cupTime:0 allTime:4 blockTime:0 startBlockTime:-1
*****************************************************************

当前时间片:13
RUNNING PROG: 0
运行完本时间片后的各个进程状态:
Ready-Queue:--->0--->4
Block-Queue:
0>>>priority:18 cupTime:1 allTime:2 blockTime:3 startBlockTime:1
4>>>priority:13 cupTime:0 allTime:4 blockTime:0 startBlockTime:-1
*****************************************************************

当前时间片:14
RUNNING PROG: 0
运行完本时间片后的各个进程状态:
Ready-Queue:--->4
Block-Queue:--->0

4>>>priority:14 cupTime:0 allTime:4 blockTime:0 startBlockTime:-1
0>>>priority:15 cupTime:2 allTime:1 blockTime:2 startBlockTime:2
*****************************************************************

当前时间片:15
RUNNING PROG: 4
运行完本时间片后的各个进程状态:
Ready-Queue:--->4
Block-Queue:--->0

4>>>priority:11 cupTime:1 allTime:3 blockTime:0 startBlockTime:-1
0>>>priority:15 cupTime:2 allTime:1 blockTime:1 startBlockTime:2
*****************************************************************

当前时间片:16
RUNNING PROG: 4
运行完本时间片后的各个进程状态:
Ready-Queue:--->4--->0
Block-Queue:
4>>>priority:8 cupTime:2 allTime:2 blockTime:0 startBlockTime:-1
0>>>priority:15 cupTime:2 allTime:1 blockTime:0 startBlockTime:2
*****************************************************************

当前时间片:17
RUNNING PROG: 0
运行完本时间片后的各个进程状态:
Ready-Queue:--->4
Block-Queue:
4>>>priority:9 cupTime:2 allTime:2 blockTime:0 startBlockTime:-1
*****************************************************************

当前时间片:18
RUNNING PROG: 4
运行完本时间片后的各个进程状态:
Ready-Queue:--->4
Block-Queue:
4>>>priority:6 cupTime:3 allTime:1 blockTime:0 startBlockTime:-1
*****************************************************************

当前时间片:19
RUNNING PROG: 4
运行完本时间片后的各个进程状态:
Ready-Queue:
Block-Queue:
*****************************************************************