In: Computer Science
Write a program using c, c++, or java that have four dynamic memory partitions of size 100 KB, 500 KB, 200 KB, and 450 KB. The program should accept from user the number of processes and their sizes. Then output the assignment of processes using the next fit algorithm (specifying which process, if any, is block).
source code:
import java.util.Arrays;
public class Main
{
// 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;
// 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
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];
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
static public void main(String[] args) {
int blockSize[] = {100, 500, 200,300};
int processSize[] = {100, 250, 300};
int m = blockSize.length;
int n = processSize.length;
NextFit(blockSize, m, processSize, n);
}
}