Hey there :) :) :)
I'm doing a "simulation" project for the first-come, first-served (FCFS) CPU scheduling algorithm.
To give u an idea of what this is:
CPU Scheduling is all about having a Scheduler determine which process should be allocated to the CPU next. It has many various algorithms such as priority sched, shortest-job-first, etc. But for this project, I'm doing the FCFS.
Yes! It's supposed to be the Easiest sched algorithm to implement. :P
Of course, since this is a "Simulation", I'm gonna have to make representations (models and not the real thing in the system) of the CPU, the processes and the scheduler.
All I have to do is store all processes in a FIFO queue. But the problem is that my teacher insists on incorporating a "Math Equation" to the program since it's supposed to be a "Simulation".
So I've found out about the "Little's Formula"
which is quite popular for Queueing Problems.
Now, the program specifics:
Processes will be generated randomly.
Each process will have a CPU-burst and an IO-burst which are both randomized.
Everytime a process becomes Ready, it will be added to the Queue.
The Scheduler then will allocate the CPU to the process if it is already that process's turn (if it's in front of the queue).
Once the process is allocated to the CPU, it will then be Executed based on its CPU-burst value. After that, the process will be thrown to another queue in case it has a randomized IO-burst. After the IO-burst it will be thrown back to the Ready Queue and wait for its turn to the CPU again.
This will keep looping until a certain number of processes has been allocated to the CPU and then the Loop will stop.
The AVERAGE WAITING TIME for the processes while they are in the Ready Queue is to be determined.
Here's what I've got so far and please tell me if i'm on the right track... :)
The code below basically creates 3 Threads (i used threads to represent processes)
and allocates them to the CPU but their CPU-bursts are fixed for now and they dont have IO bursts yet.
here's the Sched class
import java.util.*;
public class Sched extends Thread {
private Queue q;
private int burst;
private Thread current;
public Sched() {
this.setPriority(6);
System.out.println("Set to priority 6: " + this.getName());
burst = 1000;
q = new LinkedList();
}
public Sched(int b) {
burst = b;
q = new LinkedList();
}
public void addThread(Thread t){
q.offer(t); //add process/ thread to the Q
System.out.println("Thread added: " + t.getName());
}
public void executeThread(){
try{
System.out.println("Executing: " + current.getName());
Thread.sleep(burst);
}
catch(InterruptedException ex){};
}
public void run(){
while(true){
//
current = (Thread)q.poll(); //remove the process/ thread from the Q
if(current!=null){
System.out.println("Current thread to execute: " + current.getName());
executeThread();
}
else{
if(q.isEmpty()){
System.out.println("Queue is already empty...");
break;
}
}
}
}
}
and here's the main
public class CPUSched {
public static void main(String[] args) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
Sched CPUScheduler = new Sched();
CPUScheduler.start();
Thread t1 = new Thread("Thread 1");
t1.start();
CPUScheduler.addThread(t1);
Thread t2 = new Thread("Thread 2");
t2.start();
CPUScheduler.addThread(t2);
Thread t3 = new Thread("Thread 3");
t3.start();
CPUScheduler.addThread(t3);
}
}
I can easily add the Program Specifics above which is to keep on generating the processes with randomized cpu and io burst values.
but the problem is on incorporating the Little's formula to calculate for the Average Waiting time.
the formula is as easy as:
n = X * w
where n is the average size of the queue
X is the average ARRIVAL RATE of the processes into the queue (in processes per sec)
w is the AVERAGE WAITING TIME
here's the specific help that i need: :D :D :D
i have to keep on generating the processes not only with random burst values but also at Random Times, meaning each process should also have random arrival time.
Is there any way to track the Time when the whole program starts in Java?
and how do i incorporate it into this project....
thanks much <3 <3
oh and before i forget,
i also cant figure out how to display a Line Graph in java...
The time should be running on the X-axis and the average waiting time should be plotted on the Y-axis... and it should keep running while the program is scheduling and executing the processes... are there any packages in Java i can use for this???