-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab2.java
More file actions
201 lines (171 loc) · 8.8 KB
/
Copy pathLab2.java
File metadata and controls
201 lines (171 loc) · 8.8 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/******************************************************************************
Online Java Debugger.
Code, Run and Debug Java program online.
Write your code in this editor and press "Debug" button to debug program.
*******************************************************************************/
// From Faliq and Am SCSR Class
import java.util.*;
public class Lab2
{
public static void main(String[] args) {
System.out.println("Worstfit");
//PARTITION BLOCK
ArrayList<Integer> partition=new ArrayList<Integer>();
ArrayList<String> status = new ArrayList<String>();
String pass="Available";
String stop="Unavailable";
partition.add(0) ;status.add("FOR OS PURPOSE ONLY");
partition.add(9500);status.add(pass);
partition.add(7000);status.add(pass);
partition.add(4500);status.add(pass);
partition.add(8500);status.add(pass);
partition.add(3000);status.add(pass);
partition.add(9000);status.add(pass);
partition.add(1000);status.add(pass);
partition.add(5500);status.add(pass);
partition.add(1500);status.add(pass);
partition.add(500);status.add(pass);
//ArrayList<Integer> checkPart=(ArrayList<Integer>)partition.clone();
//Collections.copy(checkPart,partition);
int totalMemory = 0;
for (int i: partition) {
totalMemory += i;
}
// Here aList is an ArrayList of ArrayLists (in waiting job)
ArrayList<ArrayList<Integer> > process = new ArrayList<ArrayList<Integer> >();
process.add(new ArrayList<Integer>(Arrays.asList(0,2030,2,4)));
process.add(new ArrayList<Integer>(Arrays.asList(0,5700,5,1)));
process.add(new ArrayList<Integer>(Arrays.asList(0,4190,4,2)));
process.add(new ArrayList<Integer>(Arrays.asList(0,3290,8,3)));
process.add(new ArrayList<Integer>(Arrays.asList(0,2550,2,5)));
process.add(new ArrayList<Integer>(Arrays.asList(0,6990,6,6)));
process.add(new ArrayList<Integer>(Arrays.asList(0,8940,8,7)));
process.add(new ArrayList<Integer>(Arrays.asList(0,740,10,8)));
process.add(new ArrayList<Integer>(Arrays.asList(0,3930,7,9)));
process.add(new ArrayList<Integer>(Arrays.asList(0,6890,6,10)));
process.add(new ArrayList<Integer>(Arrays.asList(0,6580,5,11)));
process.add(new ArrayList<Integer>(Arrays.asList(0,3820,8,12)));
process.add(new ArrayList<Integer>(Arrays.asList(0,9140,9,13)));
process.add(new ArrayList<Integer>(Arrays.asList(0,420,10,14)));
process.add(new ArrayList<Integer>(Arrays.asList(0,220,10,15)));
process.add(new ArrayList<Integer>(Arrays.asList(0,7540,7,16)));
process.add(new ArrayList<Integer>(Arrays.asList(0,3210,3,17)));
process.add(new ArrayList<Integer>(Arrays.asList(0,1380,1,18)));
process.add(new ArrayList<Integer>(Arrays.asList(0,9850,9,19)));
process.add(new ArrayList<Integer>(Arrays.asList(0,3610,3,20)));
process.add(new ArrayList<Integer>(Arrays.asList(0,7540,7,21)));
process.add(new ArrayList<Integer>(Arrays.asList(0,2710,2,22)));
process.add(new ArrayList<Integer>(Arrays.asList(0,8390,8,23)));
process.add(new ArrayList<Integer>(Arrays.asList(0,5950,5,24)));
process.add(new ArrayList<Integer>(Arrays.asList(0,760,10,25)));
//Queue (already in CPU)
ArrayList<ArrayList<Integer> > queue = new ArrayList<ArrayList<Integer> >();
//Usage of max Collection for Worst Fit purpose
int max=Collections.max(partition);
System.out.println("Max Partition : "+max);
//Process Part Assigned
int block=0;
int mem=1;
int cpuBurst=2;
int noJob=3;
int t=1;
int tPut=0;
do{
System.out.println("\n---------------------Time "+(t++)+"---------------------\n");
//Reduce CPU cpuBurst
for(int count=0;count<queue.size();){
//reduce time by one second
int redTime=queue.get(count).get(cpuBurst)-1;
queue.get(count).set(cpuBurst,redTime);
//if cpuBurst ==0 remove the process from queue
if(redTime==0){
int recover=queue.get(count).get(block);
int plusMem=queue.get(count).get(mem);
queue.remove(count);
int newMem=partition.get(recover)*-1;
partition.set(recover,newMem);
//System.out.println("This means it will open another partition for available space\n");
//System.out.println("Partition : "+partition+" Status : "+status+"\n");
status.set(recover,pass);
tPut++;
}
else{
//stay put the process in queue
count++;
}
}
//Check for maximum partition
for(int i=0;i<process.size();){
max=Collections.max(partition);
int maxIndex=partition.indexOf(max);
if(process.get(i).get(mem)<=partition.get(maxIndex)&&status.get(maxIndex)==pass){
process.get(i).set(block,maxIndex);
status.set(maxIndex,stop);
queue.add(process.get(i));
process.remove(i);
int newVal=partition.get(maxIndex)*-1;
partition.set(maxIndex,newVal);
}
else{
i++;
}
}
int iFragment=0;
System.out.println(" ");
System.out.println("Arranged According to Block");
System.out.println("Job\tTime\tSize\t\n");
for(int i=1;i<partition.size();i++){
//System.out.print(partition.get(i)+"\t"+i+"\t");
for(int j=0;j<queue.size();j++){
if(queue.get(j).get(block)==i){
System.out.print(queue.get(j).get(noJob)+"\t"+
queue.get(j).get(cpuBurst)+"\t"+
queue.get(j).get(mem)+"\t"/*+
queue.get(j).get(block)*/);
iFragment+=partition.get(i)+queue.get(j).get(mem);
break;
}
}
System.out.println(" ");
}
iFragment=iFragment*-1;
int hole=totalMemory-iFragment;
/*
System.out.println(" ");
System.out.println("Arranged According to Process(Already in Queue)");
System.out.println("No. Job\tTime\tSize\tBlock Assigned\n");
for(int i=0;i<queue.size();i++){
System.out.println(queue.get(i).get(noJob)+"\t"+
queue.get(i).get(cpuBurst)+"\t"+
queue.get(i).get(mem)+"\t"+
queue.get(i).get(block));
}
*/
//Process (in waiting)== Not Allocated Yet
int waitTime=0;
System.out.println(" ");
System.out.println("Arranged According to Process(Not Allocated Yet)");
System.out.println("No. Job\tTime\tSize ---> Not Allocated Yet");
for(int i=0;i<process.size();i++){
System.out.println(process.get(i).get(noJob)+"\t"+
process.get(i).get(cpuBurst)+"\t"+
process.get(i).get(mem));
waitTime+=process.get(i).get(cpuBurst);
}
System.out.println("\n\n------------------------------------------------\n");
//System Status Detail
System.out.println("\n\nSystem Status\n");
System.out.println("Throughput : "+tPut+"\n");
System.out.println("Storage Utilization");
int usedPercent=(queue.size()*100)/(partition.size()-1);
System.out.println("\t*Used Partition : "+usedPercent+"%");
int unusedPercent=100-usedPercent;
System.out.println("\t*Unused Partition : "+unusedPercent+"%\n");
System.out.println("Waiting Queue [JOBS] : "+process.size());
System.out.println("Waiting Queue [TIME] : "+ waitTime+" \n");
System.out.println("Internal Fragmentation(KB) : "+iFragment+"\n");
System.out.println("Hole :"+hole+"\n");
}while(queue.size()>0);
}
}
//Faliq and Am SCSR OS Class 19/20 --> The Great Lockdown