-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFirstComeFirstServe.java
80 lines (74 loc) · 2.83 KB
/
FirstComeFirstServe.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.*;
public class FCFS{
// process {id, arrival, duration}
static int[][] process = {{1,0,5},{2,4,3},{3,2,4},{4,12,2}};
static ArrayList<int[]> complete = new ArrayList<>();
public static void main(String[] args){
while(!isAllcomplete()){
complete.add(getNextProcess());
}
System.out.println("FCFS CPU SCHEDULING : ");
int startTime = complete.get(0)[1];
for(int i = 0 ; i < complete.size(); i++){
int[] p = complete.get(i);
System.out.println(i+" :: process id : "+p[0]+", arrival : "+p[1]+", duration : "+p[2]
+", time esplased : "+startTime+" to "+(startTime+p[2])
+", waiting time : "+(startTime-p[1])+", turnaround time : "+((startTime+p[2])-p[1]));
startTime += p[2];
}
System.out.println("");
System.out.println("Average Waiting Time = "+getAvgWaitingTime());
System.out.println("");
System.out.println("Average turnAround Time = "+getAvgTurnAroundTime());
System.out.println("");
System.out.println("throughput = "+getThroughput());
}
public static float getAvgWaitingTime(){
float startTime = complete.get(0)[1];
float sumofwait = 0;
for(int i = 0; i < complete.size(); i++){
int[] p = complete.get(i);
sumofwait += (startTime - p[1]);
startTime += p[2];
}
System.out.println("total wait time : "+sumofwait);
return (sumofwait/(process.length));
}
public static float getAvgTurnAroundTime(){
float startTime = complete.get(0)[1];
float sumofturnAround = 0;
for(int i = 0; i < complete.size(); i++){
int[] p = complete.get(i);
sumofturnAround += ((startTime+p[2]) - p[1]);
startTime += p[2];
}
System.out.println("total turnAroundTime : "+sumofturnAround);
return (sumofturnAround/(process.length));
}
public static float getThroughput(){
float sumOfDuration = 0;
for (int[] each : process){
sumOfDuration += each[2];
}
return (process.length/sumOfDuration);
}
public static int[] getNextProcess(){
int pid = 0;
for(int i = 0; i < process.length; i++){ if(!complete.contains(process[i])){ pid = i; break;}}
for(int i = 0; i < process.length; i++){
if(!complete.contains(process[i])){
if(process[i][1] < process[pid][1]){
pid = i;
}
}
}
return process[pid];
}
public static boolean isAllcomplete(){
boolean isComplete = true;
for(int[] each : process){
if(!complete.contains(each)){ isComplete = false;}
}
return isComplete;
}
}