-
Notifications
You must be signed in to change notification settings - Fork 0
/
FCFS.java
140 lines (125 loc) · 4.21 KB
/
FCFS.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import java.util.ArrayList;
import java.util.Collections;
public class FCFS {
ArrayList<String> processesname;
ArrayList<Integer> ArrivalTime;
ArrayList<Integer> BurstTime;
ArrayList<String> Order = new ArrayList<String>();
ArrayList<Integer> WaitingTime;
ArrayList<Integer> turnaroundtime;
double averagewaitingtime;
double averageturnaroundtime;
int processesnumber;
int quantumtime;
FCFS(int quantumtime,ArrayList<Integer> ArrivalTime,ArrayList<Integer> BurstTime,ArrayList<String> processesname){
this.quantumtime = quantumtime;
this.processesname = processesname;
this.ArrivalTime = ArrivalTime;
this.BurstTime = BurstTime;
processesnumber = ArrivalTime.size();
WaitingTime = new ArrayList<Integer>(Collections.nCopies(processesnumber, 0));
turnaroundtime = new ArrayList<Integer>(Collections.nCopies(processesnumber, 0));
}
public void doFCFS(){
int time = ArrivalTime.get(0);
int activeprocessindex = 0;
String activeprocess = processesname.get(0);
int nextarrivaltime = ArrivalTime.get(1);
boolean avail = false;
int responsetime = ArrivalTime.get(0);
int nextprocessidx = 1;
int remainingtime = BurstTime.get(0);
ArrayList<Integer> remainingtimelist = new ArrayList<Integer>(BurstTime);
ArrayList<Integer> waitingqueueindex = new ArrayList<Integer>();
ArrayList<Integer> waitingqueueremainingtime = new ArrayList<Integer>();
ArrayList<Integer> waitingqueuepriority = new ArrayList<Integer>();
while(true){
// check if there's no more processes
if(checkzeros(remainingtimelist)) {
break;
}
// if a process arrived
for(int i=0;i<processesnumber;i++) {
if(activeprocessindex == i) {
continue;
}
if(ArrivalTime.get(i) == time) {
waitingqueueindex.add(i);
waitingqueueremainingtime.add(BurstTime.get(i));
}
}
///////
//if the process has finished
if(activeprocessindex!=-1) {
if(remainingtimelist.get(activeprocessindex)==0) {
avail = true;
activeprocessindex = -1;
}
}
//if there is no process ongoing
if(avail){
if(!waitingqueueindex.isEmpty()) {
activeprocessindex = waitingqueueindex.get(0);
remainingtime = waitingqueueremainingtime.get(0);
waitingqueueindex.remove(0);
waitingqueueremainingtime.remove(0);
avail = false;
}
else {
activeprocessindex = -1;
}
}
///////////////////
if(activeprocessindex!=-1) {
if(Order.isEmpty()) {
Order.add(processesname.get(activeprocessindex));
}
else {
if(!Order.get(Order.size()-1).equals(processesname.get(activeprocessindex))) {
Order.add(processesname.get(activeprocessindex));
}
}
}
for(int i=0;i<processesnumber;i++) {
if((ArrivalTime.get(i) <= time) && (i!=activeprocessindex) && (remainingtimelist.get(i)!=0)) {
WaitingTime.set(i, WaitingTime.get(i)+1);
}
}
if(activeprocessindex!=-1) {
remainingtimelist.set(activeprocessindex, remainingtimelist.get(activeprocessindex)-1);
}
time++;
}
for(int i=0;i<processesnumber;i++) {
turnaroundtime.set(i, WaitingTime.get(i)+BurstTime.get(i));
}
int waitingsum=0;
int turnaroundtimesum = 0;
for(int i=0;i<processesnumber;i++) {
waitingsum+=WaitingTime.get(i);
turnaroundtimesum+=turnaroundtime.get(i);
}
averagewaitingtime = (double)waitingsum/(double)processesnumber;
averageturnaroundtime = (double)turnaroundtimesum/(double)processesnumber;
}
boolean checkzeros(ArrayList<Integer> a) {
for(int i:a) {
if(i!=0) {
return false;
}
}
return true;
}
public void printarrstr(ArrayList<String> a) {
for(int i=0;i<a.size();i++) {
System.out.print(a.get(i) + " ");
}
System.out.println();
}
public void printarrint(ArrayList<Integer> a) {
for(int i=0;i<a.size();i++) {
System.out.print(a.get(i) + " ");
}
System.out.println();
}
}