Question: Hello, i have trouble with the method, so first output is fine but second output is incorrect because something in code makes nonsense. CAn you

Hello, i have trouble with the method, so first output is fine but second output is incorrect because something in code makes nonsense. CAn you help me with one? THANK YOU!!!!!!!!

Objective:

To 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 ondemand in the page frame order 0,1,2,3,...

{Hint: When dynamically allocating the page table, initialize each virtual page field to -1 to make it invalid; the page frames do not need to be initialized}

{Hint: In order to maintain the order of references, arrange the virtual page/physical page frame entries in the page table such that the least-recently-used (or first-in) entry is at index 0upon revisiting a virtual page (page hit), rearrange the entries, such that the most recently visited page is moved to the last (or lowest-indexed used) entry of the page table.}

{Hint: Upon replacement (page fault), decrease the index of each of the valid entries by 1 and insert the new virtual page/page frame entry at the last (or lowest-indexed unused) entry of the page table}.

Sample test run

% ./a.out

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 -------------------------------------- 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| -----------------

Main memory mapping: 

Virtual memory to -------------------------------------- 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| -----------------

Main memory mapping: 

Virtual memory to -------------------------------------- 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| -----------------

Main memory mapping: 

Virtual memory to -------------------------------------- a) Enter parameters b) Map virtual address c) Quit

Enter selection: c 

#include #include

/* Define page table as dynamic structure containing virtual page and page frame and initialize variable as pointer to structure */ struct node { int vp; int pf; } *pt = NULL;

typedef struct node entry;

/* Declare global var's */ int mm_size, page_size, policy; //use the policy for replacement: FIFO or LRU int num_entries; // all tables /**************************************************************/ void enter_params() //"void FUNCTION FOR OPTION a"() { /* Declare local var's */ int i;

/* Prompt for main memory size, page size, and replacement policy */ printf("Enter main memory size (words): "); scanf("%d", &mm_size);

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

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

num_entries = mm_size / page_size;

/* Allocate and initialize page table based on number of entries */ pt = (entry *)malloc(num_entries * sizeof(entry));

/*for loop: i = 0 to num_entries-1 */ for(i = 0; i < num_entries - 1; i++) { pt[i].vp = -1; }

return; }

/***************************************************************/ void mapping() //"void FUNCTION FOR OPTION b"() { /* Declare local var's */ int va; // virtual address int vp; // virtual page int pa; //physical address int pf; //physical format int offset; // formula for modulus int i = 0, j, k; int base; // its all about the base

/* Prompt for virtual address */ printf("Enter virtual memory address to access: "); scanf("%d", &va);

//printf("Virtual address: " + va && "physical address " + pa); //scanf("%d", &va, &pa);

/* Translate virtual mem addr to virtual page and offset*/ vp = va / page_size; // check slides offset = va % page_size; //check sludes

/* Check for end of table, unallocated entry, or matched entry in table while none of three cases, keep looping */ while( (i < num_entries) && (pt[i].vp != -1) && (pt[i].vp != vp)) //case 2 shpuld be ==, not != (POSSIBLY) { i++; }

/* In case of end of table, replace either LRU or FIFO entry (top entry in page table), print message */ if (i >= num_entries) { pf = pt[0].pf; for(j = 0; j < num_entries - 1; j++) { pt[j] = pt[j+1]; } pt[num_entries-1].vp = vp; pt[num_entries-1].pf = pf; printf("Page fault! "); } /* In case of unallocated entry, set entry according to virtal page and page frame, print message */ else if (pt[i].vp == -1) { pt[i].vp = vp; pt[i].pf =i; printf("Page fault! "); }

/* In case of hit in page table, calculate physical address and print message, update page table if LRU policy */ else { base = pt[i].pf * page_size; pa = base + offset;

if (policy == 0) //LRU { pf = pt[i].pf; for(k = i; k < num_entries - 1; k++) { pt[k] = pt[k+1]; //check if pt[k+1] == -1, then BREAK } pt[num_entries-1].vp = vp; //k OR k-1 pt[num_entries-1].pf = pf; // k OR k-1 in the argument function [.....] } printf("Virtual address %d maps to physical address %d ", va, pa); } /* Print out current state of page table */ /* Extra Credit: do least-frequently-used LFU */ printf(" ----------------- "); printf("| VP\t| PF\t| "); for(i = 0; i < num_entries - 1; i++) { printf("----------------- "); printf("| %d | %d | ", pt[i].vp, pt[i].pf); } printf("----------------- ");

return; } /**************************************************************/ int main() { /* Declare local var's */ char choice = ' '; /* Until user quits, print menu of options, prompt for user input, and select appropriate option */ while (choice != 'c') { printf(" Virtual memort to Main memory mapping: "); printf(" ----------------------------------"); printf(" a) Enter parameters: "); printf(" b) Map virtual address: "); printf(" c) Quit"); printf(" Enter selection: "); scanf(" %c", &choice); switch (choice) { case 'a': enter_params(); break; case 'b': mapping(); break; case 'c': printf("GoodBye! ");break; default: printf("Invalid. Please read carefully the following choices: a, b,c "); } } return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!