java实现动态优先权算法

本模拟程序实现对N个进程根据优先权的高低调度的模拟,创建进程描述符PCB,进程的优先权在运行过程中动态改变,每个时间片结束后显示当前各进程的状态。具体要求如下:

1) JAVA语言来实现对N个进程采用动态优先权算法的进程调度;

2) 每个用来标识进程的进程控制块PCB包括以下字段:

进程标识数 ID

进程优先数 PRIORITY,并规定优先数越大的进程,其优先权越高;

进程已占用的CPU时间CPUTIME

进程还需占用的CPU时间NEEDTIME,当进程运行完毕时,NEEDTIME变为0

进程的阻塞时间STARTBLOCK,表示当进程再运行STARTBLOCK个时间片后,将进入阻塞状态;

进程被阻塞的时间BLOCKTIME,表示已阻塞的进程再等待BLOCKTIME个时间片后,将转换成就绪状态;

进程状态STATE

队列指针NEXT,用来将PCB排成队列。

3) 优先数改变的原则:

进程在就绪队列中呆一个时间片,优先数加1;

进程每运行一个时间片,优先数减3;

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

ID

0

1

2

3

4

PRIORITY

9

38

30

29

0

CPUTIME

0

0

0

0

0

ALLTIME

3

3

6

3

4

STARTBLOCK

2

-1

-1

-1

-1

BLOCKTIME

3

0

0

0

0

STATE

READY

READY

READY

READY

READY

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

RUNNING PROGi

READY-QUEUE-id1-id2

BLOCK-QUEUE-id3-id4

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = == = =

ID 0 1 2 3 4

PRIORITY P0 P1 P2 P3 P4

CUPTIME C0 C1 C2 C3 C4

ALLTIME A0 A1 A2 A3 A4

STARTBLOCK T0 T1 T2 T3 T4

BLOCKTIME B0 B1 B2 B3 B4

STATE S0 S1 S2 S3 S4

package com.os;

import java.util.Queue;
import java.util.LinkedList;//队列类  
import java.util.Scanner;

class PCB {
	public int id; // 进程ID
	public int pri; // 进程优先级
	public int cput; // 进程已占用的时间片
	public int allt; // 进程还需占用的时间片
	public int startblock; // 进程开始时间片
	public int endblock; // 进程阻塞时间片
	public String state; // 进程状态

	public PCB() // 构造方法
	{

	}
}// 进程控制块

public class DP {

	public PCB rpcb = null; // 正在运行的进程
	public LinkedList<PCB> readyqueue = null; // 就绪队列
	public LinkedList<PCB> blockqueue = null; // 阻塞队列
	public LinkedList<PCB> tempqueue = null; // 临时队列,存储初始队列;
	public LinkedList<PCB> overqueue = null; // 执行完成的队列

	public int[] ids = { 0, 1, 2, 3, 4 };
	public int[] pris = { 9, 38, 30, 29, 0 };
	public int[] cputs = { 0, 0, 0, 0, 0 };
	public int[] allts = { 3, 3, 6, 3, 4 };
	public int[] sts = { 2, -1, -1, -1, -1 };
	public int[] ens = { 3, 0, 0, 0, 0 };
	public String[] states = { "READY", "READY", "READY", "READY", "READY" };

	public DP() {
		readyqueue = new LinkedList<PCB>();
		blockqueue = new LinkedList<PCB>();
		tempqueue = new LinkedList<PCB>();
		overqueue = new LinkedList<PCB>();
	}

	public void readyqueueSort(LinkedList<PCB> queue) // 就绪队列优先权排序
	{
		PCB temppcb1, temppcb2, temppcb3;
		int count1 = queue.size();
		int count2 = queue.size() - 1;

		LinkedList<PCB> queueQ = new LinkedList<PCB>();

		while (count1 > 0) {
			temppcb3 = temppcb1 = queue.poll();
			count1--;
			while (count2 > 0) {
				temppcb2 = queue.poll();
				count2--;
				if (temppcb2.pri >= temppcb1.pri)// 优先权
				{
					queue.offer(temppcb2);
				} else {
					queue.offer(temppcb1);
					temppcb1 = new PCB();
					temppcb1 = temppcb2;
				}

			}
			queue.offer(temppcb1);
			count2 = queue.size() - 1;

		}

	}

	public void blockqueueSort(LinkedList<PCB> queue) // 初始化阻塞队列剩余时间片排序
	{
		PCB temppcb1, temppcb2;
		int count1 = queue.size();
		int count2 = queue.size() - 1;

		while (count1 > 0) {
			temppcb1 = queue.poll();
			count1--;
			while (count2 > 0) {
				temppcb2 = queue.poll();
				count2--;
				if (temppcb2.endblock < temppcb1.endblock)// 剩余时间片
				{
					queue.offer(temppcb2);
				} else {
					queue.offer(temppcb1);
					temppcb1 = new PCB();
					temppcb1 = temppcb2;
				}
			}
			queue.offer(temppcb1);
			count2 = queue.size() - 1;

		}

	}

	public void tempqueueInit(LinkedList<PCB> queue)// 临时队列的初始化
	{

		for (int i = 0; i <= 4; i++) {
			PCB temPcb = new PCB();
			temPcb.id = ids[i];
			temPcb.pri = pris[i];
			temPcb.cput = cputs[i];
			temPcb.allt = allts[i];
			temPcb.startblock = sts[i];
			temPcb.endblock = ens[i];
			temPcb.state = states[i];
			queue.offer(temPcb);

		}
		System.out.println("临时队列初始化成功");

	}

	public String initqueue(LinkedList<PCB> queue0, LinkedList<PCB> queue1, LinkedList<PCB> queue2) {
		PCB temppcb;
		while ((temppcb = queue0.poll()) != null) {
			if (temppcb.state.equals("READY"))// 就绪队列初始化
			{
				queue1.offer(temppcb);
			} else // 阻塞队列初始化
			{
				queue2.offer(temppcb);
			}
		}

		readyqueueSort(queue1);
		blockqueueSort(queue2);

		return "队列初始化成功!";
	}

	public void inputQueue(LinkedList<PCB> queue) // 队列输出
	{
		LinkedList<PCB> tempqueue = new LinkedList<PCB>(queue);

		PCB temppcb;
		while ((temppcb = tempqueue.poll()) != null) {
			System.out.print("   " + temppcb.id + "   ");
			System.out.print(temppcb.pri + "   ");
			System.out.print(temppcb.cput + "   ");
			System.out.print(temppcb.allt + "   ");
			System.out.print(temppcb.startblock + "   ");
			System.out.print(temppcb.endblock + "   ");
			System.out.print(temppcb.state);
			System.out.println();
		}
	}

	public void input(DP sDp) {

		System.out.println("--------------------------------");
		System.out.println("RUNNING_PROCESS:");
		if (sDp.rpcb != null) {
			System.out.println(sDp.rpcb.id + "  " + sDp.rpcb.pri + "  " + sDp.rpcb.allt);
		} else {
			System.out.println("无进程在执行");
		}
		System.out.println("READY_QUEUE:");
		sDp.inputQueue(sDp.readyqueue);
		System.out.println("BLOCK_QUEUE:");
		sDp.inputQueue(sDp.blockqueue);
		System.out.println("OVER_QUEUE:");
		sDp.inputQueue(sDp.overqueue);
	}

	public boolean judgePri(LinkedList<PCB> queue)// 判断是否后来优先权更大
	{
		LinkedList<PCB> tempqueue = new LinkedList<PCB>(queue);
		PCB tempPcb = tempqueue.poll();
		if (tempPcb != null) {
			if (tempPcb.pri > rpcb.pri) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}

	}

	public void readyqueueUpdate(LinkedList<PCB> queue)// 就绪队列更新
	{
		int count = queue.size();
		PCB tempPcb;
		while (count > 0) {
			count--;
			tempPcb = queue.poll();
			tempPcb.pri++;//待在就绪队列优先数加一
			queue.offer(tempPcb);
		}
		readyqueueSort(readyqueue);
	}

	public void blockqueueUpdate(LinkedList<PCB> queue)// 阻塞队列更新
	{
		int count = queue.size();
		PCB tempPcb;
		while (count > 0) {
			count--;
			tempPcb = queue.poll();
			tempPcb.endblock--;
			if (tempPcb.endblock == 0) {
				tempPcb.endblock = 3;
				tempPcb.state = "READY";
				readyqueue.offer(tempPcb);
				readyqueueSort(readyqueue);// 使得刚转移过来的进程参与优先权竞选
			} else {
				queue.offer(tempPcb);
			}

		}
		blockqueueSort(blockqueue);
	}

	public void stepupEnd() {
		if (rpcb.allt == 0) {
			rpcb.state = "OVER";
			overqueue.offer(rpcb);
			rpcb = readyqueue.poll();
			rpcb.state = "EXECUTE";
			rpcb.allt--;
			rpcb.cput++;
			rpcb.pri = rpcb.pri - 3;

		} else {
			rpcb.allt--;
			rpcb.cput++;
			rpcb.pri = rpcb.pri - 3;
		}

	}

	public void stepup() // 运行一个时间片(优先数减3)
	{
		if (rpcb != null) {
			if (rpcb.allt == 0) {
				rpcb.state = "OVER";
				overqueue.offer(rpcb);
				rpcb = readyqueue.poll();
				rpcb.state = "EXECUTE";
				rpcb.allt--;
				rpcb.cput++;
				rpcb.pri = rpcb.pri - 3;

			} else {
				if (judgePri(readyqueue)) {
					rpcb.state = "BLOCK";
					blockqueue.offer(rpcb);
					rpcb = readyqueue.poll();
					rpcb.state = "EXECUTE";

				}
				rpcb.allt--;
				rpcb.cput++;
				rpcb.pri = rpcb.pri - 3;
			}
			readyqueueUpdate(readyqueue);
			blockqueueUpdate(blockqueue);

		} else {
			rpcb = readyqueue.poll();
			rpcb.allt--;
			rpcb.cput++;
			rpcb.pri = rpcb.pri - 3;
			rpcb.state = "EXECUTE";
			System.out.println("哈哈");

			readyqueueUpdate(readyqueue);
			blockqueueUpdate(blockqueue);

		}

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DP sDp = new DP();
		sDp.tempqueueInit(sDp.tempqueue);
		System.out.println(sDp.initqueue(sDp.tempqueue, sDp.readyqueue, sDp.blockqueue));

		System.out.println("READY_QUEUE:");
		sDp.inputQueue(sDp.readyqueue);
		System.out.println("BLOCK_QUEUE:");
		sDp.inputQueue(sDp.blockqueue);
		System.out.println("OVER_QUEUE:");
		sDp.inputQueue(sDp.overqueue);

		PCB temPcb;
		LinkedList<PCB> queue = new LinkedList<PCB>(sDp.readyqueue);
		int count = 0;
		while ((temPcb = queue.poll()) != null) {
			sDp.stepup();
			count++;
			System.out.println("第" + count + "个时间片后:");
			sDp.input(sDp);
			System.out.println();
			queue = new LinkedList<PCB>(sDp.readyqueue);
		}

		while (sDp.rpcb.allt != 0) {
			sDp.stepupEnd();
			count++;
			System.out.println("第" + count + "个时间片后:");
			sDp.input(sDp);
			System.out.println();

		}

		sDp.rpcb.state = "OVER";
		sDp.overqueue.offer(sDp.rpcb);
		sDp.rpcb = null;

		count++;
		System.out.println("第" + count + "个时间片后:");
		sDp.input(sDp);

	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值