Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

worst fit and best fir dosent work please help me public static void requestMemory(String process, int size, String flag) { int index = -1; int

worst fit and best fir dosent work please help me 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] = Integer.parseInt(process.substring(1)) + 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); } }

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

Oracle Database 11g SQL

Authors: Jason Price

1st Edition

0071498508, 978-0071498500

More Books

Students also viewed these Databases questions