-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMemoryManagment.java
More file actions
162 lines (160 loc) · 4.73 KB
/
MemoryManagment.java
File metadata and controls
162 lines (160 loc) · 4.73 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
import java.util.*;
class Block {
int totalSize; // Total size of the memory block
int freeSpace; // Remaining free space in the block
List<Integer> pIds; // List of process IDs allocated to this block
boolean isFree; // Availability status of the block (True if available)
Block(int size) {
this.totalSize = size;
this.freeSpace = size;
this.pIds = new ArrayList<>();
this.isFree = new Random().nextInt(3) != 0; // 1 in 3 chance for block to be unavailable
}
void allocate(int pId, int size) {
pIds.add(pId);
freeSpace -= size; // Decrease free space after allocation
}
boolean canFit(int size) {
return freeSpace >= size && isFree; // Check if the block can fit the process
}
boolean isEmpty() {
return pIds.isEmpty(); // Check if the block is empty
}
}
public class MemoryManagment {
private static List<Block> blocks = new ArrayList<>();
private static int pIdCounter = 1; // Process ID counter
private static Random rand = new Random();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Step 1: Get the number of memory blocks
System.out.print("Enter number of blocks: ");
int numBlocks = sc.nextInt();
// Step 2: Create blocks with random sizes
for (int i = 0; i < numBlocks; i++) {
int size = rand.nextInt(150) + 50; // Random block size between 300 and 500
blocks.add(new Block(size));
}
System.out.println("\nInitial Memory Blocks:");
displayBlocks(); // Display memory blocks
while (true) {
System.out.println("\nMemory Allocation Strategies:");
System.out.println("1. First Fit");
System.out.println("2. Best Fit");
System.out.println("3. Worst Fit");
System.out.println("4. Display Blocks");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
allocateFirstFit(sc);
break;
case 2:
allocateBestFit(sc);
break;
case 3:
allocateWorstFit(sc);
break;
case 4:
displayBlocks();
break;
case 5:
System.out.println("Exiting...");
sc.close();
return;
default:
System.out.println("Invalid choice. Try again.");
}
}
}
// First Fit Allocation
private static void allocateFirstFit(Scanner sc) {
System.out.print("Enter number of processes: ");
int numProcesses = sc.nextInt();
for (int i = 0; i < numProcesses; i++) {
int pSize = rand.nextInt(101) + 50; // Random process size between 50 and 150
System.out.println("Allocating Process " + pIdCounter + " of size " + pSize);
boolean allocated = false;
for (Block b : blocks) {
if (b.canFit(pSize)) {
b.allocate(pIdCounter++, pSize);
allocated = true;
System.out.println("Process " + (pIdCounter - 1) + " allocated.");
break;
}
}
if (!allocated) {
System.out.println("No suitable block for Process " + (pIdCounter - 1));
}
}
displayBlocks(); // Show updated blocks
}
// Best Fit Allocation
private static void allocateBestFit(Scanner sc) {
System.out.print("Enter number of processes: ");
int numProcesses = sc.nextInt();
for (int i = 0; i < numProcesses; i++) {
int pSize = rand.nextInt(101) + 50; // Random process size between 50 and 150
System.out.println("Allocating Process " + pIdCounter + " of size " + pSize);
Block bestBlock = null;
for (Block b : blocks) {
if (b.canFit(pSize)) {
if (bestBlock == null || b.freeSpace < bestBlock.freeSpace) {
bestBlock = b;
}
}
}
if (bestBlock != null) {
bestBlock.allocate(pIdCounter++, pSize);
System.out.println("Process " + (pIdCounter - 1) + " allocated.");
} else {
System.out.println("No suitable block for Process " + (pIdCounter - 1));
}
}
displayBlocks(); // Show updated blocks
}
// Worst Fit Allocation
private static void allocateWorstFit(Scanner sc) {
System.out.print("Enter number of processes: ");
int numProcesses = sc.nextInt();
for (int i = 0; i < numProcesses; i++) {
int pSize = rand.nextInt(101) + 50; // Random process size between 50 and 150
System.out.println("Allocating Process " + pIdCounter + " of size " + pSize);
Block worstBlock = null;
for (Block b : blocks) {
if (b.canFit(pSize)) {
if (worstBlock == null || b.freeSpace > worstBlock.freeSpace) {
worstBlock = b;
}
}
}
if (worstBlock != null) {
worstBlock.allocate(pIdCounter++, pSize);
System.out.println("Process " + (pIdCounter - 1) + " allocated.");
} else {
System.out.println("No suitable block for Process " + (pIdCounter));
}
}
displayBlocks(); // Show updated blocks
}
// Display current memory blocks
private static void displayBlocks() {
System.out.println("\nMemory Blocks (With Status):");
for (Block b : blocks) {
System.out.print("| ");
if (b.isEmpty()) {
System.out.print("Free ");
} else {
for (int pId : b.pIds) {
System.out.print("P" + pId + " ");
}
}
System.out.print("(Total: " + b.totalSize + ", Free: " + b.freeSpace + ", Available: " + b.isFree + ") | ");
}
System.out.println();
// Calculate total free space
int totalFree = blocks.stream().mapToInt(block -> block.freeSpace).sum();
System.out.println("Total Free Space: " + totalFree);
}
}