0% found this document useful (0 votes)
20 views4 pages

LAB Assignment 9 Memory Allocation Java

The document outlines a lab assignment for students to implement memory management strategies in programming languages such as C, C++, or Java. Students are required to simulate First Fit, Best Fit, and Worst Fit memory allocation strategies, with a program flow that includes initializing memory, user interaction for allocation requests, and displaying memory states. The provided Java code serves as an example implementation of these strategies, allowing users to allocate memory blocks based on their choice of strategy.

Uploaded by

Vibhuti Sachdeva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

LAB Assignment 9 Memory Allocation Java

The document outlines a lab assignment for students to implement memory management strategies in programming languages such as C, C++, or Java. Students are required to simulate First Fit, Best Fit, and Worst Fit memory allocation strategies, with a program flow that includes initializing memory, user interaction for allocation requests, and displaying memory states. The provided Java code serves as an example implementation of these strategies, allowing users to allocate memory blocks based on their choice of strategy.

Uploaded by

Vibhuti Sachdeva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

LAB Assignment 9 - Operating Systems (ULC404)

Instructions: The instructor is required to discuss the concept of memory management with the

students.

Students have to implement the following program.

Write a program using C/C++/Java to simulate the First Fit, Best Fit, and Worst Fit memory

allocation strategies.

Assume memory chunks and initial requirements for memory blocks from your side.

Example Program Flow:

1. Initialize the program with an initial memory chunk of a specified size.

2. Display the menu for the user to choose an allocation strategy (First Fit, Best Fit, Worst Fit) or to

exit.

3. Prompt the user to request a memory block allocation, specifying the size.

4. Allocate memory based on the chosen strategy.

5. Display the updated state of the memory chunk.

6. Repeat steps 3 to 5 until the user chooses to exit.

import java.util.Scanner;

class Block {
int size;
boolean allocated;

Block(int size) {
this.size = size;
this.allocated = false;
}
}

public class MemoryAllocation {


public static void main(String[] args) {
Block[] memory = {
new Block(100),
new Block(500),
new Block(200),
new Block(300),
new Block(600)
};

Scanner sc = new Scanner(System.in);


while (true) {
System.out.println("\n1. First Fit\n2. Best Fit\n3. Worst Fit\n4. Exit");
System.out.print("Enter choice: ");
int choice = sc.nextInt();
if (choice == 4) break;

System.out.print("Enter memory size to allocate: ");


int reqSize = sc.nextInt();
int index = -1;

switch (choice) {
case 1:
for (int i = 0; i < memory.length; i++) {
if (!memory[i].allocated && memory[i].size >= reqSize) {
index = i;
break;
}
}
break;
case 2:
int minDiff = Integer.MAX_VALUE;
for (int i = 0; i < memory.length; i++) {
if (!memory[i].allocated && memory[i].size >= reqSize &&
memory[i].size - reqSize < minDiff) {
minDiff = memory[i].size - reqSize;
index = i;
}
}
break;
case 3:
int maxDiff = -1;
for (int i = 0; i < memory.length; i++) {
if (!memory[i].allocated && memory[i].size >= reqSize &&
memory[i].size - reqSize > maxDiff) {
maxDiff = memory[i].size - reqSize;
index = i;
}
}
break;
}

if (index != -1) {
memory[index].allocated = true;
System.out.println("Memory allocated at block " + (index + 1));
} else {
System.out.println("No suitable block found. Allocation failed.");
}
System.out.println("Memory Blocks:");
for (int i = 0; i < memory.length; i++) {
System.out.println("Block " + (i + 1) + ": Size = " + memory[i].size +
", " + (memory[i].allocated ? "Allocated" : "Free"));
}
}
sc.close();
}
}
Sample Output:

You might also like