Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please i need some one to complete my code i cant find a solution on the compactMemory() method and the reportMemory() method import java.util.*; public

please i need some one to complete my code i cant find a solution on the compactMemory() method and the reportMemory() method

import java.util.*;

public class MemoryManagement { static int MAX; static int[] addresses; public MemoryManagement(int MAX) { this.MAX = MAX; this.addresses = new int[MAX]; } public static void requestMemory(String process, int size, String flag) { int index = -1; int remainingSize = -1; switch (flag) { case "F": for (int i = 0; i < MAX; i++) { if (addresses[i] == 0) { // Found a free block of memory int j = i; while (j < MAX && addresses[j] == 0 && j - i + 1 < size) { // Look for a contiguous block of free memory j++; } if (j - i + 1 == size) { // Found a contiguous block of free memory of the required size index = i; remainingSize = j - i + 1 - size; break; } i = j; } } break; case "B": // Best-fit approach index = -1; int bestFit = MAX + 1; for (int i = 0; i < MAX; i++) { if (addresses[i] == 0) { int j = i; while (j < MAX && addresses[j] == 0 && j - i + 1 < size) { j++; } if (j - i + 1 >= size && j - i + 1 < bestFit) { bestFit = j - i + 1; index = i; remainingSize = j - i + 1 - size; } i = j; } } break; case "W": // Worst-fit approach index = -1; int worstFit = -1; for (int i = 0; i < MAX; i++) { if (addresses[i] == 0) { int j = i; while (j < MAX && addresses[j] == 0 && j - i + 1 < size) { j++; } if (j - i + 1 >= size && j - i + 1 > worstFit) { worstFit = j - i + 1; index = i; remainingSize = j - i + 1 - size; } i = j; } } break; } if (index != -1) { // Found a suitable block of memory for (int i = index; i < index + size; i++) { addresses[i] = 1; } if (remainingSize > 0) { // Create a new hole for (int i = index + size; i < index + size + remainingSize; i++) { addresses[i] = 0; } } System.out.printf("Memory allocated to process %s: [%d, %d]%n", process, index, index + size - 1); } else { // Failed to find a suitable block of memory System.out.printf("Memory allocation failed for process %s%n", process); } } public static void releaseMemory(String process) { for (int i = 0; i < MAX; i++) { if (addresses[i] == 1) { // This region is allocated to some process int j = i; while (j < MAX && addresses[j] == 1) { // Look for the end of the allocated region j++; } if (process.equals("ALL") || new String(addresses, i, j - i).trim().equals(process)) { // Found a region allocated to the specified process (or all processes) for (int k = i; k < j; k++) { addresses[k] = 0; } i = j; // Skip the freed region } else { i = j - 1; // Skip the region allocated to another process } } } System.out.printf("Memory released from process %s%n", process); } public static void compactMemory() {

// help

}

public static void reportMemory() {

// report the regions of free and allocated memory

} public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the inital size: "); int size = scanner.nextInt(); scanner.nextLine(); MemoryManagement memoryManagement = new MemoryManagement(size);

while (true) { System.out.print("allocator>"); String input = scanner.nextLine(); String processID = ""; String[] command = input.split(" "); switch (command[0]) { case "RQ": requestMemory(command[1], Integer.parseInt(command[2]), command[3]); break; case "RL": releaseMemory(command[1]); break; case "C": compactMemory(); break; case "STAT": reportMemory(); break; case "X": System.exit(0); break; default: System.out.println("Invalid command"); } } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Oracle9i Database Administration

Authors: Sam R. Alapati

1st Edition

1590590228, 978-1590590225

More Books

Students also viewed these Databases questions

Question

What do you think Katsoudas means by the phrase one size fits one?

Answered: 1 week ago

Question

How do you think GM should handle this decision and why?

Answered: 1 week ago