Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int pageReplacement(int *numFrames, int *numReferences, int **references){
- int curReference, pageFaults = 0, curFrameSize = 0;
- List list = NULL, tmp;
- // second-page algorithm
- for(int i=0; i<*numReferences; i++){
- curReference = (*references)[i];
- printf("%d\t\t", curReference); // DEBUG
- if( isNotInMemory(list, curReference) ){
- printf("Y\t\t"); // DEBUG
- pageFaults++;
- // queue is full
- if( curFrameSize == (*numFrames) ){
- while(list->referenceBit != 0){
- // set reference bit to 0
- list->referenceBit = 0;
- // move to end of queue
- tmp = list;
- while(tmp->next != NULL){
- tmp = tmp->next;
- }
- tmp->next = list;
- list = list->next;
- }
- printf("[%d] ", list->val);
- // take the head out
- tmp = list;
- list = list->next;
- // free(tmp);
- curFrameSize--;
- }
- // allocate memory for new element
- List newElem = malloc(sizeof(struct ListNode));
- assert( newElem != NULL);
- newElem->val = curReference;
- newElem->referenceBit = 0;
- newElem->next = NULL;
- // insert new element at the end
- tmp = list;
- if(i>0){
- while(tmp->next != NULL){
- tmp = tmp->next;
- }
- tmp->next = newElem;
- } else {
- list = newElem;
- }
- curFrameSize++;
- } else {
- printf("N\t\t"); // DEBUG
- // page is in memory
- // set reference bit to 1
- List referenceToPage = list;
- while(referenceToPage->val != curReference){
- assert(referenceToPage != NULL);
- referenceToPage = referenceToPage->next;
- }
- referenceToPage->referenceBit = 1;
- }
- printList(list); // DEBUG
- printf("\n");
- }
- freeList(list);
- return pageFaults;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement