You are on page 1of 2

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.

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;
}

//setup the object prototype methods


new Queue();
Queue.prototype.enqueue = enq;
Queue.prototype.dequeue = dq;
Queue.prototype.isEmpty = ise;
Queue.prototype.check = chk;
Queue.prototype.print = prn;
Queue.prototype.startup = incTime;
</script>
</head>
<body>
<div id="maincontent">
<input type="text" value="0" readonly="true" id="ctm"> Current Time<br>
<a href="#" onclick="if(/^[123456789]
[\d]*$/.test(document.getElementById('ptm').value)) q.enqueue(new
Process(parseInt(document.getElementById('ptm').value)));theq.print('qdisplay');ret
urn false;">Add a process</a>
<input type="text" value="0" id="ptm" onmousedown="this.value=''"> CPU Time
Required<br>
<textarea style="position:relative;border:solid #000000 2px;background-
color:#cccccc;width:40%;height:200px;overflow:auto" id="qdisplay"></textarea>
<textarea style="position:relative;border:solid #000000 2px;background-
color:#cccccc;width:50%;height:200px;overflow:auto" id="res"></textarea>
<script type="text/javascript">
//create a queue and start its timer
var q=new Queue();
theq=q;
q.startup();
</script>
</div>
</body>
</html>

You might also like