Skip to content

Commit b06fe01

Browse files
committed
Programming Assignment - Shortest Job First Scheduling
1 parent 41fad63 commit b06fe01

File tree

10 files changed

+275
-0
lines changed

10 files changed

+275
-0
lines changed
465 Bytes
Binary file not shown.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.ArrayList;
2+
import java.util.Collection;
3+
import java.util.Iterator;
4+
import java.util.List;
5+
import java.util.Queue;
6+
7+
/* implement this class for all three strategies */
8+
9+
public abstract class AllocationStrategy {
10+
protected List<Job> Jobs;
11+
protected ArrayList<Job> Queue;
12+
13+
public AllocationStrategy(List<Job> jobs) {
14+
super();
15+
Jobs = jobs;
16+
}
17+
18+
public abstract void run();
19+
// update current job by 1 tick
20+
// check if the job queue might need to be changed.
21+
// check for jobs to add to the queue
22+
}
1.71 KB
Binary file not shown.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
public class Job {
2+
private int id, submitTime, CPUTime, CPUTimeLeft;
3+
4+
private int startTime = 0, endTime = 0;
5+
6+
7+
public int ProcessCompletionTime;
8+
public int processArrivalTime;
9+
public int waitingTime;
10+
public int turnAroundTime;
11+
12+
private int arrivalTime,cpuTime,processId;
13+
14+
public Job(int processId, int arrivalTime, int cpuTime) {
15+
16+
this.processId = processId;
17+
this.arrivalTime = arrivalTime;
18+
this.cpuTime = cpuTime;
19+
20+
}
21+
22+
public void start(int sysTime) {
23+
startTime = sysTime;
24+
}
25+
26+
public int getId() {
27+
return id;
28+
}
29+
30+
public void setId(int id) {
31+
this.id = id;
32+
}
33+
34+
public int getSubmitTime() {
35+
return submitTime;
36+
}
37+
38+
public void setSubmitTime(int submitTime) {
39+
this.submitTime = submitTime;
40+
}
41+
42+
public int getCPUTime() {
43+
return CPUTime;
44+
}
45+
46+
public void setCPUTime(int cPUTime) {
47+
CPUTime = cPUTime;
48+
}
49+
50+
public int getCPUTimeLeft() {
51+
return CPUTimeLeft;
52+
}
53+
54+
public void setCPUTimeLeft(int cPUTimeLeft) {
55+
CPUTimeLeft = cPUTimeLeft;
56+
}
57+
58+
public int getStartTime() {
59+
return startTime;
60+
}
61+
62+
public void setStartTime(int startTime) {
63+
this.startTime = startTime;
64+
}
65+
66+
public int getEndTime() {
67+
return endTime;
68+
}
69+
70+
public void setEndTime(int endTime) {
71+
this.endTime = endTime;
72+
}
73+
74+
public int getArrivalTime() {
75+
return arrivalTime;
76+
}
77+
78+
public void setArrivalTime(int arrivalTime) {
79+
this.arrivalTime = arrivalTime;
80+
}
81+
82+
public int getCpuTime() {
83+
return cpuTime;
84+
}
85+
86+
public void setCpuTime(int cpuTime) {
87+
this.cpuTime = cpuTime;
88+
}
89+
90+
public int getProcessId() {
91+
return processId;
92+
}
93+
94+
public void setProcessId(int processId) {
95+
this.processId = processId;
96+
}
97+
98+
99+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public interface JobFinishEvent {
2+
public void onFinish(Job j);
3+
}
1.78 KB
Binary file not shown.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
import java.io.BufferedReader;
3+
import java.io.FileReader;
4+
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Scanner;
8+
9+
10+
public class Question1 {
11+
12+
public static void main(String[] args) {
13+
14+
int quantum=20;
15+
String filename = "testing.txt";
16+
17+
BufferedReader br = null;
18+
19+
try {
20+
21+
String sCurrentLine;
22+
23+
br = new BufferedReader(new FileReader(filename));
24+
System.out.println("processId arrivalTime cpuTime");
25+
26+
List<Job> jobList = new ArrayList<Job>();
27+
while ((sCurrentLine = br.readLine()) != null) {
28+
29+
String a[] = sCurrentLine.split(",");
30+
int processId = new Integer(a[0]);
31+
int arrivalTime = new Integer(a[1]);
32+
int cpuTime = new Integer(a[2]);
33+
34+
Job job = new Job(processId, arrivalTime, cpuTime);
35+
jobList.add(job);
36+
37+
System.out.println(processId + "\t\t" + arrivalTime + "\t\t" + cpuTime);
38+
}
39+
40+
ShortestRemainingTime srtf = new ShortestRemainingTime(jobList);
41+
srtf.run(jobList);
42+
43+
} catch (IOException e) {
44+
e.printStackTrace();
45+
} finally {
46+
try {
47+
if (br != null) br.close();
48+
} catch (IOException ex) {
49+
ex.printStackTrace();
50+
}
51+
}
52+
}
53+
54+
}
Binary file not shown.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import java.util.List;
2+
3+
4+
public class ShortestRemainingTime extends AllocationStrategy {
5+
6+
public ShortestRemainingTime(List<Job> jobs) {
7+
super(jobs);
8+
}
9+
10+
public void run() {
11+
12+
}
13+
14+
public void run(List<Job> jobList) {
15+
16+
float avgTurnArroundTime = 0;
17+
float avgWaitigTime = 0;
18+
int n;
19+
n = jobList.size();
20+
int p[] = new int[n];
21+
int at[] = new int[n];
22+
int bt[] = new int[n];
23+
int bt2[] = new int[n];
24+
int wt[] = new int[n];
25+
int tat[] = new int[n];
26+
int count = 0;
27+
double tempWT=0;
28+
for (Job job:jobList) {
29+
p[count] = count;
30+
at[count] = job.getArrivalTime();
31+
bt[count] = job.getCpuTime();
32+
bt2[count] = bt[count];
33+
count++;
34+
}
35+
int tbt = 0;
36+
for (int i = 0; i < n; i++) {
37+
tbt = tbt + bt[i];
38+
}
39+
int time[] = new int[tbt];
40+
int k = 0;
41+
int q2 = 0;
42+
43+
System.out.println("============================================ ");
44+
System.out.println("Process ID | Turnaround time | Waiting time ");
45+
System.out.println("============================================ ");
46+
int counter=1;
47+
for (int i = 0; i < tbt; i++) {
48+
49+
int q = Min(bt, at, tbt, i, n);
50+
51+
if (q != q2) {
52+
time[k++] = i;
53+
54+
wt[q] = i;
55+
tat[q] = i + bt[q];
56+
counter++;
57+
tempWT+=wt[q];
58+
}
59+
avgWaitigTime+= wt[q];
60+
avgTurnArroundTime+=tat[q];
61+
bt[q] = bt[q] - 1;
62+
q2 = q;
63+
64+
System.out.println( (p[q]+1) +"\t|\t"+tat[q]+"\t|\t"+wt[q]);
65+
System.out.println("----------------------------------------");
66+
67+
}
68+
time[k] = tbt;
69+
System.out.println();
70+
System.out.print("0\t");
71+
72+
for (int i = 0; i <= k; i++) {
73+
System.out.print(time[i] + "\t");
74+
}
75+
System.out.println("\n============================================ ");
76+
System.out.println("Avg WT||TAT::"+tempWT/counter+"|"+avgTurnArroundTime/counter);
77+
System.out.println("============================================ ");
78+
79+
}
80+
81+
public int Min(int b[], int a[], int tbt, int r, int n) {
82+
int j = 0;
83+
int min = tbt;
84+
for (int i = n - 1; i != 0; i--) {
85+
if (b[i] < min && b[i] > 0 && r != a[i]) {
86+
min = b[i];
87+
j = i;
88+
}
89+
}
90+
return j;
91+
}
92+
93+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1,0,8
2+
2,1,4
3+
3,2,9
4+
4,3,5

0 commit comments

Comments
 (0)