Java~操作系统实验按优先数调度算法实现处理器调度

一、实验内容

按优先数调度算法实现处理器调度。

二、实验目的

在采用多道程序设计的系统中,往往有若干个进程同时处于就绪状态。当就绪进程个数大于处理器数时,就必须依照某种策略来决定哪些进程优先占用处理器。本实验模拟在单处理器情况下的处理器调度,帮助学生加深了解处理器调度的工作。

三、实验原理

设计一个按优先数调度算法实现处理器调度的程序。
(1) 假定系统有五个进程,每一个进程用一个进程控制块PCB来代表,进程控制块的格式为:
在这里插入图片描述

class PCB {
    public String pcbName;
    public PCB next;
    public int time;
    public int priority;
    public char state = 'R';

    public PCB(String pcbName, int time, int priority) {
        this.pcbName = pcbName;
        this.time = time;
        this.priority = priority;
    }

    @Override
    public String toString() {
        return "进程名:" + pcbName +  "  要求运行时间:" + time + "  优先级数:" + priority
                 + "  状态:" + state;
    }
}

其中,进程名——作为进程的标识,假设五个进程的进程名分别为P1,P2,P3,P4,P5。
指针——按优先数的大小把五个进程连成队列,用指针指出下一个进程的进程控制块的首地址,最后一个进程中的指针为“0”。
要求运行时间——假设进程需要运行的单位时间数。
优先数——赋予进程的优先数,调度时总是选取优先数大的进程先执行。
状态——可假设有两种状态,“就绪”状态和“结束”状态。五个进程的初始状态都为“就绪”,用“R”表示,当一个进程运行结束后,它的状态为“结束”,用“E”表示。
(2) 在每次运行你所设计的处理器调度程序之前,为每个进程任意确定它的“优先数”和“要求运行时间”。
(3) 为了调度方便,把五个进程按给定的优先数从大到小连成队列。用一单元指出队首进程,用指针指出队列的连接情况。例:
在这里插入图片描述

(4) 处理器调度总是选队首进程运行。采用动态改变优先数的办法,进程每运行一次优先数就减“1”。由于本实验是模拟处理器调度,所以,对被选中的进程并不实际的启动运行,而是执行:
优先数-1
要求运行时间-1
来模拟进程的一次运行。

System.out.println("开始执行" + cur.pcbName + "进程");
            System.out.println(cur);
            //cur的运行优先级-1 运行时间-1
            cur.priority -= 1;
            cur.time -= 1;
            System.out.println(cur.pcbName + "进程执行完毕");

提醒注意的是:在实际的系统中,当一个进程被选中运行时,必须恢复进程的现场,让它占有处理器运行,直到出现等待事件或运行结束。在这里省去了这些工作。
(5) 进程运行一次后,若要求运行时间不为0,则再将它加入队列(按优先数大小插入,且置队首标志);若要求运行时间=0,则把它的状态修改成“结束”(E),且退出队列。

public void addPCB(PCB node) {
        //如果所需运行时间为0,状态改为E 退出进程队列
        if (node.time == 0) {
            node.state = 'E';
            System.out.println(node.pcbName + "退出进程队列");
            System.out.println(node);
            System.out.println();
            return;
        }
        //若头结点为空
        if (this.head == null) {
            this.head = node;
            return;
        }
        //进行边插边按优先级进行排序

        //如果优先级比头结点大
        if (node.priority > this.head.priority) {
            node.next = this.head;
            this.head = node;
            return;
        }
        //如果和头结点优先级相同,按先来先服务原则
        if (node.priority == this.head.priority) {
            node.next = this.head.next;
            this.head.next = node;
            return;
        }
        //在node的优先级在中间插入
        PCB cur = this.head.next;
        PCB parent = this.head;
        while (cur != null) {
            if (node.priority > cur.priority) {
                //进行插入
                node.next = parent.next;
                parent.next = node;
                return;
            } else if (node.priority == cur.priority) {
                //优先级相同,按先来先服务原则
                parent = cur;
                node.next = parent.next;
                parent.next = node;
                return;
            }
            parent = cur;
            cur = cur.next;
        }
        //此时还未结束说明优先级最下就直接插到尾
        parent.next = node;
        node.next = null;
    }

(6) 若“就绪”状态的进程队列不为空,则重复上面(4)和(5)的步骤,直到所有进程都成为“结束”状态。

//进程运行方法
    public void runPCB() {
        while (this.head != null) {
            //运行优先级最高的第一个进程
            PCB cur = this.head;
            this.head = this.head.next;
            System.out.println();
            System.out.println("开始执行" + cur.pcbName + "进程");
            System.out.println(cur);
            //cur的运行优先级-1 运行时间-1
            cur.priority -= 1;
            cur.time -= 1;
            System.out.println(cur.pcbName + "进程执行完毕");
            System.out.println();
            //将cur再插入进程队列
            addPCB(cur);
            //运行一次结束后遍历显示此时进程队列所有信息
            if (this.head == null) {
                System.out.println("所有进程执行完毕");
                return;
            }
            System.out.println("此时进程队列的所有进程信息:");
            System.out.println("=================");
            display();
            System.out.println("=================");
        }
    }

(7) 在所设计的程序中应有显示或打印语句,能显示或打印每次被选中进程的进程名以及运行一次后进程队列的变化。
(8) 为五个进程任意确定一组“优先数”和“要求运行时间”,启动所设计的处理器调度程序,显示或打印逐次被选中进程的进程名以及进程控制块的动态变化过程。

四、实现代码

class PCB {
    public String pcbName;
    public PCB next;
    public int time;
    public int priority;
    public char state = 'R';

    public PCB(String pcbName, int time, int priority) {
        this.pcbName = pcbName;
        this.time = time;
        this.priority = priority;
    }

    @Override
    public String toString() {
        return "进程名:" + pcbName +  "  要求运行时间:" + time + "  优先级数:" + priority
                 + "  状态:" + state;
    }
}

public class ProcessScheduling {
    private PCB head;
    //pcb对象插入操作
    public void addPCB(PCB node) {
        //如果所需运行时间为0,状态改为E 退出进程队列
        if (node.time == 0) {
            node.state = 'E';
            System.out.println(node.pcbName + "退出进程队列");
            System.out.println(node);
            System.out.println();
            return;
        }
        //若头结点为空
        if (this.head == null) {
            this.head = node;
            return;
        }
        //进行边插边按优先级进行排序

        //如果优先级比头结点大
        if (node.priority > this.head.priority) {
            node.next = this.head;
            this.head = node;
            return;
        }
        //如果和头结点优先级相同,按先来先服务原则
        if (node.priority == this.head.priority) {
            node.next = this.head.next;
            this.head.next = node;
            return;
        }
        //在node的优先级在中间插入
        PCB cur = this.head.next;
        PCB parent = this.head;
        while (cur != null) {
            if (node.priority > cur.priority) {
                //进行插入
                node.next = parent.next;
                parent.next = node;
                return;
            } else if (node.priority == cur.priority) {
                //优先级相同,按先来先服务原则
                parent = cur;
                node.next = parent.next;
                parent.next = node;
                return;
            }
            parent = cur;
            cur = cur.next;
        }
        //此时还未结束说明优先级最下就直接插到尾
        parent.next = node;
        node.next = null;
    }

    //进程运行方法
    public void runPCB() {
        while (this.head != null) {
            //运行优先级最高的第一个进程
            PCB cur = this.head;
            this.head = this.head.next;
            System.out.println();
            System.out.println("开始执行" + cur.pcbName + "进程");
            System.out.println(cur);
            //cur的运行优先级-1 运行时间-1
            cur.priority -= 1;
            cur.time -= 1;
            System.out.println(cur.pcbName + "进程执行完毕");
            System.out.println();
            //将cur再插入进程队列
            addPCB(cur);
            //运行一次结束后遍历显示此时进程队列所有信息
            if (this.head == null) {
                System.out.println("所有进程执行完毕");
                return;
            }
            System.out.println("此时进程队列的所有进程信息:");
            System.out.println("=================");
            display();
            System.out.println("=================");
        }
    }

    public void display() {
        for (PCB pcb = this.head; pcb != null; pcb = pcb.next) {
            System.out.println(pcb);
        }
    }
}

五、测试

public class TestProcess {
    public static void main(String[] args) {
        PCB[] pcbs = new PCB[5];
        pcbs[0] = new PCB("P1", 2, 1);
        pcbs[1] = new PCB("P2", 3, 5);
        pcbs[2] = new PCB("P3", 1, 3);
        pcbs[3] = new PCB("P4", 2, 4);
        pcbs[4] = new PCB("P5", 4, 2);
        ProcessScheduling processScheduling = new ProcessScheduling();
        for (PCB p : pcbs
         ) {
            processScheduling.addPCB(p);
        }
        processScheduling.runPCB();
    }
}

测试结果

开始执行P2进程
进程名:P2  要求运行时间:3  优先级数:5  状态:R
P2进程执行完毕

此时进程队列的所有进程信息:
=================
进程名:P4  要求运行时间:2  优先级数:4  状态:R
进程名:P2  要求运行时间:2  优先级数:4  状态:R
进程名:P3  要求运行时间:1  优先级数:3  状态:R
进程名:P5  要求运行时间:4  优先级数:2  状态:R
进程名:P1  要求运行时间:2  优先级数:1  状态:R
=================

开始执行P4进程
进程名:P4  要求运行时间:2  优先级数:4  状态:R
P4进程执行完毕

此时进程队列的所有进程信息:
=================
进程名:P2  要求运行时间:2  优先级数:4  状态:R
进程名:P3  要求运行时间:1  优先级数:3  状态:R
进程名:P4  要求运行时间:1  优先级数:3  状态:R
进程名:P5  要求运行时间:4  优先级数:2  状态:R
进程名:P1  要求运行时间:2  优先级数:1  状态:R
=================

开始执行P2进程
进程名:P2  要求运行时间:2  优先级数:4  状态:R
P2进程执行完毕

此时进程队列的所有进程信息:
=================
进程名:P3  要求运行时间:1  优先级数:3  状态:R
进程名:P2  要求运行时间:1  优先级数:3  状态:R
进程名:P4  要求运行时间:1  优先级数:3  状态:R
进程名:P5  要求运行时间:4  优先级数:2  状态:R
进程名:P1  要求运行时间:2  优先级数:1  状态:R
=================

开始执行P3进程
进程名:P3  要求运行时间:1  优先级数:3  状态:R
P3进程执行完毕

P3退出进程队列
进程名:P3  要求运行时间:0  优先级数:2  状态:E

此时进程队列的所有进程信息:
=================
进程名:P2  要求运行时间:1  优先级数:3  状态:R
进程名:P4  要求运行时间:1  优先级数:3  状态:R
进程名:P5  要求运行时间:4  优先级数:2  状态:R
进程名:P1  要求运行时间:2  优先级数:1  状态:R
=================

开始执行P2进程
进程名:P2  要求运行时间:1  优先级数:3  状态:R
P2进程执行完毕

P2退出进程队列
进程名:P2  要求运行时间:0  优先级数:2  状态:E

此时进程队列的所有进程信息:
=================
进程名:P4  要求运行时间:1  优先级数:3  状态:R
进程名:P5  要求运行时间:4  优先级数:2  状态:R
进程名:P1  要求运行时间:2  优先级数:1  状态:R
=================

开始执行P4进程
进程名:P4  要求运行时间:1  优先级数:3  状态:R
P4进程执行完毕

P4退出进程队列
进程名:P4  要求运行时间:0  优先级数:2  状态:E

此时进程队列的所有进程信息:
=================
进程名:P5  要求运行时间:4  优先级数:2  状态:R
进程名:P1  要求运行时间:2  优先级数:1  状态:R
=================

开始执行P5进程
进程名:P5  要求运行时间:4  优先级数:2  状态:R
P5进程执行完毕

此时进程队列的所有进程信息:
=================
进程名:P1  要求运行时间:2  优先级数:1  状态:R
进程名:P5  要求运行时间:3  优先级数:1  状态:R
=================

开始执行P1进程
进程名:P1  要求运行时间:2  优先级数:1  状态:R
P1进程执行完毕

此时进程队列的所有进程信息:
=================
进程名:P5  要求运行时间:3  优先级数:1  状态:R
进程名:P1  要求运行时间:1  优先级数:0  状态:R
=================

开始执行P5进程
进程名:P5  要求运行时间:3  优先级数:1  状态:R
P5进程执行完毕

此时进程队列的所有进程信息:
=================
进程名:P1  要求运行时间:1  优先级数:0  状态:R
进程名:P5  要求运行时间:2  优先级数:0  状态:R
=================

开始执行P1进程
进程名:P1  要求运行时间:1  优先级数:0  状态:R
P1进程执行完毕

P1退出进程队列
进程名:P1  要求运行时间:0  优先级数:-1  状态:E

此时进程队列的所有进程信息:
=================
进程名:P5  要求运行时间:2  优先级数:0  状态:R
=================

开始执行P5进程
进程名:P5  要求运行时间:2  优先级数:0  状态:R
P5进程执行完毕

此时进程队列的所有进程信息:
=================
进程名:P5  要求运行时间:1  优先级数:-1  状态:R
=================

开始执行P5进程
进程名:P5  要求运行时间:1  优先级数:-1  状态:R
P5进程执行完毕

P5退出进程队列
进程名:P5  要求运行时间:0  优先级数:-2  状态:E

所有进程执行完毕

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/Shangxingya/article/details/106340891