Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective: -In C translate a virtual address to a physical address, involving a

ID: 3717968 • Letter: O

Question

Objective:

-In C translate a virtual address to a physical address, involving a custom-sized fully associative page table.

Inputs:

-The total size of physical memory (in words)

-The page size (words/page)

-The replacement policy (LRU, FIFO)

Outputs:

-The corresponding physical address for a virtual address A message indicating a page fault (if any) in the page table

Specification:

-The program translates a virtual address to a physical address based on choosing from a menu of choices, where each choice calls the appropriate procedure, where the choices are:

a) Set parameters

b) Map virtual address

c) Quit program

-Upon entering the parameters, the page table is to be dynamically allocated based on the total number of page frames. The virtual pages will be mapped to the page frames on demand in the page frame order 0,1,2,3,…

--------------------------------------------------------------------------------

TEST RUN EXMPLE

Use below to test the program, yours should run exactly the same and have same outputs

--------------------------------------------------------------------------------

-------------------------------------------------------

Virtual memory to Main memory mapping:

-------------------------------------------------------

a) Enter parameters

b) Map virtual address

c) Quit

Enter selection: a

Enter main memory size (words): 2048

Enter page size (words/page): 1024

Enter replacement policy (0=LRU, 1=FIFO): 0

-------------------------------------------------------

Virtual memory to Main memory mapping:

-------------------------------------------------------

a) Enter parameters

b) Map virtual address

c) Quit

Enter selection: b

Enter virtual memory address to access: 5000

Page fault!

-----------------

| VP | PF |

-----------------

| 4 | 0 |

-----------------

-------------------------------------------------------

Virtual memory to Main memory mapping:

-------------------------------------------------------

a) Enter parameters

b) Map virtual address

c) Quit

Enter selection: b

Enter virtual memory address to access: 2048

Page fault!

-----------------

| VP | PF |

-----------------

| 4 | 0 |

-----------------

| 2 | 1 |

-----------------

-------------------------------------------------------

Virtual memory to Main memory mapping:

-------------------------------------------------------

a) Enter parameters

b) Map virtual address

c) Quit Enter selection:

b Enter virtual memory address to access: 4509

Virtual address 4509 maps to physical address 413

-----------------

| VP | PF |

-----------------

| 2 | 1 |

-----------------

| 4 | 0 |

-----------------

-------------------------------------------------------

Virtual memory to Main memory mapping:

-------------------------------------------------------

a) Enter parameters

b) Map virtual address

c) Quit Enter selection: b

Enter virtual memory address to access: 7160

Page fault!

-----------------

| VP | PF |

-----------------

| 4 | 0 |

-----------------

| 6 | 1 |

-----------------

-------------------------------------------------------

Virtual memory to Main memory mapping:

-------------------------------------------------------

a) Enter parameters

b) Map virtual address

c) Quit

Enter selection: c

Explanation / Answer

ANSWER:-

#include <stdio.h>
#include <stdlib.h>


//Parameters to enter
int sizeOfMainMemory;
int sizeOfPage;
int replacementPolicy;//Can be either 1 or 0
int numPages;
int choice;//user selection for options

struct pageTable {
    //Page frames will be mapped to virtual pages
    int pageFrame;
    int virtualPage;
} *memory;

void enterParameters() {
    printf("Enter main memory size (words):");
    scanf("%d", &sizeOfMainMemory);

    printf("Enter page size (words/page):");
    scanf("%d", &sizeOfPage);

    printf("Enter replacement policy (0=LRU, 1=FIFO): ");
    scanf("%d", &replacementPolicy);

    numPages = sizeOfMainMemory / sizeOfPage;

    memory = (struct pageTable *)malloc(numPages * sizeof(struct pageTable));


    int i;
    for (i = 0; i < numPages; i++) {
        //Flag each member of struct's index as -1
        memory[i].pageFrame = -1;
        memory[i].virtualPage = -1;
    }


}

void mapVA() {
    int VMAddress;//From user
    int realAddress;//Generated
    int virtualPage;
    int offset;


    printf("Enter virtual memory address to access: ");
    scanf("%d", &VMAddress);

    //Calculate offset and virtual page
    offset = VMAddress % sizeOfPage;
    virtualPage = VMAddress / sizeOfPage;

    int i;
    for (i = 0; i < numPages; i++) {
        //if there is a hit (page in memory)
        if (memory[i].virtualPage == virtualPage) {
            realAddress = (memory[i].pageFrame * sizeOfPage) + offset;
            //LRU
            if (replacementPolicy == 0) {
                int pfHolder;//placeholder for pageframe
                int vpHolder;//placeholder for virtualpage
                int g;
                for (g = i; g < numPages - 1; g++) {
                    //Shift page table at index of hit
                    pfHolder = memory[g].pageFrame;
                    vpHolder = memory[g].virtualPage;
                    memory[g] = memory[g + 1];
                    memory[g + 1].pageFrame = pfHolder;
                    memory[g + 1].virtualPage = vpHolder;
                }
            }
            printf("Virtual Address: %d maps to physical address %d", VMAddress,realAddress);
            i = numPages-1;
        }
            //if theres a blank space
        else if (memory[i].virtualPage == -1) {
            //map virtual page to generated virtual page
            memory[i].virtualPage = virtualPage;
            //map pageframe to current index
            memory[i].pageFrame = i;
            printf("Page fault! ");
            //set index to last element
            i = numPages - 1;
        }
        else if (i == numPages - 1) {//condition for last element
            memory[0].virtualPage = virtualPage;
            int pfHolder;//placeholder for pageframe
            int vpHolder;//placeholder for virtualpage
            int h;
            for (h = 0; h < numPages - 1; h++) {
                //shift page table from beginning
                pfHolder = memory[h].pageFrame;
                vpHolder = memory[h].virtualPage;
                memory[h] = memory[h + 1];
                memory[h + 1].pageFrame = pfHolder;
                memory[h + 1].virtualPage = vpHolder;
            }
            printf("Page Fault! ");
        }

    }

    printf(" ---------------------- ");
    printf("| VP   | PF   | ");
    printf("---------------------- ");
    for (i = 0; i < numPages; i++) {
        if (memory[i].pageFrame != -1 && memory[i].virtualPage != -1) {//To avoid unnecessary printing
            printf("| %d    |", memory[i].virtualPage);
            printf(" %d    |", memory[i].pageFrame);
            printf(" ---------------------- ");
        }
    }
}


int main() {
    int boolean = 0;
    while (!boolean) {
        printf("Virtual memory to Main memory mapping: ");
        printf("------------------------------------ ");
        printf("1) Enter parameters ");
        printf("2) Map virtual address ");
        printf("3) Quit: ");

        scanf("%d", &choice);

        if (choice == 1) {
            enterParameters();
        }

        else if (choice == 2) {
            mapVA();
        }

        else if (choice == 3) {
            return 0;
        }

        else//Catches anything outside of 1-3
            printf("Invalid Entry");
        printf(" ");
        printf(" ");

    }
}