3.CPU模型

一个中央处理器可带若干个终端。终端每隔一定时间(均值为25秒的指数分布)向CPU传送一个任务,服务时间服从均值为0.8秒的指数分布。
该计算机系统用轮转法选择下一进程。
1 CPU为每一个任务分配的服务时间最大为:
q=0.1秒。
2 若进程的剩余服务时间s<=q,CPU用s+t时间
处理该进程(t=0.015秒,切换时间)。
3 若s>q,CPU用q+t完成该任务的处理
和切换后,该任务排到队尾,剩余服
务时间减少q。CPU为下一进程服务。
4 重复上述步骤直到某任务结果返回终
端,经过一定间隔时间下一任务再传
出。
5 模拟处理完1000个任务的情形。在响
应时间低于30秒的前提下,求CPU可
带最大终端数和CPU的利用率。

事件:n个到达,1个离开
思路:
1.到达事件:记录到达时间,服务时间,所属终端,该终端的下一个到达时间无穷。
若CPU不忙,则占用CPU,CPU状态设忙,安排占用CPU.
2.安排占用CPU:若本次可以完成处理,则安排离开系统的时间,所需的剩余服务时间为0;若还得继续处理,则安排离开CPU时间,计算所需的剩余服务时间。
3.离开CPU事件:若已经处理完成,计算响应时间,安排下一个的到达,若系统为空,则设备只闲,下次离开CPU不发生,若不空,则安排队首占用CPU。
若没服务完成,若系统只有自己,安排自己占用CPU,若系统还有其他人,安排队首占用CPU,自己排到队尾。

java程序1:事件数组版本:
到达事件:

public void arrive(int terminalNo){
this.currentTime=events[terminalNo];
this.numberOfTaskInSystem++;
Task tmp=new Task();
tmp.setArriveTime(events[terminalNo]);
tmp.setServiceTime(this.GenerateExpRandom(theta2));
tmp.setTerminalNo(terminalNo);
queue.add(tmp);
System.out.println("at "+this.currentTime+"----terminal["+tmp.getTerminalNo()+"] arrived...");
events[terminalNo]=this.INFINITE;

if(statusBusy==false){
this.statusBusy=true;
this.task=this.queue.getFirst();
this.occupyCPU();
}


}


占用CPU:

public void occupyCPU(){
//this.task=queue.getFirst();
if(task.getServiceTime()<=this.cycleTime){
events[TerminalSize+1]=task.getServiceTime()+this.currentTime;
task.setServiceTime(0.0);
}else{
events[TerminalSize+1]=this.cycleTime+this.currentTime;
task.setServiceTime(task.getServiceTime()-this.cycleTime);
}
}



离开CPU:

public void depart(){
this.currentTime=events[TerminalSize+1];
if(task.getServiceTime()==0){
// 可以离开系统,计算停留时间
this.stayingTime+=this.currentTime-task.getArriveTime();
this.finishNumer++;
//安排此终端下次到达时间
events[task.getTerminalNo()]=this.currentTime+this.GenerateExpRandom(theta1);
this.numberOfTaskInSystem--;
queue.removeFirst();
System.out.println("at "+this.currentTime+"----terminal["+task.getTerminalNo()+"] depart cpu...");

if(this.numberOfTaskInSystem==0){
this.statusBusy=false;
events[TerminalSize+1]=this.INFINITE;

}else{

this.task=queue.getFirst();
this.occupyCPU();
}
}else{
if(this.numberOfTaskInSystem==1){
this.occupyCPU();
}else{
Task tmp=queue.getFirst();
queue.removeFirst();
queue.addLast(tmp);
this.task=queue.getFirst();
this.occupyCPU();
}
}
}


链表版本:
到达CPU:

public void arrive(Event e){
this.currentTime=e.getTime();
this.numberOfTaskInSystem++;
Task tmp=new Task();
tmp.setArriveTime(e.getTime());
tmp.setServiceTime(this.GenerateExpRandom(theta2));
tmp.setTerminalNo(e.getTerminalNo());
queue.add(tmp);
System.out.println("at "+this.currentTime+"----terminal["+tmp.getTerminalNo()+"] arrived...");


if(statusBusy==false){
this.statusBusy=true;
this.task=this.queue.getFirst();
this.occupyCPU();
}


}


占用CPU:

public void occupyCPU(){
//this.task=queue.getFirst();
Event e=new Event();
e.setEventType(Event.departType);
e.setTerminalNo(task.getTerminalNo());

if(task.getServiceTime()<=this.cycleTime){

e.setTime(task.getServiceTime()+this.currentTime);
task.setServiceTime(0.0);
}else{
e.setTime(this.cycleTime+this.currentTime);
task.setServiceTime(task.getServiceTime()-this.cycleTime);
}

eventList.add(e);
}


离开CPU:

public void depart(Event e){
this.currentTime=e.getTime();
if(task.getServiceTime()==0){
// 可以离开系统,计算停留时间
this.stayingTime+=this.currentTime-task.getArriveTime();
this.finishNumer++;
//安排此终端下次到达时间
//System.out.println("测试两个地方的终端号:"+(e.getTerminalNo()==task.getTerminalNo()));
Event e1=new Event();
e1.setEventType(Event.arriveType);
e1.setTerminalNo(e.getTerminalNo());
e1.setTime(this.currentTime+this.GenerateExpRandom(theta1));
eventList.add(e1);

this.numberOfTaskInSystem--;
queue.removeFirst();
System.out.println("at "+this.currentTime+"----terminal["+task.getTerminalNo()+"] depart cpu...");

if(this.numberOfTaskInSystem==0){
this.statusBusy=false;


}else{

this.task=queue.getFirst();
this.occupyCPU();
}
}else{
if(this.numberOfTaskInSystem==1){
this.occupyCPU();
}else{
Task tmp=queue.getFirst();
queue.removeFirst();
queue.addLast(tmp);
this.task=queue.getFirst();
this.occupyCPU();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值