- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryModule.java
More file actions
Latest commit
194 lines (184 loc) · 7.06 KB
/
Copy pathMemoryModule.java
File metadata and controls
194 lines (184 loc) · 7.06 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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
packagememorymodule;
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.Collections;
importjava.util.List;
importjava.util.Scanner;
publicclassMemoryModule {
/**
* @param args the command line arguments
*/
privatestaticfinalintSIZE = 1024;
privatestaticfinalString[] Memory = newString[SIZE];
staticvoidBestFit(Integer[] process, Integer[] holes) {
intbestIdx = -1;
for (intj = 0; j < holes.length; j++) {
if (process[1] <= holes[j]) {
if (bestIdx == -1) {
bestIdx = j;
} elseif (holes[j] < holes[bestIdx]) {
bestIdx = j;
}
}
}
if (bestIdx == -1) {
System.err.println("\n** P" + process[0] + " can't be allocated ** Not enough space in Memory **");
return;
}
intj = 0, idx = 0;
booleanflag = false;
for (intk = 0; k < SIZE && j < process[1]; k++) {
if (Memory[k] == null) {
if (idx == bestIdx) {
Memory[k] = "P" + process[0];
j++;
}
flag = true;
} else {
if (flag) {
++idx;
flag = false;
}
}
}
}
privatestaticvoidaddProcess(List<Integer[]> Process) {
Integer[][] Processes = Process.toArray(Integer[][]::new);
Arrays.sort(Processes, (Integer[] c1, Integer[] c2) -> {
if (c1[2] > c2[2]) {
return1;
} else {
return -1;
}
});
for (Integer[] process : Processes) {
intj = 0;
booleanflag = false;
List<Integer> holes = newArrayList<>();
for (intk = 0; k < SIZE; k++) {
if (Memory[k] == null) {
j++;
flag = true;
} else {
if (flag) {
holes.add(j);
flag = false;
j = 0;
}
}
}
if (flag) {
holes.add(j);
}
BestFit(process, holes.toArray(Integer[]::new));
}
}
privatestaticvoidremoveProcess(StringR) {
booleanflag = false;
for (intk = 0; k < SIZE; k++) {
if (Memory[k] == null ? R == null : Memory[k].equals(R)) {
Memory[k] = null;
flag = true;
}
}
if (!flag) {
System.err.println("** No such process exist in memory! **");
}
}
privatestaticvoidprint() throwsInterruptedException {
for (intk = 0; k < Memory.length - 1; k++) {
if (k == 0) {
System.out.println("\n" + k + " +------------+");
System.out.println(" \t " + Memory[0]);
}
if (Memory[k] == null ? Memory[k + 1] != null : !Memory[k].equals(Memory[k + 1])) {
System.out.println(k + " +------------+");
System.out.println(" \t " + Memory[k + 1]);
}
}
System.out.println(Memory.length - 1 + " +------------+\n");
intfreePartitions = 0;
List<Integer> allocatedPartitions = newArrayList<>();
boolean[] visited = newboolean[SIZE];
for (intk = 0; k < SIZE; k++) {
if (visited[k] == true) {
continue;
}
intcount = 1;
for (intj = k + 1; j < SIZE; j++) {
if (Memory[k] == null ? Memory[j] == null : Memory[k].equals(Memory[j])) {
visited[j] = true;
count++;
}
}
if (Memory[k] == null) {
freePartitions = count;
} else {
allocatedPartitions.add(count);
}
}
Thread.sleep(1000);
intsum = allocatedPartitions.stream().mapToInt(val -> val).sum();
intavg = allocatedPartitions.isEmpty() ? 0 : sum / allocatedPartitions.size();
intmax = allocatedPartitions.isEmpty() ? 0 : Collections.max(allocatedPartitions);
intmin = allocatedPartitions.isEmpty() ? 0 : Collections.min(allocatedPartitions);
System.out.println("\n ************************* \n");
System.out.println("Total no. of processes in Memory: " + allocatedPartitions.size()+"\n");
System.out.println("Average size of processes in Memory: " + avg+"\n");
System.out.println("Max size of processes in Memory: " + max+"\n");
System.out.println("Min size of processes in Memory: " + min+"\n");
System.out.println("Total amount of available Memory: " + freePartitions+"\n");
System.out.println("\n ************************* ");
newScanner(System.in).nextLine();
}
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
inti = 0;
intopt = 0;
System.out.println("\n\t***** MEMORY MODULE *****\n");
do {
System.out.println("\t1) ENTER A NEW PROCESS\n\n\t2) REMOVE A PROCESS\n\n\t3) SEE CURRENT STATE OF MEMORY\n\n\t4) EXIT");
do {
System.out.print("\nEnter your choice: ");
opt = sc.nextInt();
} while (opt < 1 || opt > 4);
switch (opt) {
case1:
System.out.println("\n ***TO STOP, ENTER ANY CHARACTER***\n");
System.out.println("\tSize Arrival Time");
List<Integer[]> Process = newArrayList<>();
while (true) {
try {
System.out.print("P" + (i + 1) + "\t");
Process.add(newInteger[]{++i, sc.nextInt(), sc.nextInt()});
} catch (Exceptione) {
sc.next();
break;
}
}
addProcess(Process);
break;
case2:
System.out.print("\nEnter process name: ");
StringR = sc.next();
removeProcess(R);
break;
case3:
try {
print();
} catch (InterruptedExceptionex) {
System.out.println(ex);
}
break;
case4:
default:
break;
}
System.out.println();
} while (opt != 4);
}
}