Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1: Memory Management You are requested to write a Java program of a simp

ID: 3731552 • Letter: P

Question

Problem 1: Memory Management 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. 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 Addres 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

Explanation / Answer

import java.io.*;
import java.util.*;

class mmu
{
public static void main(String agrs[])throws IOException
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter amount of memory to be managed");
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=-1; //array a stores the process id corresponding to each memory address. if empty, then it stores -1
int id=1;
String[] s=sc.nextLine().split(" ");
while(true)
{
System.out.println("Enter cr AMOUNT_OF_REQUESTED_MEMORY dl PROCESS_ID cv PROCESS_ID VIRTUAL_ADDRESS pm exit");
s=sc.nextLine().split(" ");
if(s[0].equals("cr"))
{
    int space=0,pos=0,pos_final=0,max=0;
    for(int i=0;i<n;i++)
    {
      if(a[i]==-1)
         {space++; if(space==1) pos=i;}
      else
          space=0;
       if(space>max)
              {max=space;
                pos_final=pos;}
       }
       if(space>=Integer.parseInt(s[1]))
        { for(int i=pos_final;i<pos_final+Integer.parseInt(s[1]);i++)
              a[i]=id;
        System.out.println(id+++" "+pos_final+" "+(pos_final+Integer.parseInt(s[1])-1));
        }
        else
         System.out.println("No memory available for the amount you requested");
   }

  
   if(s[0].equals("dl"))
   {
     for(int i=0;i<n;i++)
        if(a[i]==Integer.parseInt(s[1])) a[i]=-1;
   }


if(s[0].equals("cv"))
{
   int i;
   for(i=0;i<n;i++)
     if(a[i]==Integer.parseInt(s[1]))
       break;
    System.out.println(i+Integer.parseInt(s[2]));
}

   if(s[0].equals("pm"))
   {
    for(int i=0;i<n;i++)
       if(a[i]==-1)
          System.out.print("0");
       else
           System.out.print("1");
      System.out.println();
   }


   if(s[0].equals("exit"))
     System.exit(0);
}
}
}