Last active
December 29, 2015 15:29
-
-
Save JSkally/7691386 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.lang.Math; | |
class Process | |
{ | |
public static final boolean CPU_BURST=true, IO_BURST=false; | |
private int pid; // process ID | |
private int CPU_BurstTime; // CPU burst time | |
private int IO_BurstTime; // IO burst time | |
private int CPU_Bursts; // number of CPU bursts | |
private int IO_Bursts; // number of I/O bursts | |
private int finishTime; | |
private int leftTime; | |
private int burstLeftTime; | |
private int ioBurstLeftTime; | |
private int ioLeftBursts; | |
private int leftBursts; | |
private int burstIndex=0; | |
private int arrivalTime; | |
private int tempArrivalTime; | |
private int waitingTime; | |
private int flag; | |
private boolean sequence []; | |
// Process constructor | |
public Process() { | |
this(0,0,0,0,0); | |
} | |
public Process(int pid,int cpuBurstsNum, int cpuBurstTime, int ioBurstsNum, int ioBurstTime) { | |
this.pid = pid; | |
this.CPU_BurstTime = cpuBurstTime; | |
this.IO_BurstTime = ioBurstTime; | |
this.CPU_Bursts = cpuBurstsNum; | |
this.IO_Bursts = ioBurstsNum; | |
burstLeftTime = CPU_BurstTime; | |
ioBurstLeftTime = IO_BurstTime; | |
leftBursts = CPU_Bursts; | |
ioLeftBursts = IO_Bursts; | |
leftTime = getTotalCPUTime(); | |
sequence = new boolean[CPU_Bursts+IO_Bursts]; | |
generateSequence(); | |
waitingTime = 0; | |
flag = 1; | |
//printSequence(); | |
} | |
public int getTotalCPUTime() { | |
return CPU_BurstTime*CPU_Bursts; | |
} | |
public int getBurstLeftTime() { | |
return burstLeftTime; | |
} | |
public int getLeftBursts() { | |
return leftBursts; | |
} | |
public int getIOBurstLeftTime() { | |
return ioBurstLeftTime; | |
} | |
public void servedOneIOClock() { | |
ioBurstLeftTime--; | |
} | |
public int getIOBurstsLeft() { | |
return ioLeftBursts; | |
} | |
public String toString() { | |
return (pid + " - " +getTotalCPUTime()); | |
} | |
public int getRemainingTime() { | |
return leftTime; | |
} | |
public void servedOneClock() { | |
leftTime--; | |
burstLeftTime--; | |
} | |
public void servedOneIOBurst() { | |
ioLeftBursts--; | |
ioBurstLeftTime = IO_BurstTime; | |
burstIndex++; | |
} | |
public void servedOneBurst() { | |
leftBursts--; | |
burstLeftTime = CPU_BurstTime; | |
burstIndex++; | |
} | |
public boolean getBurstType() { | |
return sequence[burstIndex-1]; | |
} | |
public void finishedAt(int t) { | |
finishTime=t; | |
} | |
public int getPID() { | |
return pid; | |
} | |
public void setArrivalTime(int t) { | |
arrivalTime =t; | |
flag = 1; // to indicate the first burst has already arrived. | |
} | |
public void setTempArrivalTime(int t) { | |
tempArrivalTime =t; | |
} | |
public void setItsWait(int clock) { | |
if(flag == 1){ | |
waitingTime = (clock - arrivalTime); | |
flag = 0; | |
} | |
} | |
int getFlag() { | |
return flag; | |
} | |
public int getItsWait() { | |
return waitingTime; | |
} | |
public int getArrivalTime() { | |
return arrivalTime; | |
} | |
public void generateSequence() { | |
int cpu = 1; | |
int io = 0; | |
sequence[0] = true; | |
for( int i = 1; cpu < this.CPU_Bursts || io < this.IO_Bursts ; i++) { | |
if((i%2==0) || (io == this.IO_Bursts)) { | |
sequence[i] = true; | |
cpu++; | |
} | |
else { | |
sequence[i] = false; | |
io++; | |
} | |
} | |
} | |
public int getTotalBursts() { | |
return sequence.length; | |
} | |
public int getBurstIndex() { | |
return burstIndex; | |
} | |
public int getTurnAroundTime() { | |
return finishTime; | |
} | |
public void printSequence() { | |
System.out.print("[" ); | |
for(int i=0;i<sequence.length-1; i++){ | |
System.out.print(sequence[i] + ","); | |
} | |
System.out.println(sequence[sequence.length-1] + "]"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment