设计目的:
进程管理是操作系统中的重要功能,用来创建进程、撤消进程、实现进程状态转换,它提供了在可运行的进程之间复用CPU的方法。在进程管理中,进程调度是核心,因为在采用多道程序设计的系统中,往往有若干个进程同时处于就绪状态,当就绪进程个数大于处理器数目时,就必须依照某种策略决定哪些进程优先占用处理器。本设计模拟在单处理器情况下的进程调度,目的是加深对进程调度工作的理解,掌握不同调度算法的优缺点。
设计内容:
设计程序模拟单处理机系统中的进程调度算法,在短作业优先调度算法、时间片轮转调度、最高优先级优先算法三种算法中选择两种实现。
代码如下:
import java.util.Scanner;
public class PP {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
System.out.println("请输入算法:1、时间片轮转调度算法.2、优先级调度算法");
int input=reader.nextInt();
switch(input){
case 1:
SJ hhh = new SJ();
hhh.way();
break;
case 2:
PIRO hh = new PIRO();
hh.way();
break;
default:
System.out.println("非法输入");
break;
}
}
}
class SJ{
void way() {
int Num;
int i,j;
int t;
int a,b,c;
int NowTime;
Scanner Shu = new Scanner(System.in);
System.out.print("请输入进程数: ");
Num = Shu.nextInt();
System.out.println("请依次输入进程名 到达时间 需要运行时间 ");
PCB pcb[] = new PCB[Num];
for(i = 0;i < Num;i++){
a = Shu.nextInt();
b = Shu.nextInt();
c = Shu.nextInt();
pcb[i] = new PCB(a,b,c);
}
PCB Q = new PCB();
Q.sort1(pcb, Num);//首先按先后顺序排序
System.out.println("进程名 到达时间 需要运行时间 ");
for(i = 0; i < Num; i++){
System.out.print(" "+pcb[i].Name+" ");
System.out.print(pcb[i].ArrT+" ");
System.out.println(pcb[i].RunT);
}
NowTime = pcb[0].ArrT;
System.out.println("现在的时间为:"+NowTime);
while(true) {
for(i=0;i<Num;i++){
if(pcb[i].State !='F')
break;
}
if(i==Num)
break;
if(pcb[0].RunT == 0)
Q.change(pcb, Num);
for(i = 1; i < Num; i++) {
Q.setSTATE(pcb, i, 'W');
}
Q.setCPUTIME(pcb,0);
Q.setSTATE(pcb,0,'R');
Q.setRunT(pcb, 0);
NowTime++;
for(i = 0;i < Num;i++){
if(pcb[i].RunT == 0){
Q.setSTATE(pcb,i,'F');
}
}
System.out.println("进程名 到达时间 需要运行时间 已占用CPU时间 进程状态");//输出进程状态
for(i = 0;i < Num;i++) {
System.out.print(" "+pcb[i].Name+" ");
System.out.print(pcb[i].ArrT+" ");
System.out.print(pcb[i].RunT+" ");
System.out.print(pcb[i].CPUTIME+" ");
System.out.println(pcb[i].State);
}
System.out.println("现在的时间为:"+NowTime);
}
}
}
class PIRO{
void way() {
int Num;
int i,j;
int t;
int a,b,c,d;
int o,p,q,r,s;
int NowTime;
Scanner Shu = new Scanner(System.in);
System.out.print("请输入进程数:");
Num = Shu.nextInt();
System.out.println("请依次输入进程名 到达时间 需要运行时间 优先级");
PCB pcb[] = new PCB[Num];
for(i = 0;i < Num;i++){
a = Shu.nextInt();
b = Shu.nextInt();
c = Shu.nextInt();
d = Shu.nextInt();
pcb[i] = new PCB(a,b,c,d);
}
PCB e = new PCB();
e.sort1(pcb,Num); //第一次按先后顺序排序
System.out.println("进程名 到达时间 需要运行时间 优先级"); //输出排序后的进程
for(o=0;o<Num;o++){
System.out.print(" "+pcb[o].Name+" ");
System.out.print(pcb[o].ArrT+" ");
System.out.print(pcb[o].RunT+" ");
System.out.println(pcb[o].Piro);
}
NowTime = pcb[0].ArrT;
e.setCPUTIME(pcb,0);
e.setSTATE(pcb,0,'R');
e.setRunT(pcb, 0);
System.out.println("进程名 到达时间 需要运行时间 已占用CPU时间 进程状态");
for(o=0;o<Num;o++){
System.out.print(" "+pcb[o].Name+" ");
System.out.print(pcb[o].ArrT+" ");
System.out.print(pcb[o].RunT+" ");
System.out.print(pcb[o].CPUTIME+" ");
System.out.println(pcb[o].State);
}
System.out.println("现在的时间为:"+NowTime++);
while(true){
for(i=0;i<Num;i++){
if(pcb[i].State !='F')
break;
}
if(i==Num)
break;
e.sort2(pcb,Num);
e.setPiro(pcb, 0);
e.setCPUTIME(pcb,0);
e.setSTATE(pcb,0,'R');
e.setRunT(pcb, 0);
NowTime++;
for(p = 1;p < Num && pcb[p].State!='F';p++){
e.setSTATE(pcb,p,'W');
}
for(r = 0;r < Num;r++){
if(pcb[r].RunT == 0){
e.setSTATE(pcb,r,'F');
e.changepiro(pcb, r);
}
}
System.out.println("进程名 到达时间 需要运行时间 已占用CPU时间 进程状态");//输出进程状态
for(q = 0;q < Num;q++) {
System.out.print(" "+pcb[q].Name+" ");
System.out.print(pcb[q].ArrT+" ");
System.out.print(pcb[q].RunT+" ");
System.out.print(pcb[q].CPUTIME+" ");
System.out.println(pcb[q].State);
}
System.out.println("现在的时间为:"+NowTime);
}
}
}
class PCB{
int Name; //进程名
int ArrT; //到达时间
int RunT; //需要运行时间
int Piro; //优先级
int CPUTIME=0; //已用CPU时间
char State; //进程状态
PCB(){};
PCB(int a,int b,int c,int d){
Name = a;
ArrT = b;
RunT = c;
Piro = d;
}
PCB(int a,int b,int c){
Name = a;
ArrT = b;
RunT = c;
}
void sort1(PCB pcb[],int n){ //排序,先到达的排在前面
PCB t = new PCB();
int i,j;
for(i = 0;i < n-1;i++)
for(j = 0;j < n-1;j++)
if(pcb[j].ArrT > pcb[j+1].ArrT){
t = pcb[j];
pcb[j] = pcb[j+1];
pcb[j+1] = t;
}
}
void sort2(PCB pcb[],int n){ //按照优先级排序
PCB t = new PCB();
int i,j;
for(i = 0;i < n-1;i++)
for(j = 0;j < n-1;j++)
if(pcb[j].Piro < pcb[j+1].Piro){
t = pcb[j];
pcb[j] = pcb[j+1];
pcb[j+1] = t;
}
}
void change(PCB pcb[],int n){
int i;
PCB t = new PCB();
for(i = 0; i < n-1; i++) {
t = pcb[i];
pcb [i] = pcb[i+1];
pcb[i+1] = t;
}
}
int setCPUTIME(PCB pcb[],int n){ //进程的CPUTIME加一
pcb[n].CPUTIME++;
return pcb[n].CPUTIME;
}
int setSTATE(PCB pcb[],int t,char n){ //进程状态改变
pcb[t].State = n;
return pcb[t].State;
}
int setRunT(PCB pcb[],int n){ //进程的需要运行时间减一
pcb[n].RunT = pcb[n].RunT - 1;
return pcb[n].RunT;
}
int setPiro(PCB pcb[],int n){ //在进程还没完成之前,运行一次就优先级减一
pcb[n].Piro--;
return pcb[n].Piro--;
}
int changepiro(PCB pcb[],int n){ //以一个进程完成之后,优先级设置为-99
pcb[n].Piro = -99;
return pcb[n].Piro;
}
}