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
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.
Implement the Banker's algorithm for deadlock avoidance, that works on a given set of N processes...
Implement the Banker's algorithm for deadlock avoidance, that works on a given set of N processes and M resource types (N<10,M<10). Use C/C++/C# or Java for the implementation, with a simple text interface, where the user enters only the name of the input file (text only). The program reads all the necessary input data from that file. The input data and result is then displayed on the screen. You may use your program to validate the example you gave in...
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...
IN JAVA: Implement an algorithm to check whether a LinkedList is a palindrome. 2 methods should...
IN JAVA: Implement an algorithm to check whether a LinkedList is a palindrome. 2 methods should be implemented: Constant space, i.e. without creating extra copies of the linked list Unrestricted space q1: / For this implementation you should use constant space   // Note: you are free to add any extra methods,   // but this method has to be used   public static boolean isPalindromeRestricted(ListNode head) {     // Your code goes here     return false;   } q2: // For this implementation the space...
Implement a C++ program to implement the Banker’s algorithm for deadlock avoidance. Number of process 5,...
Implement a C++ program to implement the Banker’s algorithm for deadlock avoidance. Number of process 5, number of resources 3 and the number of instances of each given resource is in available. You should complete the functionalities for safe state check and resource request processing. To Do 1. Complete the definition of isSafe function. The function take, the process array, 1D array of available resources, 2D array storing current allocation, and 2D array of current need. The function does not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT