Question
Please fix my java code. The program to simulate the memory allocation using First Fit algorithm. Thank you very much. import java.util.*; public class Memory{
Please fix my java code. The program to simulate the memory allocation using First Fit algorithm. Thank you very much.
import java.util.*;
public class Memory{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int fixedMemoryX, dynamicMemoryY;
System.out.println("Enter available memory for fixed partitioning scheme:");
fixedMemoryX=sc.nextInt();
System.out.println("Enter available memory for dynamic partitioning scheme:");
dynamicMemoryY=sc.nextInt();
System.out.println("Enter starting address Z:");
int startingAddressZ = sc.nextInt();
System.out.println("Enter Fixed partitioning size M (partition size):");
int partitionSize = sc.nextInt();
System.out.println("Enter number of jobs:");
int jobs=sc.nextInt();
int jobsMemory[] = new int[jobs];
for(int i = 0; i < jobs; i++){
System.out.println("Enter memory requirement for job [" + i + "]:");
jobsMemory[i] = sc.nextInt();
}
fixedPartitioning(fixedMemoryX, startingAddressZ, partitionSize, jobsMemory);
dynamicPartitioning(dynamicMemoryY, startingAddressZ, jobsMemory);
sc.close();
}
static void fixedPartitioning(int memorySize, int startingAddressZ, int partitionSize, int jobs[]){
int noOfBlocks = memorySize / partitionSize;
int fixedPartitions[] = new int[noOfBlocks];
for(int b = 0; b < noOfBlocks; b++)
fixedPartitions[b] = 0; // 0= not used; > 0 = used
for(int i = 0; i < jobs.length; i++){
int size = jobs[i];
int start = -1;
for(int j = 0; j < fixedPartitions.length; j++){
//calculate where the job will fit
if(fixedPartitions[j] == 0){
//empty partition, let's see if the job will fit
if (start == -1) start = j; //mark the position
size = size - partitionSize;
if (size > 0 && fixedPartitions[j+1] !=0) {
//job does not fit in this memory location
start = -1; //reset start
size = jobs[i]; //reset size
}
else if (size <= 0) {
last; //end the j loop
}
}
{
if (size > 0) printf ("job " + i + " does not fit ");
else {
printf ("job " + i + "enters at mem location " + start + " ");
while (job[i] > 0) {
fixedPartions[start] = jobsTime[i];
//time taken to use this mem. this partition is now used
jobs[i] = job[i] - partitionSize; //when job[x] is <=0, known job is done
start++;}
}
}
}
}
System.out.println("Job "+ i + " has been placed into memory block " + b);
fixedPartitions[b] = fixedPartitions[b] - jobs[i];
memorySize = memorySize - jobs[i];
System.out.println("Available Memory:" + memorySize);
break;
}
static void dynamicPartitioning(int memorySize, int startingAddressZ, int jobs[]){
int i = 0;
do {
int mp=jobs[i];
if(mp > memorySize) {
System.out.println("Not enough memory for Job " + i);
}
else {
System.out.println("Allocated memory to Job " + i);
memorySize = memorySize-mp;
}
i++;
}
while( i < jobs.length);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started