-
Notifications
You must be signed in to change notification settings - Fork 0
/
nextfit.java
72 lines (61 loc) · 2.32 KB
/
nextfit.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
// Java program for next fit
// memory management algorithm
import java.util.*;
public class nextfit {
// Function to allocate memory to blocks as per Next fit
// algorithm
static void NextFit(int blockSize[], int m, int processSize[], int n) {
// Stores block id of the block allocated to a
// process
int allocation[] = new int[n], j = 0, t = m - 1;
// Initially no block is assigned to any process
Arrays.fill(allocation, -1);
// pick each process and find suitable blocks
// according to its size ad assign to it
// pick each process and find sui table blocks
// according to its size ad assign to it
for(int i = 0; i < n; i++){
// Do not start from beginning
while (j < m){
if(blockSize[j] >= processSize[i]){
// allocate block j to p[i] process
allocation[i] = j;
// Reduce available memory in this block.
blockSize[j] -= processSize[i];
// sets a new end point
t = (j - 1) % m;
break;
}
if (t == j){
// sets a new end point
t = (j - 1) % m;
// breaks the loop after going through all memory block
break;
}
// mod m will help in traversing the
// blocks from starting block after
// we reach the end.
j = (j + 1) % m;
}
}
System.out.print("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < n; i++) {
System.out.print( i + 1 + "\t\t" + processSize[i]
+ "\t\t");
if (allocation[i] != -1) {
System.out.print(allocation[i] + 1);
} else {
System.out.print("Not Allocated");
}
System.out.println("");
}
}
// Driver program
public static void main(String[] args) {
int blockSize[] = {5, 10, 20};
int processSize[] = {10, 20, 5};
int m = blockSize.length;
int n = processSize.length;
NextFit(blockSize, m, processSize, n);
}
}