Question
I need help in my C code.. I am implementing a Virtual Memory Manager. we are given file containing logical adresses. and we have to
I need help in my C code..
I am implementing a Virtual Memory Manager. we are given file containing logical adresses. and we have to translate them to physical adress and print out signed byte value stored in that address.
But we need to use the clock replacement policy or second-chance algorithm as the page replacement algorithm. Use C/C++ as your language.
pdf book link..
http://materias.fi.uba.ar/7508/OSC9/Abraham%20Silberschatz-Operating%20System%20Concepts%20(9th,2012.12).pdf
first few lines of input textfile(there are 1000 logical adresses in the file.):
16916 62493 30198 53683 40185 28781 24462 48399 64815 18295 12218 22760 57982 27966 54894 38929 32865 64243 2315 64454
Sample output:
Virtual address: 16916 Physical address: 20 Value: 0 Virtual address: 62493 Physical address: 285 Value: 0 Virtual address: 30198 Physical address: 758 Value: 29 Virtual address: 53683 Physical address: 947 Value: 108 Virtual address: 40185 Physical address: 1273 Value: 0 Virtual address: 28781 Physical address: 1389 Value: 0 Virtual address: 24462 Physical address: 1678 Value: 23
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Working code... but output is incorrect. and it does not print the Value and TLB hit rate..
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include
const int PAGE_SIZE = 256; const int VM_SIZE = 256; const int MM_SIZE = 128; const int TLB_SIZE = 16;
int main(int argc, char *argv[]){
int physical_memory[MM_SIZE]; int virtual_memory[VM_SIZE][2]; int tlb[TLB_SIZE][2];
// INITIAL PAGE TABLE FILLING // [0] Physical Address [1] Age for(int i = 0; i < VM_SIZE; i++){ virtual_memory[i][0] = (i > MM_SIZE - 1)? -1 : i; virtual_memory[i][1] = (i > MM_SIZE - 1)? -1 : MM_SIZE - i; } for(int i = 0; i < MM_SIZE; i++){ physical_memory[i] = i; }
for(int i = 0; i < TLB_SIZE; i++){ tlb[i][0] = (i > TLB_SIZE - 1)? -1 : i; tlb[i][1] = (i > TLB_SIZE - 1)? -1 : TLB_SIZE - i; }
// Check to see if correct arguments exist if(argc != 2){ printf("Incorrect Number of Arguments. "); return 1; }
FILE *pFile; pFile = fopen(argv[1], "r");
//checks to see if the .txt file supplied is empty if(pFile == NULL){ printf("Error opening a file %s: %s ", argv[1], strerror(errno)); exit(EXIT_FAILURE); }
char *line = NULL; size_t len = 0; ssize_t read;
int page_number = 0; int physicalAddress = 0; int pageFaultCount = 0;
double pageFaultRate = 0.0;
printf("================================== " ); printf("|| Virtual Memory Manager || " ); printf("================================== " );
printf("Translating 1000 Logical Addresses: ");
while((read = getline(&line, &len, pFile)) != -1){ int offset = atoi(line) & 255; int page = atoi(line) & 65280; int page_table_number = page >> 8; int tlb_hit = 0;
for(int i = 0; i < TLB_SIZE; i++){ if(tlb[i][0] == page_table_number){ tlb_hit = 1; printf("TLB HIT "); break; } }
if(virtual_memory[page_table_number][0] < 0 && !tlb_hit){ pageFaultCount++; srand(time(NULL)); int r = rand();
// EVICT SOMEONE int largest = 0; int evict = 0; for(int i = 0; i < VM_SIZE; i++){ if(virtual_memory[i][1] > largest){ largest = virtual_memory[i][1]; evict = i; } }
int tlb_replacement = r % 15; tlb[tlb_replacement][0] = page_table_number; tlb[tlb_replacement][1] = virtual_memory[evict][0]; virtual_memory[page_table_number][0] = virtual_memory[evict][0]; virtual_memory[page_table_number][1] = 0; virtual_memory[evict][0] = -1; virtual_memory[evict][1] = 0; }
//printing formatting for Virtual Address if (page < 100) { printf("Virtual Address = %d \t", page); } else if (page < 1000) { printf("Virtual Address = %d \t", page); } else { printf("Virtual Address = %d \t", page); }
//calculates the physical address physicalAddress = (physical_memory[virtual_memory[page_table_number][0]] * PAGE_SIZE) + offset;
printf("Physical Address: %d ", physicalAddress);
page_number++;
for(int i = 0; i < VM_SIZE; i++){ virtual_memory[i][1]++; } }
//calculates the % of page faults pageFaultRate = (double) pageFaultCount / 1000 * 100; printf(" Page Fault Rate: %.2f%% ", pageFaultRate);
free(line); fclose(pFile);
exit(EXIT_SUCCESS);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started