操作系统——进程调度模拟实验Java实现(先来先服务、最高优先数)

【实验目的】

  1.  掌握进程控制块的作用和实现技术;
  2.  熟悉操作系统的进程调度算法及实现方法。 

【实验原理】

每个进程有一个进程控制块(PCB)表示。进程控制块可以包含如下信息:进程名、优先数、到达时间、需要运行时间、已用CPU时间、进程状态等等。

进程的优先数及需要的运行时间可以事先人为地指定(也可以由随机数产生)。进程的到达时间为进程输入的时间。

进程的运行时间以时间片为单位进行计算。

每个进程的状态可以是就绪 W(Wait)、运行R(Run)、或完成F(Finish)三种状态之一。

就绪进程获得 CPU后都只能运行一个时间片。用已占用CPU时间加1来表示。

如果运行一个时间片后,进程的已占用 CPU时间已达到所需要的运行时间,则撤消该进程,如果运行一个时间片后进程的已占用CPU时间还未达所需要的运行时间,也就是进程还需要继续运行,此时应将进程的优先数减1(即降低一级),然后把它插入就绪队列等待CPU。

每进行一次调度程序都打印一次运行进程、就绪队列、以及各个进程的 PCB,以便进行检查。   

重复以上过程,直到所要进程都完成为止。

【代码(Java实现)】

扫描二维码关注公众号,回复: 11457604 查看本文章

首先是一个辅助类:

public class Shuru {
	String name;
	int Super;
	int zxTime;
	int ddtime;//到达时间
	int runtime;//剩余执行时间
	String zt;//进程状态
}

正式代码:

import java.util.Scanner;

public class Jincheng {
	Scanner s = new Scanner(System.in);
	public void begin1(int n) {
		Scanner sr[] = new Scanner[n];
		Shuru shuru[] = new Shuru[n];
		int yxj[] = new int[n];
		int zzxtime = 0;// 总执行时间
		for (int i = 0; i < n; i++) {
			System.out.println("进程" + (i + 1) + "信息输入:");
			shuru[i] = new Shuru();
			sr[i] = new Scanner(System.in);
			System.out.print("请输入进程名:");
			shuru[i].name = sr[i].nextLine();
			System.out.print("请输入优先级:");
			shuru[i].Super = sr[i].nextInt();
			yxj[i] = shuru[i].Super;// 每次将优先级传给数组
			System.out.print("请输入执行时间:");
			shuru[i].zxTime = sr[i].nextInt();
			System.out.print("请输入到达时间:");
			shuru[i].ddtime=sr[i].nextInt();
			zzxtime += shuru[i].zxTime;
			shuru[i].runtime = shuru[i].zxTime;
		}
		System.out.println("***************************************");
		for (int i = 0; i < zzxtime; i++) {
			System.out.println("*******************************这是第"+(i+1)+"次执行*****"
					+ "***************************************");
			sort(shuru);// 将优先级进行排序
			shuru[n - 1].Super -= 1;// 优先级减一
			shuru[n - 1].runtime = shuru[n - 1].runtime - 1;// 剩余执行次数减一
			System.out.println("现在正在执行的进程是:" + shuru[n - 1].name);
			System.out.println("进程名\t 优先级 \t 剩余执行时间 \t状态");
			shuru[n-1].zt="r";
			show(shuru[n - 1]);
			System.out.println("就绪中的进程有:");
			System.out.println("进程名\t 优先级 \t 剩余执行时间\t状态");
			for (int j = 0; j < n - 1; j++) {
				if (shuru[j].runtime != 0 && shuru[j].Super != 0) {
					for (int k = j; k < n - 1; k++) {
						shuru[k].zt="w";
						show(shuru[k]);
						break;
					}
				}
			}
			if (shuru[n - 1].runtime == 0) {// 如果剩余执行次数为0,进程结束
				System.out.println("__________进程" + shuru[n - 1].name + "结束_____________");
				shuru[n - 1].Super = 0;// 如果一个进程执行完毕,将它的优先级置零
			}
		}
	}
	public void sort(Shuru shuru[]) {// 比较优先级
		for (int i = 1; i <= shuru.length; i++) {// 计数,第几轮比较
			for (int j = 0; j < shuru.length - i; j++) {// 注意这一步,第一趟比较length次,第二趟比较length-1次,以此类推
				if (shuru[j].Super > shuru[j + 1].Super) {
					Shuru t = shuru[j];
					shuru[j] = shuru[j + 1];
					shuru[j + 1] = t;// 交换
				}
			}
		}
	}
	
	public void sort1(Shuru shuru[]) {//比较到达时间
		for (int i = 1; i <= shuru.length; i++) {// 计数,第几轮比较
			for (int j = 0; j<shuru.length - i; j++) {// 注意这一步,第一趟比较length次,第二趟比较length-1次,以此类推
				if (shuru[j].ddtime >shuru[j + 1].ddtime) {
					Shuru t = shuru[j];
					shuru[j] = shuru[j + 1];
					shuru[j + 1] = t;// 交换
				}
			}
		}
	}
public void begin2(int n) {
	Scanner sr[] = new Scanner[n];
	Shuru shuru[] = new Shuru[n];
	int yxj[] = new int[n];
	int zzxtime = 0;// 总执行时间
	for (int i = 0; i < n; i++) {
		System.out.println("进程" + (i + 1) + "信息输入:");
		shuru[i] = new Shuru();
		sr[i] = new Scanner(System.in);
		System.out.print("请输入进程名:");
		shuru[i].name = sr[i].nextLine();
		System.out.print("请输入执行时间:");
		shuru[i].zxTime = sr[i].nextInt();
		System.out.print("请输入到达时间:");
		shuru[i].ddtime=sr[i].nextInt();
		zzxtime += shuru[i].zxTime;
		shuru[i].runtime = shuru[i].zxTime;
	}
	int count=0;
	for(int i=0;i<zzxtime;i++) {
		System.out.println("*******************************"
				+ "**这是第"+(i+1)+"次执行**************************************");
		shuru[count].runtime = shuru[count].runtime - 1;// 剩余执行次数减一
		sort1(shuru);
		System.out.println("现在正在执行的进程是:" + shuru[count].name);
		System.out.println("进程名\t 剩余执行时间\t 到达时间\t\t状态");
		shuru[count].zt="r";
		show1(shuru[count]);
		System.out.println("就绪中的进程有:");
		System.out.println("进程名\t 剩余执行时间 \t到达时间\t\t状态");
		for(int j=count+1;j<n;j++) {
			shuru[j].zt="w";
			show1(shuru[j]);
		}
		if (shuru[count].runtime == 0) {// 如果剩余执行次数为0,进程结束
			System.out.println("__________进程" + shuru[count].name + "结束_____________");
			count++;
		}
	}
}
	public void show(Shuru shuru) {
		System.out.println(shuru.name + "\t" + shuru.Super + "\t " + shuru.runtime+"\t\t"+shuru.zt);
	}
	public void show1(Shuru shuru) {
		System.out.println(shuru.name + "\t"  + shuru.runtime+"\t\t"+shuru.ddtime+"\t\t"+shuru.zt);
	}

	public static void main(String[] args) {
		Jincheng j = new Jincheng();
		Scanner s1 = new Scanner(System.in);
		System.out.print("请输入进程数目:");
		int n = s1.nextInt();
		System.out.println("请输入你要选择的调度算法:(1  最高优先数优先;2  先来先服务)");
		int choice=s1.nextInt();
		if(choice==1) {
		j.begin1(n);
		}else if(choice==2) {
			j.begin2(n);
		}
		else {
			System.out.println("输入错误!请重新输入!");
		}
		System.out.println("所有进程执行完毕!");
		}
	}

【结果截图】

还可以选择先来先服务:

转载请注明出处~我是一个有梦想的baby girl!

猜你喜欢

转载自blog.csdn.net/weixin_42189863/article/details/86489472