Question
Java Homework Assignment. Just need to implement PysicalMemory for this assingment. I'll include what is done so far to test it. public class Factory {
Java Homework Assignment. Just need to implement PysicalMemory for this assingment.
I'll include what is done so far to test it.
public class Factory
{
public static void main(String args[]) {
PhysicalMemory physicalMemory = new PhysicalMemory();
Thread[] threadArray = new Thread[5];
for(int i=0; i
threadArray[i] = new Thread(new MemoryThread(physicalMemory, i));
for(int i=0; i
threadArray[i].start();
}
}
----------------------
import java.util.Random;
public class MemoryThread implements Runnable
{
public MemoryThread(PhysicalMemory m, int id) {
physicalMemory = m;
threadID = id;
}
public void run()
{
//Generate a memory request of a random size
Random generator = new Random();
int requestSize = 1 + generator.nextInt(MAX_REQUEST_SIZE);
//Debuging message
//System.out.println("Thread " + threadID + " has been started");
//request memory space
boolean flag = physicalMemory.requestMemory(threadID, requestSize);
//Debuging message
//System.out.println("Thread " + threadID + " has been allocate memory space " + flag);
while (!flag)
{
flag = physicalMemory.requestMemory(threadID, requestSize);
}
//Generate a random memory access
int requestPageNumber = generator.nextInt(requestSize);
int frameNumber = physicalMemory.accessMemory(threadID, requestPageNumber);
System.out.printf("Thread %d requests for page number %d which has frame number %d ", threadID, requestPageNumber, frameNumber);
// Release the memory
physicalMemory.releaseMemory(threadID);
}
private PhysicalMemory physicalMemory;
private int threadID;
public static final int MAX_REQUEST_SIZE = 50;
}
----------------------
import java.util.Arrays;
public class PageTable
{
/* id of the thread a page table is associated with */
private int tid;
/* size of the page table */
private int size;
/* frame number array. page number is used to index the array to find the corresponding frame number */
private int[] pageTableArray;
/* length of the page table */
private int length;
public PageTable(int id, int s)
{
tid = id;
size = s;
length = 0;
pageTableArray = new int[s];
}
/* return the frame number of the page number (index) */
public int lookupPageTable(int index)
{
return pageTableArray[index];
}
/* add an entry to the end of the page table */
public void appendEntry(int frameNumber)
{
pageTableArray[length] = frameNumber;
length++;
}
/* return length of the page table */
public int getLength()
{
return length;
}
/* return thread ID */
public int getThreadID()
{
return tid;
}
/* print the content of the page table */
public void printPageTable()
{
for(int i=0; i
{
System.out.println("Thread " + tid + " Page " + i + " has frame number " + pageTableArray[i]);
}
}
}
----------------------
public class PageTableList
{
public PageTableList()
{
first = null;
}
/* add a new entry to the head of the linked list */
public void addFirst(PageTable pt)
{
Node newNode = new Node();
newNode.pt = pt;
newNode.next = first;
first = newNode;
}
/* return the page table associated with the thread id */
public PageTable lookup(int id)
{
Node t = first;
while(t != null)
{
if(t.pt.getThreadID() == id)
return t.pt;
t = t.next;
}
return null;
}
/* head of the linked list */
private Node first;
private class Node
{
public PageTable pt;
public Node next;
}
}
You are asked to implement the PhysicalMemory class with the following public interfaces *constructor* public PhysicalMemoryo /* If the memory has enough free frames to satisfy this request, allocate the frames to the thread and build the page table @param id the threadID of the requesting thread @param requestLength the number of pages the thread requests @return If the request can be satisfied(which means the free frames in memory is greater or equal to the requestLength), return true else return false public boolean requestMemory(int id, in requestLength) You are asked to implement the PhysicalMemory class with the following public interfaces *constructor* public PhysicalMemoryo /* If the memory has enough free frames to satisfy this request, allocate the frames to the thread and build the page table @param id the threadID of the requesting thread @param requestLength the number of pages the thread requests @return If the request can be satisfied(which means the free frames in memory is greater or equal to the requestLength), return true else return false public boolean requestMemory(int id, in requestLength)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started