@@ -15,6 +15,10 @@ class ReadyQue
public List <Process > readyProcesses ;
public bool running ;
public Stopwatch timer ;
public int simulation_time ;
public int total_time_running ;
public double cpu_utilization ;
private int jobs_completed ;
public ReadyQue (Form1 form , CPU cpu )
{
this .form = form ;
@@ -24,6 +28,9 @@ public ReadyQue(Form1 form, CPU cpu)
}
public void run ()
{
total_time_running = 0 ;
cpu_utilization = 0 ;
jobs_completed = 0 ;
// Calculate time quantum
int quantum = 0 ;
foreach (Process p in processes )
@@ -50,6 +57,14 @@ public void run()
form .lblTimeRunning .Text = (timer .ElapsedMilliseconds / 1000 ).ToString ();
});
}
// Update simulation time
if (form .InvokeRequired )
{
form .lblTimeRunning .Invoke ((MethodInvoker )delegate ()
{
form .lblSimulationTime .Text = simulation_time .ToString ();
});
}
// Update ready que size
if (form .lblReadyQueSize .InvokeRequired )
{
@@ -58,6 +73,20 @@ public void run()
form .lblReadyQueSize .Text = readyProcesses .Count .ToString ();
});
}
// Calculate average context switch
int avg_context_switch = 0 ;
int rows = form .dataGridView1 .Rows .Count ;
for (int i = 0 ;i < form .dataGridView1 .Rows .Count - 1 ;i ++ )
{
avg_context_switch += int .Parse (form .dataGridView1 [" ContextSwitches" , i ].Value .ToString ());
}
if (form .lblAvgContextSwitch .InvokeRequired )
{
form .lblAvgContextSwitch .Invoke ((MethodInvoker )delegate ()
{
form .lblAvgContextSwitch .Text = (avg_context_switch / rows ).ToString ();
});
}
// Add Processes to ready que when arrival time is passed
foreach (Process process in processes )
{
@@ -71,9 +100,9 @@ public void run()
}
}
}
// Check if there are still processes left to run
if (processes .Count == 0 && readyProcesses .Count == 0 )
{
// If no processes, CPU is idle
MessageBox .Show (" Idle cpu" );
// Attempt to clear the ready que
form .processes = new List <Process >();
@@ -86,6 +115,7 @@ public void run()
{
// Run the process for the allowed timeslice
cpu .runProcess (readyProcesses [0 ], quantum );
jobs_completed ++ ;
}
}
if (form .readyQuePanel .InvokeRequired )
@@ -98,6 +128,25 @@ public void run()
{
form .readyQuePanel .Refresh ();
}
// Calculate CPU Utilization
if (total_time_running > 0 )
cpu_utilization = Math .Round ((double )(100 - (timer .ElapsedMilliseconds / total_time_running )),2 );
// Update CPU Utilization
if (form .lblCpuUtilization .InvokeRequired )
{
form .lblCpuUtilization .Invoke ((MethodInvoker )delegate ()
{
form .lblCpuUtilization .Text = cpu_utilization + " %" ;
});
}
// Update jobs completed
if (form .lblJobsCompleted .InvokeRequired )
{
form .lblJobsCompleted .Invoke ((MethodInvoker )delegate ()
{
form .lblJobsCompleted .Text = jobs_completed .ToString ();
});
}
}
}
public void paint (object sender , PaintEventArgs e )