In: Computer Science
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
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