I am trying to simulate processor scheduling algorithms in java and am using multithreading. I have successfully implemented FCFS (First Come First Serve) and SJF (Shortest Job First). But the problem is that I'm starting to think about SRTF (Shortest Remaining Time), which is a proactive form of SJF. I am using the following model:
- A thread for a CPU that has a variable
CLOCKthat saves a tick (simple increase) each 100ms. I have a flag boolean isAvailable;for processes to check if the CPU is available before starting. - A thread for a Long Term Scheduler (LTS) that pushes a process from the process list into a finished queue.
- A thread for a short-term scheduler (STS) that receives a process from ReadyQueue and assigns it to the processor.
- Once the process has been removed from the ReadyQueue STS for execution, the process checks the
isAvailableCPU flag . If true, it sets the flag to false and starts its execution (for which I just make the thread sleep for (100 * burstTime) ms, as this is just a simulation). Otherwise, the process simply comes alive: while(CPU.isAvailable != true);.
I have a list of processes along with their arrival and break time before distribution. This is normal until I simulate unmanaged scheduling (FCFS and SJF). But since I am trying to use SRTF, I cannot find a way to interrupt the current current thread of processes.
SRTF , , ReadyQueue. isAvailable false , , ? b/w, CPU. . . !
:
enum State {ARRIVED, WAITING, READY, RUNNING, EXECUTED}
public class Process implements Runnable
{
int pid;
int arrTime;
int burstTime;
int priority;
long startTime;
long endTime;
State procState = null;
Process(int pid, int arrTime, int burstTime, int priority)
{
this.pid = pid;
this.arrTime = arrTime;
this.burstTime = burstTime;
this.priority = priority;
this.procState = State.ARRIVED;
this.startTime = 0;
this.endTime = 0;
}
boolean isReady()
{
if((this.arrTime <= CPU.CLOCK) && (this.procState == State.ARRIVED))
return true;
else return false;
}
@Override
public void run() {
if(this.procState == State.READY)
this.procState = State.WAITING;
while(!CPU.isAvailable());
try
{
this.procState = State.RUNNING;
System.out.println("Process " + pid + " executing...");
this.startTime = CPU.CLOCK;
System.out.println("Process " + this.pid + ": Begins at " + this.startTime);
Thread.sleep(this.burstTime * 100);
this.endTime = CPU.CLOCK;
System.out.println("Process " + this.pid + ": Ends at " + this.endTime);
this.procState = State.EXECUTED;
}
catch (InterruptedException e)
{
System.out.println("Interrupted: " + pid);
e.printStackTrace();
}
}
}
CPU:
import java.util.LinkedList;
import java.util.Queue;
public class CPU implements Runnable
{
static Long CLOCK = new Long(0);
static LinkedList<Process> ReadyQ = new LinkedList<Process>();
private static boolean isAvailable = true;
static boolean done = false;
public static boolean isAvailable() {
return isAvailable;
}
public static void setAvailable(boolean isAvailable) {
CPU.isAvailable = isAvailable;
}
static void incrementCLOCK()
{
LTS.checkArrival();
CPU.CLOCK++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Clock Tick: " + CPU.CLOCK);
}
@Override
public void run() {
System.out.println("CPU starts.!!!");
while(CPU.done != true)
synchronized(CPU.CLOCK)
{
incrementCLOCK();
}
}
}
LTS:
public class LTS implements Runnable
{
private static Process[] pList = null;
private final int NUM;
static Integer procStarted;
static Integer procFinished;
static boolean STSDone = false;
LTS(Process[] pList, int num)
{
this.NUM = num;
LTS.pList = pList;
}
static void checkArrival()
{
if(pList == null) return;
for(int i = 0; i < pList.length; i++)
if(pList[i].isReady())
{
pList[i].procState = State.READY;
System.out.println("Process " + pList[i].pid + " is now ready.");
CPU.ReadyQ.add(pList[i]);
}
}
@Override
public void run() {
System.out.println("Long Term Scheduler starts.!!!");
while(LTS.STSDone != true)
{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(LTS.STSDone);
System.out.println("LTS ends.!!!");
CPU.done = true;
}
}