Professional Documents
Culture Documents
01 STRICT//EN"
"http://www.w3.org/TR/HTML401/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>FCFS CPU Simulation</title>
<script type="text/javascript">
var qTimeScale=5; //queue will move forward 5 ms
var qDelay=500; //every real 500ms
var procs=0;
var theq=null;
function Process(ct) { //create a process object
this.cpuTime=ct; //time required to complete the process
this.pname=procs++;
this.enqTime=0; //time the process is added to the queue
this.strTime=0; //time the process begins execution
this.endTime=0; //time the process completes execution
}
function Queue() { //create a queue
this.currentTime=0; //keep track of simulated queue-time
this.length=0; //current number of items in array
this.start=0; //first element position in array
this.qArray=Array();
}
function enq(item) { //enqueue a process
if(item != null) {
item.enqTime=this.currentTime;
if(this.isEmpty()) {
item.strTime=this.currentTime;
}
this.length++;
this.qArray[this.qArray.length]=item;
}
}
function dq() { //dequeue a process
if(this.length>0) {
this.length--;
return this.qArray[this.start++];
} else return null;
}
function ise() { //see if the queue is empty
if(this.length>0) return false;
return true;
}
function chk() { //see if we need to dequeue, etc
if(!this.isEmpty()) {
if((this.qArray[this.start].strTime+this.qArray[this.start].cpuTime)<=this.currentT
ime) {
var cout=this.dequeue();
cout.endTime=this.currentTime;
document.getElementById("res").value+="Process "+cout.pname+": total
"+cout.cpuTime+", enq "+cout.enqTime+", start "+cout.strTime+", end
"+cout.endTime+"\n";
if(!this.isEmpty()) {
this.qArray[this.start].strTime=this.currentTime;
}
}
}
}
function prn(divid) {
var theDiv=document.getElementById(divid);
theDiv.value="";
if(!this.isEmpty()) {
theDiv.value+="Process"+this.qArray[this.start].pname+": "+
(this.qArray[this.start].cpuTime-(this.currentTime-
this.qArray[this.start].strTime))+"\n";
for(var i=this.start+1; i<this.start+this.length; i++) {
theDiv.value+="Process"+this.qArray[i].pname+":
"+this.qArray[i].cpuTime+"\n";
}
}
}
function incTime() {
setTimeout("incTime()", qDelay);
theq.currentTime+=qTimeScale;
window.status=theq.currentTime;
theq.check();
theq.print("qdisplay");
document.getElementById("ctm").value=theq.currentTime;
}