/*Purpose: Memory Management Unit Simulation*/ /*Authors: Martin Payne */ /* Jeremy Libner */ /* Tyler Makovicka */ /* Kushal Tiwari */ /* Trebor Asher */ /* Kimberly Batton */ import java.util.Random; import java.util.Scanner; public class MemoryManagementUnit { public static void main(String [] args) { //The following arrays simulating RAM, Virtual Memory space of a single program, and the disk. int[] RAM = new int [8]; //the 1st dimintion of VM array gives where the page is located on the disk //the 2nd dimintion of VM array gives page location in RAM or is 8 if not in RAM int[][] VM = new int [16][2]; int[] disk = new int [64]; int randomNumber; //location of program on disk int nextOpenSlot = 0; int pageRequest; boolean enterAgain = true; Scanner in = new Scanner(System.in); Random rng = new Random(); int programStartSlot = rng.nextInt(48); //fills every slot on disk with a random number for (int i=0; i<64; i++) { randomNumber = rng.nextInt(1000); disk[i] = randomNumber; } //stores program addresses into virtual memory for (int i=0; i<16; i++) { VM[i][0] = programStartSlot + i; VM[i][1] = 8; } //while loop runs while user simulates program requesting pages of memory while (enterAgain == true) { System.out.println("Enter the page segment of the program that you need to access 1-16."); pageRequest = in.nextInt() - 1; //handles case of page not being in RAM and moves it to RAM then displays it if (VM[pageRequest][1] == 8) { System.out.println("Page is not in RAM, moving page to RAM slot " + nextOpenSlot + " from disk."); RAM[nextOpenSlot] = disk[VM[pageRequest][0]]; VM[pageRequest][1] = nextOpenSlot; if (nextOpenSlot != 7) { nextOpenSlot++; }else { nextOpenSlot = 0; } System.out.println("Page moved to RAM the value stored in it is: " + RAM[VM[pageRequest][1]]); //handles case of page being in RAM and displays the value }else { System.out.println("Page in RAM, the value stored in the page is: " + RAM[VM[pageRequest][1]]); } //displays values in RAM and virtual memory every time a page request is made System.out.println("Current values in RAM: [" + RAM[0] + "] [" + RAM[1] + "] [" + RAM[2] + "] [" + RAM[3] + "] [" + RAM[4] + "] [" + RAM[5] + "] [" + RAM[6] + "] [" + RAM[7] + "]"); System.out.println("Current values in Virtual Memory:"); for (int i=0; i<16; i++) { if (VM[i][1] == 8) { System.out.println("Disk Location [" + VM[i][0] + "] not in RAM"); }else { System.out.println("Disk Location [" + VM[i][0] + "] in RAM slot [" + VM[i][1] + "]"); } } System.out.println("To request another page enter 1, enter 0 to exit."); if (in.nextInt() != 1) { enterAgain = false; } } } }