package 进程调度;
public class Diaodu {
public static void main(String[] args) {
// TODO Auto-generated method stub
String name[]= {" 进程名", " 到达时间", " 运行时间", " 优先级"};
for(int i=0;i<name.length;i++){
System.out.print(name[i]);
}
System.out.println();
double a[][]= {{1,8.00,2.00,2},{2,8.50,0.50,4},{3,9.00,0.10,3},{4,9.50,0.20,1}};
for(int j=0;j<a.length;j++){
for(int i=0;i<a[j].length;i++){
System.out.print( " "+ a[j][i]);
}
System.out.println();
}
System.out.println();
System.out.println("短作业优先算法,调度如下:");
for (int n=0; n<a.length;n++){//按运行时间由短到长排序
for(int m=n+1;m<a.length;m++){
if(a[m][2]<a[n][2]){
double t = a[m][2];
double f = a[m][0];
double b = a[m][1];
double o = a[m][3];
a[m][2]=a[n][2];
a[m][0]=a[n][0];
a[m][1]=a[n][1];
a[m][3]=a[n][3];
a[n][2]= t;
a[n][0]= f;
a[n][1]= b;
a[n][3]= o;
}//必须把4个数据都交换
}
}
int sam = 0;//作为CPU是否被占用的标志
int a1= (int)(a[0][1]*10),a2= (int)(a[0][2]*10),cia1=0,cia2=0,ta1= 0;//切换成整型,防止用double类型产生误差
int b1= (int)(a[1][1]*10),b2= (int)(a[1][2]*10),cib1=0,cib2=0,tb1= 0;//ci是为了运行过的进程只运行一次。t1用来计算运行时间
int c1= (int)(a[2][1]*10),c2= (int)(a[2][2]*10),cic1=0,cic2=0,tc1= 0;
int d1= (int)(a[3][1]*10),d2= (int)(a[3][2]*10),cid1=0,cid2=0,td1= 0;
int num= 0;//记录是否全部完成
for(int t=0; t<=150; t++){
if(a1<=t && sam==0 && cia1==0){
sam = 1;
ta1 = t;
cia1= 1;
double t2=t;
System.out.println(t2/10+"时开始运行进程"+a[0][0]);
}
if(t-ta1==a2 && sam==1 &&cia2==0){
sam = 0;
cia2= 1;
double t3=t;
System.out.println(t3/10+"时结束进程"+a[0][0]);
t=t-1;
num = num+1;
continue;
}
if(b1<=t && sam==0 && cib1==0){
sam = 1;
tb1 = t;
cib1= 1;
double t2=t;
System.out.println(t2/10+"时开始运行进程"+a[1][0]);
}
if(t-tb1==b2 && sam==1 &&cib2==0){
sam = 0;
cib2= 1;
double t3=t;
System.out.println(t3/10+"时结束进程"+a[1][0]);
t=t-1;
num = num+1;
continue;
}
if(c1<=t && sam==0 && cic1==0){
sam = 1;
tc1 = t;
cic1= 1;
double t2=t;
System.out.println(t2/10+"时开始运行进程"+a[2][0]);
}
if(t-tc1==c2 && sam==1 &&cic2==0){
sam = 0;
cic2= 1;
double t3=t;
System.out.println(t3/10+"时结束进程"+a[2][0]);
t=t-1;
num = num+1;
continue;
}
if(d1<=t && sam==0 && cid1==0){
sam = 1;
td1 = t;
cid1= 1;
double t2=t;
System.out.println(t2/10+"时开始运行进程"+a[3][0]);
}
if(t-td1==d2 && sam==1 &&cid2==0){
sam = 0;
cid2= 1;
num = num+1;
double t3=t;
System.out.println(t3/10+"时结束进程"+a[3][0]);
t=t-1;
continue;
}
if(num==4) {
System.out.println("所有进程已全部结束");
break;
}
}
System.out.println();
}
}
Java短作业优先
最新推荐文章于 2023-07-19 19:12:58 发布