进程线程调度 FCFS

package PCProgram;


public class FCFSProcess {
public static void main(String[] args) {
MyQueue myqueue = new MyQueue();// 声明队列
PCB[] pcb = { new PCB("进程1", 8), new PCB("进程2", 7), new PCB("进程3", 3),
new PCB("进程4", 1), new PCB("进程5", 7) };
System.out.println();
myqueue.start(pcb);
}
}


class MyQueue {
int index = 0;// 索引号
PCB[] pc = new PCB[5];
PCB[] pc1 = new PCB[4];
PCB temp = new PCB();


public void enQueue(PCB process) {
// 入队算法
if (index == 5) {
System.out.println("out of bounds !");
return;
}
pc[index] = process;
index++;
}


public PCB deQueue() {
// 出队算法
if (index == 0)
return null;
for (int i = 0; i < pc1.length; i++) {
pc1[i] = pc[i + 1];
}
index--;
temp = pc[0];
for (int i = 0; i < pc1.length; i++) {
pc[i] = pc1[i];
}
return temp;
}


public void start(PCB[] pc) {
int m = 1;
// 显示进程表算法
while (pc[0].isNotFinish == true || pc[1].isNotFinish == true
|| pc[2].isNotFinish == true || pc[3].isNotFinish == true
|| pc[4].isNotFinish == true) {
// *注意:||运算符,所有表达式都为false结果才为false,否则为true
System.out.println("第" + m + "轮运行开始:");
m++;
for (int i = 0; i < pc.length; i++) {
pc[i].run(this);
}
System.out.println();


}
System.out.println("所有进程结束。");
}
}


class PCB {
String name;// 声明进程类
int totaltime, runtime;
String State = "待运行";
boolean isNotFinish;


public PCB() {


}


public PCB(String name, int totaltime) {
this.name = name;// 进程名
this.totaltime = totaltime;// 总时间
this.runtime = 2;// 时间片,这里设值为2
this.isNotFinish = true;// 是否执行完毕
System.out.println("初始值:进程号 : " + name + " 总用时 : " + totaltime
+ " 状态 :" + State);
System.out.println();
}


public void run(MyQueue mq) {
// 进程的基于时间片的执行算法
if (totaltime > 1) {
totaltime -= runtime;// 在总时间大于1的时候,总时间=总时间-时间片
if (totaltime == 0) {
State = "运行结束";
isNotFinish = false;
}


System.out.println("进程号 : " + name + " 剩余时长 : " + totaltime
+ "状态 :" + State);
} else if (totaltime == 1) {
totaltime--;// 在总时间为1时,执行时间为1
State = "运行结束";
isNotFinish = false;
System.out.println("进程号 : " + name + " 剩余时长 : " + totaltime
+ "状态 :" + State);
} else {
isNotFinish = false;// 总时间为0,将isNotFinish标记置为false
}
if (isNotFinish == true) {
mq.deQueue();
mq.enQueue(this);
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值