-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathalloc.java
293 lines (258 loc) · 6.74 KB
/
alloc.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
Title: Memory Allocation in Operating Systems in Java
Description:
(a) First-Fit:
- Allocate the first hole that is big enough.
- Searching can start either at the beginning of the set of holes or where the previous first search ended.
- Stop searching as soon as we find a free hole that is large enough.
(b) Best-Fit:
- Allocate the smallest hole that is big enough.
- Search the entire list unless the list is ordered by size.
(c) Worst-Fit:
- Allocate the largest hole.
- Search the entire list unless the list is ordered by size.
*/
import java.util.ArrayList;
import java.util.Scanner;
public class memory1 {
public void first()
{
System.out.println("ENTER THE NUMBER OF PROCESSES");
int size = s.nextInt();
System.out.println("ENTER THE SIZES OF PROCESSES");
int s1[] = new int[size];
for (int i = 0; i < size; i++) {
int a = s.nextInt();
s1[i] = a;
}
System.out.println("ENTER THE SIZE OF MEMORY BLOCK");
int size1 = s.nextInt();
System.out.println("ENTER THE MEMORY PARTITIONS");
int s2[] = new int[size1];
for (int i = 0; i < size1; i++) {
int a1 = s.nextInt();
s2[i] = a1;
}
int allocation[] = new int[size];
for (int i = 0; i < allocation.length; i++) //initialize to -1
{
allocation[i] = -1;
}
for (int i = 0; i < size; i++) {
for (int j = 0; j < size1; j++) {
if (s2[j] >= s1[i]) {
allocation[i] = j;
s2[j] -= s1[i];
break;
}
}
}
System.out.println("\nProcess No.\t\tProcess Size\t\tBlock no.");
for (int i = 0; i < size; i++) {
System.out.print(" " + (i + 1) + "\t\t\t" + s1[i] + "\t\t\t");
if (allocation[i] != -1) System.out.print(allocation[i] + 1);
else System.out.print("Not Allocated");
System.out.println();
}
}
public void best()
{
System.out.println("ENTER THE NUMBER OF PROCESSES");
size = s.nextInt();
System.out.println("ENTER THE SIZES OF PROCESSES");
int s11[] = new int[size];
for (int i = 0; i < size; i++) {
int a = s.nextInt();
s11[i] = a;
}
System.out.println("ENTER THE SIZE OF MEMORY BLOCK");
size1 = s.nextInt();
System.out.println("ENTER THE MEMORY PARTITIONS");
int s21[] = new int[size1];
for (int i = 0; i < size1; i++) {
int a1 = s.nextInt();
s21[i] = a1;
}
int allocation1[] = new int[size];
for (int i = 0; i < allocation1.length; i++) {
allocation1[i] = -1;
}
for (int i = 0; i < size; i++) {
int best = -1;
for (int j = 0; j < size1; j++) {
if (s21[j] >= s11[i]) {
if (best == -1) best = j;
else if (s21[best] > s21[j]) best = j;
}
}
if (best != -1) {
allocation1[i] = best;
s21[best] -= s11[i];
}
}
System.out.println("\nProcess No.\t\tProcess Size\t\tBlock no.");
for (int i = 0; i < size; i++) {
System.out.print(" " + (i + 1) + "\t\t\t" + s11[i] + "\t\t\t");
if (allocation1[i] != -1) System.out.print(allocation1[i] + 1);
else System.out.print("Not Allocated");
System.out.println();
}
}
public void worst(){
System.out.println("ENTER THE NUMBER OF PROCESSES");
size = s.nextInt();
System.out.println("ENTER THE SIZES OF PROCESSES");
int s111[] = new int[size];
for (int i = 0; i < size; i++) {
int a = s.nextInt();
s111[i] = a;
}
System.out.println("ENTER THE SIZE OF MEMORY BLOCK");
size1 = s.nextInt();
System.out.println("ENTER THE MEMORY PARTITIONS");
int s211[] = new int[size1];
for (int i = 0; i < size1; i++) {
int a1 = s.nextInt();
s211[i] = a1;
}
int allocation11[] = new int[size];
for (int i = 0; i < allocation11.length; i++) {
allocation11[i] = -1;
}
for (int i = 0; i < size; i++) {
int wst = -1;
for (int j = 0; j < size1; j++) {
if (s211[j] >= s111[i]) {
if (wst == -1) wst = j;
else if (s211[wst] < s211[j]) wst = j;
}
}
if (wst != -1) {
allocation11[i] = wst;
s211[wst] -= s111[i];
}
}
System.out.println("\nProcess No.\t\tProcess Size\t\tBlock no.");
for (int i = 0; i < size; i++) {
System.out.print(" " + (i + 1) + "\t\t\t" + s111[i] + "\t\t\t");
if (allocation11[i] != -1) System.out.print(allocation11[i] + 1);
else System.out.print("Not Allocated");
System.out.println();
}
}
}
public class alloc {
public static void main(String[] args) {
Scanner s = new Scanner(System. in );
int u = 0;
System.out.println("MEMORY ALLOCATION TECHNIQUES");
do {
memory1 c1=new memory1();
System.out.println("ENTER 1 FOR FIRST FIT");
System.out.println("ENTER 2 FOR BEST FIT");
System.out.println("ENTER 3 FOR WORST FIT");
System.out.println("PRESS O FOR EXIT ");
u = s.nextInt();
switch (u) {
case 1:
c1.first();
break;
case 2:
c1.best();
break;
case 3:
c1.worst();
break;
}
} while ( u != 0 );
}
}
/*
*MEMORY ALLOCATION TECHNIQUES
ENTER 1 FOR FIRST FIT
ENTER 2 FOR BEST FIT
ENTER 3 FOR WORST FIT
PRESS O FOR EXIT
1
ENTER THE NUMBER OF PROCESSES
5
ENTER THE SIZES OF PROCESSES
210
167
34
356
267
ENTER THE SIZE OF MEMORY BLOCK
5
ENTER THE MEMORY PARTITIONS
100
300
200
400
378
Process No. Process Size Block no.
1 210 2
2 167 3
3 34 1
4 356 4
5 267 5
ENTER 1 FOR FIRST FIT
ENTER 2 FOR BEST FIT
ENTER 3 FOR WORST FIT
PRESS O FOR EXIT
1
ENTER THE NUMBER OF PROCESSES
5
ENTER THE SIZES OF PROCESSES
200
13
678
456
789
ENTER THE SIZE OF MEMORY BLOCK
5
ENTER THE MEMORY PARTITIONS
100
500
200
346
135
Process No. Process Size Block no.
1 200 2
2 13 1
3 678 Not Allocated
4 456 Not Allocated
5 789 Not Allocated
ENTER 1 FOR FIRST FIT
ENTER 2 FOR BEST FIT
ENTER 3 FOR WORST FIT
PRESS O FOR EXIT
3
ENTER THE NUMBER OF PROCESSES
5
ENTER THE SIZES OF PROCESSES
100
300
478
124
245
ENTER THE SIZE OF MEMORY BLOCK
5
ENTER THE MEMORY PARTITIONS
100
500
300
245
357
Process No. Process Size Block no.
1 100 2
2 300 2
3 478 Not Allocated
4 124 5
5 245 3
ENTER 1 FOR FIRST FIT
ENTER 2 FOR BEST FIT
ENTER 3 FOR WORST FIT
PRESS O FOR EXIT
0
*/