Question

In: Computer Science

using Java, Implement the following algorithm: - Accept 5 different memory partitions and 4 different processes...

using Java, Implement the following algorithm:
- Accept 5 different memory partitions and 4 different processes from user and show how best fit algorithm allocates them.

Solutions

Expert Solution

Here is the solution to your question. I tried my best to solve your doubt, however, if you find it is not as good as expected by you. Please do write your further doubts regarding this question in the comment section, I will try to resolve your doubts regarding the submitted solution as soon as possible.

Please give proper indentation as shown in the screenshot

If you think, the solution provided by me is helpful to you please do an upvote.

import java.util.Scanner;

public class Main
{
   // Method to allocate memory to blocks as per Best fit
   // algorithm
   static void bestFit(int blockSize[], int m, int processSize[], int n)
   {
       // Stores block id of the block allocated to a
       // processSize
       int allocation[] = new int[n];
  
       // Initially no block is assigned to any processSize
       for (int i = 0; i < allocation.length; i++)
           allocation[i] = -1;
  
   // pick each processSize and find suitable blocks
       // according to its size ad assign to it
       for (int i=0; i<n; i++)
       {
           // Find the best fit block for current processSize
           int bestIdx = -1;
           for (int j=0; j<m; j++)
           {
               if (blockSize[j] >= processSize[i])
               {
                   if (bestIdx == -1)
                       bestIdx = j;
                   else if (blockSize[bestIdx] > blockSize[j])
                       bestIdx = j;
               }
           }
  
           // If we could find a block for current processSize
           if (bestIdx != -1)
           {
               // allocate block j to p[i] processSize
               allocation[i] = bestIdx;
  
               // Reduce available memory in this block.
               blockSize[bestIdx] -= processSize[i];
           }
       }
  
       System.out.println("\nProcess\tProcess Size\tBlock no.");
       for (int i = 0; i < n; i++)
       {
           System.out.print("\t" + (i+1) + "\t\t" + processSize[i] + "\t\t\t\t");
           if (allocation[i] != -1)
               System.out.print(allocation[i] + 1);
           else
               System.out.print("Not Allocated");
           System.out.println();
       }
   }
  
  
   public static void main(String[] args)
   {
Scanner sc=new Scanner(System.in);
int[] blockSize=new int[5];
int[] processSize=new int[4];
      
       int m = 5;
       int n = 4;
System.out.println("Partition: ");
for(int i=0;i<m;i+=1)
{
System.out.print("Enter Block size of Block "+(i+1)+": ");
blockSize[i]=sc.nextInt();
}
System.out.println("Process: ");
for(int i=0;i<n;i+=1)
{
System.out.print("Enter Process Size of Process "+(i+1)+": ");
processSize[i]=sc.nextInt();
}
      
       bestFit(blockSize, m, processSize, n);

sc.close();
   }
}


Related Solutions

Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) Requirements Choose one problem with an algorithm and implement it. You should show and explain the result whatever you got. I recommend using N-Queen problem (at least N=8 or more) or any simple perfect games. For example, - N-Queen problem with hill climbing - N-Queen problem with simulated annealing - N-Queen problem with genetic algorithm - Tic-Tac-Toe with Minimax
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) N-Queen problem with genetic algorithm Please use the N-Queen problem (at least N=8 or more) or any simple perfect games. Please provide a screenshot of output and please heavily comment the code. Thanks!
Write a program using c, c++, or java that have four dynamic memory partitions of size...
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).
Please write a Java algorithm solving the following problem: Implement a Java method to check if...
Please write a Java algorithm solving the following problem: Implement a Java method to check if a binary tree is balanced. For this assignment, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one. 1. First, please create the following two classes supporting the Binary Tree Node and the Binary Tree: public class BinTreeNode<T> { private T key; private Object satelliteData; private BinTreeNode<T> parent;...
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array...
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array of integers into ascending order. Consider the given array as input and produce a sorted array as output. Each time you take an integer from the input array, place it at the end of the output array. If the result is unsorted, backtrack.
Based on the scenario above, implement the Huffman coding algorithm using Java NetBeans. Assume the characters...
Based on the scenario above, implement the Huffman coding algorithm using Java NetBeans. Assume the characters and frequencies as listed below. The total number of nodes is n = 6.
how to accept string from shared memory using c language ?
how to accept string from shared memory using c language ?
Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting...
Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting .Arrival time, burst time and priority values.The code should also display: (i) Gantt chart and determine the following: (ii) Determine the Turnaround time(TAT), waiting time(WT) of each process (iii) Determine the Average Waiting Time (AWT) and Average Turnaround Time (ATAT) of all processes. please write the comments
In this activity, we are going to implement the previous algorithm in Java. Just in case,...
In this activity, we are going to implement the previous algorithm in Java. Just in case, here is problem again: This program calculates and displays a person's body mass index (BMI). The BMI is calculated as the weight times 703 divided by the height squared where the weight is measured in pounds and the height in inches. The program should display a message indicating whether the person has optimal weight (BMI between 18.5 and 25), is underweight (BMI is less...
4 Implement a Java program that meets the following requirements • You can use the Java...
4 Implement a Java program that meets the following requirements • You can use the Java standard sequence data structure API types for sets, lists, stack,queue and priority queue as needed. All are available in the java.util package, which you will want to import in your program. 1. Argue in code comments which data structure, stack or queue, you will use to implement this method. Implement a method which creates some String objects as food orders for a small restaurant,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT