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

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).
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!
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.
4   Write an algorithm to settle the following questions: Accept a starting balance (For example :A...
4   Write an algorithm to settle the following questions: Accept a starting balance (For example :A bank account starts out with $10,000) Accept an Interest rate (For example: 6 percent per year is 0.005 percent per month). Accept an amount to withdraw every month (For example: Every month, $500 is withdrawn to meet college expenses.) After how many years is the account depleted? (You are to loop and find out how many months this will take) (For example: Suppose the...
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 ?
Question 5 The following processes are being scheduled using a preemptive, priority-based, round-robin scheduling algorithm. Each...
Question 5 The following processes are being scheduled using a preemptive, priority-based, round-robin scheduling algorithm. Each process is assigned a numerical priority,with a higher number indicating a higher relative priority. The scheduler will execute the highest-priority process. For processes with the same priority, a round-robin scheduler will be used with a time quantum of 10 units. If a process is preempted by a higher-priority process, the preempted process is placed at the end of the queue. Process          Burst Time      Arrival Time   Priority P1                15            0            8...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT