Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "vmsim.h"
- /** pageFault **/
- /* Process page fault */
- int pageFault(unsigned logicalAddress) {
- int pageNumber = logicalAddress >> 10;
- static int clockHand = 0; // always points to one index past last frame referenced
- int i;
- /* my implementation */
- /* placement round */
- for(i=0; i< numPageFrames; i++) {
- /* if we've reached a page not in memory, use that index */
- if(pageTable[i].valid == 0) {
- pageTable[i].vmPage = pageNumber;
- pageTable[i].valid = 1;
- pageTable[i].referenced = 0;
- pageTable[i].modified = 0;
- clockHand = i+1;
- if(clockHand >= numPageFrames) clockHand = 0;
- return (0);
- }
- }
- /* Replacement Loop, because placement failed */
- while(1) {
- /* if current spot is not referenced, replace it */
- if(pageTable[clockHand].referenced == 0) {
- pageTable[clockHand].vmPage = pageNumber;
- pageTable[clockHand].valid = 1;
- pageTable[clockHand].referenced = 0;
- if(pageTable[clockHand].modified == 1) {
- pageTable[clockHand].modified = 0;
- clockHand++; /* increment clock pointer for next time */
- if(clockHand >= numPageFrames) clockHand = 0;
- return (2);
- } else {
- clockHand++; /* increment clock pointer for next time */
- if(clockHand >= numPageFrames) clockHand = 0;
- return (1);
- }
- } else { /* if spot has already been referenced, set it to unreferenced and move on */
- pageTable[clockHand].referenced = 0;
- }
- /* if we failed for this index, increment clockHand, if over threshold, reset to 0 */
- clockHand++;
- if(clockHand >= numPageFrames) clockHand = 0;
- }
- printf("Oh no! I don't think I'm supposed to be here! ");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement