Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.48 KB | None | 0 0
  1. //description of a memory manager
  2. //in this simple case we assume only one process
  3. //otherwise the we need a list of Page Tables instead of only one
  4. //It is also assumed that frame size is equal to page size
  5. package lab3;
  6.  
  7. import java.io.FileNotFoundException;
  8. import java.io.IOException;
  9. import java.io.RandomAccessFile;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12.  
  13. /**
  14.  *
  15.  * @author jof
  16.  */
  17. public class MemoryManager {
  18.  
  19.     private int NbrOfPages; //number of pages in virtual memory
  20.     private int PageSize; //the number of bytes in one page
  21.     private int NbrOfFrames; //number of frames in physical memory
  22.     private int[] pageTable; //pageTable[n] gives the physical address for page n
  23.     //-1 if page is not in physical memory
  24.     private byte[] RAM; //physical memory RAM
  25.     private RandomAccessFile pageFile;
  26.     private int freePos; //points to the frame where we should insert page
  27.     private int pageFaults = 0;
  28.     private int[] currPages;
  29.     private long[] timeStamp;
  30.     private boolean full;
  31.  
  32.     public MemoryManager(int pages, int pageSize, int frames, String pFile) {
  33.         try {
  34.             //initate the virtual memory
  35.             NbrOfPages = pages;
  36.             PageSize = pageSize;
  37.             NbrOfFrames = frames;
  38.             freePos = 0;
  39.             currPages = new int[NbrOfFrames];
  40.             timeStamp = new long[NbrOfFrames];
  41.             full = false;
  42.             //create pageTable
  43.             //initialy no pages loaded into physical memory
  44.             pageTable = new int[NbrOfPages];
  45.             for (int n = 0; n < NbrOfPages; n++) {
  46.                 pageTable[n] = -1;
  47.             }
  48.             //allocate space for physical memory
  49.             RAM = new byte[NbrOfFrames * PageSize];
  50.             //initiate page file
  51.             pageFile = new RandomAccessFile(pFile, "r");
  52.         } catch (FileNotFoundException ex) {
  53.             Logger.getLogger(MemoryManager.class.getName()).log(Level.SEVERE, null, ex);
  54.         }
  55.     }
  56.  
  57.     public byte read(int logicalAddress) {
  58.         //called by a process to read memory from its logical address
  59.         byte data = 0;
  60.         //calculate pageNumber and index from the logical address
  61.         int index = logicalAddress % NbrOfPages;
  62.         int pageNumber = logicalAddress / NbrOfPages;
  63.         //check if we get a pageFault        
  64.         if (pageTable[pageNumber] == -1) {
  65.             //call method to solve page fault
  66.             //pageFault(pageNumber);
  67.             //the following two should be used in step 2 and 3 of the lab
  68.             //pageFaultFIFO(pageNumber);
  69.             pageFaultLRU(pageNumber);
  70.         }
  71.         //read data from RAM
  72.         int frame = pageTable[pageNumber];
  73.         int physicalAddress = frame * PageSize + index;
  74.         data = RAM[physicalAddress];
  75.         //print result
  76.         System.out.print("Virtual address: " + logicalAddress);
  77.         System.out.print(" Physical address: " + physicalAddress);
  78.         System.out.println(" Value: " + data);
  79.         return data;
  80.     }
  81.  
  82.     //solve a page fault for page number pageNumber
  83.     private void pageFault(int pageNumber) {
  84.         //this is the simple solution where we assume same size of physical and logical number
  85.         pageFaults++;
  86.         //load page into frame number freePos
  87.         pageTable[pageNumber] = freePos;
  88.         try {
  89.             //read data from pageFile into RAM
  90.             pageFile.seek(pageNumber * PageSize);
  91.             for (int b = 0; b < PageSize; b++) {
  92.                 RAM[freePos * PageSize + b] = pageFile.readByte();
  93.             }
  94.         } catch (IOException ex) {
  95.             Logger.getLogger(MemoryManager.class.getName()).log(Level.SEVERE, null, ex);
  96.         }
  97.         //update position to store next page
  98.         freePos++;
  99.  
  100.     }
  101.  
  102.     //solve a page fault for page number pageNumber
  103.     private void pageFaultFIFO(int pageNumber) {
  104.         //this solution allows different size of physical and logical number
  105.         pageFaults++;
  106.         //load page into frame number freePos
  107.         for (int i = 0; i < pageTable.length; i++) {
  108.             if (pageTable[i] == freePos) {
  109.                 pageTable[i] = -1;
  110.             }
  111.         }
  112.        
  113.         pageTable[pageNumber] = freePos;
  114.        
  115.         try {
  116.             //read data from pageFile into RAM
  117.             pageFile.seek(pageNumber * PageSize);
  118.             for (int b = 0; b < PageSize; b++) {
  119.                 RAM[freePos * PageSize + b] = pageFile.readByte();
  120.             }
  121.         } catch (IOException ex) {
  122.             Logger.getLogger(MemoryManager.class.getName()).log(Level.SEVERE, null, ex);
  123.         }
  124.         //update position to store next page
  125.         freePos++;
  126.         if (freePos == NbrOfFrames) {
  127.             freePos = 0;
  128.         }
  129.         //page replacement using FIFO
  130.         //freePos is used to point to next position
  131.     }
  132.  
  133.     //solve a page fault for page number pageNumber
  134.     private void pageFaultLRU(int pageNumber) {
  135.         //this solution allows different size of physical and logical number
  136.         //victim is chosen by least recently used algorithm
  137.         if (full) {         //if frames are full, check for LRU page
  138.             int LRUIndex = 0;
  139.             for (int i = 0; i < timeStamp.length; i++) {
  140.                 if (timeStamp[LRUIndex] > timeStamp[i]) {
  141.                     LRUIndex = i;
  142.                 }
  143.             }
  144.             pageTable[currPages[LRUIndex]] = -1;
  145.             freePos = LRUIndex;
  146.         }                  
  147.         //else, fill in all the frames and store their position, pageNumber and
  148.         //timestamp.
  149.         pageFaults++;
  150.         //load page into frame number freePos
  151.         pageTable[pageNumber] = freePos;
  152.         currPages[freePos] = pageNumber;
  153.         timeStamp[freePos] = System.nanoTime();
  154.         try {
  155.             //read data from pageFile into RAM
  156.             pageFile.seek(pageNumber * PageSize);
  157.             for (int b = 0; b < PageSize; b++) {
  158.                 RAM[freePos * PageSize + b] = pageFile.readByte();
  159.             }
  160.         } catch (IOException ex) {
  161.             Logger.getLogger(MemoryManager.class.getName()).log(Level.SEVERE, null, ex);
  162.         }
  163.         //update position to store next page
  164.         freePos++;
  165.         //if frames are full, then "full" is true and start looking for the
  166.         //LRU page.
  167.         if (freePos == NbrOfFrames) {
  168.             full = true;
  169.         }
  170.     }
  171.  
  172.     public int getNbrOfPagefaults() {
  173.         return pageFaults;
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement