Question

In: Computer Science

java 2D array / recursion explain every method You are requested to write a Java program...

java

2D array / recursion
explain every method

You are requested to write a Java program of a simple Memory Management Unit.
The program should allow the following:
1. The user can create a process asking for memory. The program will return a process ID if the requested memory can be allocated. It will also print the allocated Base and Limit.
2. The user can delete a process by specifying a process ID. The program should do that and free any allocated memory for the deleted process.
3. The user can, using a process ID, ask to convert between virtual addresses and physical addresses.
4. The user can ask the program to print the memory map showing what memory is allocated and to which process.
Your program should be named mmu. When you start your program it prompts the user for the amount of memory to be managed in KB.
Your program should display a prompt for the user and should accept the following 4 commands:
1. cr AMOUNT_OF_REQUESTED_MEMORY
Description: Create a process and allocate the requested amount of memory (in KB) to it. The command should return the process ID and the Base and Limit of the allocated memory. The command may return an error message if there is not enough memory.
e.g. cr 1500
2. dl PROCESS_ID
Description: Delete the specified process and free the allocated memory. The command should return an error message if there is no process with the specified ID.
e.g. dl 6
3. cv PROCESS_ID VIRTUAL_ADDRESS
Description: Make a conversion for the specified process ID from the specified Virtual Address to the Physical Address. The command should return an error message if the process tries to access an address outside its address space.
e.g. cv 6 200
4.   pm
Description: Print the memory map. The command should print which memory locations are assigned.
e.g. pm

output

Enter amount of memory to be managed
10
         
Enter:   cr AMOUNT_OF_REQUESTED_MEMORY
dl PROCESS_ID
cv PROCESS_ID VIRTUAL_ADDRESS
pm
Exit
cr 4
1 0 3
Enter:   cr AMOUNT_OF_REQUESTED_MEMORY
dl PROCESS_ID
cv PROCESS_ID VIRTUAL_ADDRESS
pm
Exit
cr 8
No memory available for the amount you requested
Enter:   cr AMOUNT_OF_REQUESTED_MEMORY
dl PROCESS_ID
cv PROCESS_ID VIRTUAL_ADDRESS
pm
Exit
cr 2
2 4 5
Enter:   cr AMOUNT_OF_REQUESTED_MEMORY
dl PROCESS_ID
cv PROCESS_ID VIRTUAL_ADDRESS
pm
Exit
pm
1111110000
Enter:   cr AMOUNT_OF_REQUESTED_MEMORY
dl PROCESS_ID
cv PROCESS_ID VIRTUAL_ADDRESS
pm
Exit
cv 2 1
5
Enter:   cr AMOUNT_OF_REQUESTED_MEMORY
dl PROCESS_ID
cv PROCESS_ID VIRTUAL_ADDRESS
pm
Exit
dl 1
Enter:   cr AMOUNT_OF_REQUESTED_MEMORY
dl PROCESS_ID
cv PROCESS_ID VIRTUAL_ADDRESS
pm
Exit
pm
0000110000

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;

import org.junit.platform.engine.TestDescriptor.Visitor;

class Process{
   protected Integer processID;
   protected Integer startIndex;
   protected Integer endIndex;
  
   public Process(Integer pID,Integer startIndex, Integer endIndex) {
       super();
       this.processID = pID;
       this.startIndex = startIndex;
       this.endIndex = endIndex;
   }
}

public class MemoryManagment {

   private Integer memorySize;
   ArrayList<Process> process = new ArrayList<>();
  
   public MemoryManagment(Integer memorySize) {
       this.memorySize = memorySize;
   }


   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter amount of memory to be managed");
       Integer size = sc.nextInt();
       int spaceAvailable = size;
      
       int memoriesSpaces[] = new int[size];
       int Pids = 1;
       MemoryManagment mmu = new MemoryManagment(size) ;
      
       String command;
       while (true) {
           System.out.println("Enter: cr AMOUNT_OF_REQUESTED_MEMORY\r\n" +
                   "dl PROCESS_ID\r\n" +
                   "cv PROCESS_ID VIRTUAL_ADDRESS\r\n" +
                   "pm\r\n" +
                   "Exit");
           command = sc.nextLine();
           String com;
           int requestedmemory;
           if(command.equalsIgnoreCase("exit")) {
               break;
           }else if(command.startsWith("cr")) {
               StringTokenizer st = new StringTokenizer(command, " ");
               com = st.nextToken();
               requestedmemory = Integer.parseInt(st.nextToken());
              
               if(spaceAvailable < requestedmemory) {
                   System.out.println("No memory available for the amount you requested");
                   continue;
               }
              
               //find chunk in memory
               int startIndex=0, endIndex=requestedmemory -1;
               boolean memoryAlloacted = false;
               for(int i=0;i<size;i++) {
                   startIndex = i; // set sstart index with first filled memory space
                   int counter = startIndex;
                   int j=0;
                   while(j < requestedmemory) {
                       if(memoriesSpaces[counter++] != 0)
                           break;
                       j++;
                   }
                   if(j == requestedmemory) { // if we found a continous chunk of memory then alocated the momory
                       memoryAlloacted = true;
                       spaceAvailable -= requestedmemory;
                       endIndex = counter -1; // set end index with the last fill memory space
                       break;
                   }
               }
               if(memoryAlloacted == false) {
                   System.out.println("No memory available for the amount you requested");
                   continue;
               }else {
                   for(int i=startIndex;i<=endIndex;i++)
                       memoriesSpaces[i] = 1;
                  
                   Process p = new Process(Pids, startIndex, endIndex); // create a new process and add into memory managment unit
                   mmu.process.add(p);
                   System.out.println(Pids + " " + startIndex + " " + endIndex);
                   Pids++;
               }
           }else if(command.startsWith("dl")) {
              
               StringTokenizer st = new StringTokenizer(command, " ");
               com = st.nextToken();
               int pid = Integer.parseInt(st.nextToken());
              
               Process p = null;
               for(Process p1 : mmu.process) {
                   if(p1.processID == pid) {
                       p = p1;
                       break;
                   }
               }
               if(p.equals(null)) {
                   System.out.println("No process found with id " + pid);
               }else {
                   int start = p.startIndex, end = p.endIndex;
                  
                   //if we found the process the deallocate the memory spaces
                   for(int i=start;i<=end;i++) {
                       memoriesSpaces[i] = 0;
                   }
               }
              
           }else if(command.startsWith("cv")) {
               StringTokenizer st = new StringTokenizer(command, " ");
               com = st.nextToken();
               int pid = Integer.parseInt(st.nextToken());
               int virtualAddress = Integer.parseInt(st.nextToken());
              
               Process p = null;
               for(Process p1 : mmu.process) {
                   if(p1.processID == pid) {
                       p = p1;
                       break;
                   }
               }
               if(p.equals(null)) {
                   System.out.println("No process found with id " + pid);
               }else {
                   int start = p.startIndex, end = p.endIndex;
                  
                   int physicalAddress = start + virtualAddress; // convert virtual address to physical address
                   System.out.println(physicalAddress);
               }
           }else if(command.startsWith("pm")) {
               for(int i=0;i<size;i++) {
                   System.out.print(memoriesSpaces[i]);
               }
               System.out.println();
           }
          
       }
      
   }
}

//output:

Enter amount of memory to be managed
10
Enter: cr AMOUNT_OF_REQUESTED_MEMORY
dl PROCESS_ID
cv PROCESS_ID VIRTUAL_ADDRESS
pm
Exit
Enter: cr AMOUNT_OF_REQUESTED_MEMORY
dl PROCESS_ID
cv PROCESS_ID VIRTUAL_ADDRESS
pm
Exit
cr 4
1 0 3
Enter: cr AMOUNT_OF_REQUESTED_MEMORY
dl PROCESS_ID
cv PROCESS_ID VIRTUAL_ADDRESS
pm
Exit
cr 8
No memory available for the amount you requested
Enter: cr AMOUNT_OF_REQUESTED_MEMORY
dl PROCESS_ID
cv PROCESS_ID VIRTUAL_ADDRESS
pm
Exit
cr 2
2 4 5
Enter: cr AMOUNT_OF_REQUESTED_MEMORY
dl PROCESS_ID
cv PROCESS_ID VIRTUAL_ADDRESS
pm
Exit
pm
1111110000
Enter: cr AMOUNT_OF_REQUESTED_MEMORY
dl PROCESS_ID
cv PROCESS_ID VIRTUAL_ADDRESS
pm
Exit
cv 2 1
5
Enter: cr AMOUNT_OF_REQUESTED_MEMORY
dl PROCESS_ID
cv PROCESS_ID VIRTUAL_ADDRESS
pm
Exit
dl 1
Enter: cr AMOUNT_OF_REQUESTED_MEMORY
dl PROCESS_ID
cv PROCESS_ID VIRTUAL_ADDRESS
pm
Exit
pm
0000110000
Enter: cr AMOUNT_OF_REQUESTED_MEMORY
dl PROCESS_ID
cv PROCESS_ID VIRTUAL_ADDRESS
pm
Exit
exit


Related Solutions

. Write a program to print * in the following order using 2d array in java...
. Write a program to print * in the following order using 2d array in java                                              *             *             *             *             *                                              *             *             *             *                                              *             *             *                                              *             *                                                          *
Write a method that will accept a 2D character array. The 2D array represents a grid...
Write a method that will accept a 2D character array. The 2D array represents a grid (table) of characters in which a triple may occur. A triple is 3 matching characters. This method will search through the array to determine whether or not it contains a set of 3 matching characters. Specifically, it will check to see if the 3 matching characters appear somewhere in the array as three adjacent characters either horizontally (left to right) or vertically (top to...
Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
java 1.) a method to append a 2d double array at the right of another 2d...
java 1.) a method to append a 2d double array at the right of another 2d double array, return a new array. E.g. numss1: {{1, 2}, {3, 4, 5}, {6}}, numss2: {{7}, {8, 9}}, return {{1, 2, 7}, {3, 4, 5, 8, 9}, {6}}
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
Write a method that displays every other element of an array. Write a program that generates...
Write a method that displays every other element of an array. Write a program that generates 100 random integers between 0 and 9 and displays the count for each number. (Hint: Use an array of ten integers, say counts, to store the counts for the number of 0s, 1s, . . . , 9s.) Write two overloaded methods that return the average of an array with the following headers:      public static int average(int[] intArray)        public static double average(double[] dArray) Also,...
i want a program in java that finds the shortest path in a 2D array with...
i want a program in java that finds the shortest path in a 2D array with obstacles from source to destination using BFS and recursion. The path must be stored in a queue. The possible moves are left,right,up and down.
please use java swing and recursion and the suggested method hints listed Objective: Write a program...
please use java swing and recursion and the suggested method hints listed Objective: Write a program in which draws (yes it actually makes a picture) a triangular fractal using recursion. This is best if done using a java applet. Suggested Methodology The idea for it is this First draw a filled equilateral triangle Next draw another filled equilateral triangle of a different color that’s upside down in the middle of that triangle Using the other triangles formed repeat step 2...
***JAVA PROGRAM Write a method called shrink that accepts an array of integers (that the user...
***JAVA PROGRAM Write a method called shrink that accepts an array of integers (that the user inputs) as a parameter and returns a new array containing the result of replacing each pair of integers with the sum of that pair. For example, if an array called list stores the values {7, 2, 8, 9, 4, 15, 7, 1, 9, 10}, then the call of shrink(list) should return a new array containing {9, 17, 19, 8, 19}. The first pair from...
Recursion java: 1. Write a recursive algorithm to add all the elements of an array of...
Recursion java: 1. Write a recursive algorithm to add all the elements of an array of n elements 2. Write a recursive algorithm to get the minimum element of an array of n elements 3. Write a recursive algorithm to add the corresponding elements of two arrays (A and B) of n elements. Store the results in a third array C .4. Write a recursive algorithm to get the maximum element of a binary tree 5. Write a recursive algorithm...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT