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 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...
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...
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...
In java write a method that will take an array and change it into a linkedlist...
In java write a method that will take an array and change it into a linkedlist and then display it in the main method
Data Structures ( Recursion ) Assignment Write a recursive method removeMiddle that receives an array list...
Data Structures ( Recursion ) Assignment Write a recursive method removeMiddle that receives an array list which has odd number of elements, then it deletes the element in the middle only. The method should receive only one parameter (the array list) The base case is when the array list has only one element, just remove that, otherwise, you need to remove the first one and the last one then call the method, then you need to add them again after...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Which row has the largest sum? Write a method that takes a 2D int array and...
Which row has the largest sum? Write a method that takes a 2D int array and prints: of The index of the row that has the largest sum The sum of elements in that row java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT