Example #1
0
/**
 * Common framework for page replacement, with divergent functions
 * broken out.
 */
void replacePages(const char *refString, int frameCount, char *frameReport, void (*initPageReplacement)(void),
        void (*servicePageFault)(int), int (*getVictimPage)(int *, int), void (*usePage)(int), void (*cleanUp)(void)) {
    // The simulated physical memory.
    int frames[frameCount];
    initializeFrames(frames, frameCount);
    
    // Initialize the frame report.
    strncpy(frameReport, "", 1);
    
    // *** Initialize any needed page replacement data structures.
    initPageReplacement();
    
    // Iterate through the reference string.
    int currentReference = 0;
    int referenceCount = strlen(refString);
    while (currentReference < referenceCount) {
        // First, report on the current state of things.
        appendFrameState(frames, frameCount, frameReport);
        
        // Grab the page reference and see if it's in memory.
        int page = getReference(refString, currentReference);
        if (getPageFrame(page, frames, frameCount) == -1) {
            // *** Page fault; fetch the page.
            servicePageFault(page);
            
            // Allocate a frame.
            int frame = getAvailableFrame(frames, frameCount);
            if (frame == -1) {
                // *** Not enough frames; replace a page.
                int victim = getVictimPage(frames, frameCount);
                frame = getPageFrame(victim, frames, frameCount);
            }
            frames[frame] = page;
        }
        
        // *** "Access" the page.
        usePage(page);

        // Move to the next reference.
        currentReference++;
    }
    
    // Issue one last report, and conclude it.
    appendFrameState(frames, frameCount, frameReport);
    finishFrameReport(frameReport);
    
    // *** Perform any necessary clean-up.
    cleanUp();
}
int main(void)
{
  // open input file.
  printf("Enter input file name :\n");
  scanf("%s", gStrBuf);
  printf("\n");
  gInOutFile = fopen(gStrBuf, "r");

  if(gInOutFile != NULL) {
    printf("File : %s opened success.\n", gStrBuf);

  } // end if
  else {
    printf("File : %s could not be opened, or not exists.\n", gStrBuf);
	  printf("Program terminated.\n");

	  return 0;

  } // end else

  // Get page frame value
  getPageFrame();
  printf("Page frame : %d\n", gPageFrame);

  // Get page request
  getPageRequest();
  // print out page request
  printf("Page request : ");
  for(int CNT = 0; gPageRequest[CNT] != END_SIGN; CNT++)
    printf("%d ", gPageRequest[CNT]);
  printf("\n");

  // Close input file
  fclose(gInOutFile);

  // Declaration memory page reference
  MEM_REF* memoryPage = (MEM_REF*) malloc(sizeof(MEM_REF) * gPageFrame);
  // Initial memory page
  initMemPage(memoryPage);
  // Output memory page and information
  outMemPage(0, memoryPage);
  printf("\n");
  outMemInf();

  // Reset memory information and memory page
  clearMemInf();
  initMemPage(memoryPage);

  // Method First In First Out
  printf("----- First In First Out -------------\n");
  FIFO(memoryPage);
  outMemInf();

  return 0;

} // END main