Advertisement
Tiger0915

Untitled

Dec 5th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.95 KB | None | 0 0
  1. #include "vmsim.h"
  2.  
  3. /** pageFault **/
  4. /*  Process page fault */
  5. int pageFault(unsigned logicalAddress) {
  6.     int pageNumber = logicalAddress >> 10;
  7.     static int clockHand = 0; // always points to one index past last frame referenced
  8.     int i;
  9.      
  10.     /* my implementation */
  11.  
  12.     /* placement round */
  13.     for(i=0; i< numPageFrames; i++) {
  14.         /* if we've reached a page not in memory, use that index */
  15.         if(pageTable[i].valid == 0) {
  16.             pageTable[i].vmPage = pageNumber;
  17.             pageTable[i].valid = 1;
  18.             pageTable[i].referenced = 0;
  19.             pageTable[i].modified = 0;
  20.             clockHand = i+1;
  21.             if(clockHand >= numPageFrames) clockHand = 0;
  22.             return (0);
  23.         }
  24.     }
  25.  
  26.  
  27.     /* Replacement Loop, because placement failed */
  28.     while(1) {
  29.         /* if current spot is not referenced, replace it */
  30.         if(pageTable[clockHand].referenced == 0) {
  31.             pageTable[clockHand].vmPage = pageNumber;
  32.             pageTable[clockHand].valid = 1;
  33.             pageTable[clockHand].referenced = 0;
  34.             if(pageTable[clockHand].modified == 1) {
  35.                 pageTable[clockHand].modified = 0;
  36.                 clockHand++; /* increment clock pointer for next time */
  37.                 if(clockHand >= numPageFrames) clockHand = 0;
  38.                 return (2);
  39.             } else {
  40.                 clockHand++; /* increment clock pointer for next time */            
  41.                 if(clockHand >= numPageFrames) clockHand = 0;
  42.                 return (1);
  43.             }
  44.         } else { /* if spot has already been referenced, set it to unreferenced and move on */
  45.             pageTable[clockHand].referenced = 0;
  46.         }
  47.         /* if we failed for this index, increment clockHand, if over threshold, reset to 0 */
  48.         clockHand++;
  49.         if(clockHand >= numPageFrames) clockHand = 0;
  50.     }
  51.     printf("Oh no! I don't think I'm supposed to be here! ");    
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement