Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

convert this code to java language #include #include #include #include #include //define the constant variables of the memory size const int PAGE_SIZE = 256; const

convert this code to java language

#include

#include

#include

#include

#include

//define the constant variables of the memory size

const int PAGE_SIZE = 256;

const int VIRTUALMEMORY_SIZE = 256;

const int MAINMEMORY_SIZE = 128;

const int TLB_SIZE = 16;

//define the main() function takes the input

//file as command line argument.

int main(int argc, char *argv[]) {

//declare the array for physical Addresses

int totalPhysicalAddresses[128];

//declare the 2D-array for virtual Addresses

int virtualAddresses[256][2];

//declare the 2D-array for TLB Addresses

int TLB_Addresses[16][2];

//declare a file object

FILE *fptr;

//open the file in the read mode

fptr = fopen(argv[1], "r");

//if the file not open properly then prompt the error message.

if (fptr == NULL) {

printf("Error opening a file %s: %s ", argv[1], strerror(errno));

exit(EXIT_FAILURE);

}

//Iterate over the size of the virtual memory using for loop

//initialize the page tables of the virtualAddresses

for (int i = 0; i < VIRTUALMEMORY_SIZE; i++) {

if (i > MAINMEMORY_SIZE - 1)

{

virtualAddresses[i][0] = -1;

virtualAddresses[i][1] = -1;

}

else

{

virtualAddresses[i][0] = i;

virtualAddresses[i][1] = MAINMEMORY_SIZE - i;

}

}

//Iterate over the size of the main memory using for loop

//initialize the page tables of the totalPhysicalAddresses

for (int i = 0; i < MAINMEMORY_SIZE; i++) {

totalPhysicalAddresses[i] = i;

}

//Iterate over the size of the TBL memory using for loop

//initialize the page tables of the TLB Addresses

for (int i = 0; i < TLB_SIZE; i++) {

if (i > TLB_SIZE - 1)

{

TLB_Addresses[i][0] = -1;

TLB_Addresses[i][1] = -1;

}

else

{

TLB_Addresses[i][0] = i;

TLB_Addresses[i][1] = TLB_SIZE - i;

}

}

// Check to see if correct arguments exist

if (argc != 2) {

printf("Incorrect Number of Arguments. ");

return 1;

}

//decalre the variables

char *line = NULL;

size_t len = 0;

int page_number = 0;

int totalPhysicalAddress = 0;

int pageFaultCount = 0;

double pageFaultRate = 0.0;

int offset;

int page;

int page_table_number;

int TLB_hit = 0;

printf("\t\tVirtual Memory Manager ");

printf("Translating 1000 Logical Addresses: ");

//read the file line by line

while (fgets(line, len, fptr) != NULL) {

//get offset by using bitwise operator & with 255

//and atoi is used to convert string to int.

offset = atoi(line) & 255;

//get page by using bitwise operator & with 65280

page = atoi(line) & 65280;

//get page_table_number by using right shift with 8

page_table_number = page >> 8;

TLB_hit = 0;

//check if there is TLB HIT.

for (int i = 0; i < TLB_SIZE; i++) {

if (TLB_Addresses[i][0] == page_table_number) {

TLB_hit = 1;

printf("TLB HIT ");

break;

}

}

//check if the there is no TLB_hit and virtualAddresses is less than 0

if (virtualAddresses[page_table_number][0] < 0 && !TLB_hit) {

pageFaultCount++;

//generate a random number

srand(time(NULL));

int r = rand();

int largest = 0;

int temp = 0;

//find the largest virtual Addresses

for (int i = 0; i < VIRTUALMEMORY_SIZE; i++) {

if (virtualAddresses[i][1] > largest) {

largest = virtualAddresses[i][1];

temp = i;

}

}

//find the TLB_replacement

int TLB_replacement = r % 15;

//assign the page_table_number to the TLB_Addresses

TLB_Addresses[TLB_replacement][0] = page_table_number;

//assign the virtualAddresses to the TLB_Addresses

TLB_Addresses[TLB_replacement][1] = virtualAddresses[temp][0];

//assign the values in the virtualAddresses

virtualAddresses[page_table_number][0] = virtualAddresses[temp][0];

virtualAddresses[page_table_number][1] = 0;

virtualAddresses[temp][0] = -1;

virtualAddresses[temp][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);

}*/

printf("Virtual Address = %d \t", page);

//calculates the physical address

totalPhysicalAddress = (totalPhysicalAddresses[virtualAddresses[page_table_number][0]] * PAGE_SIZE) + offset;

//print the physical address

printf("Physical Address: %d ", totalPhysicalAddress);

page_number++;

//Iterate over the size of the virtual memory using for loop

//initialize the page tables of the virtualAddresses

for (int i = 0; i < VIRTUALMEMORY_SIZE; i++) {

virtualAddresses[i][1]++;

}

}

//calculates the percentage of page faults

pageFaultRate = (double)pageFaultCount / 1000 * 100;

//print the percentage of page faults

printf(" Page Fault Rate: %.2f%% ", pageFaultRate);

free(line);

fclose(fptr);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Main Memory Database Systems

Authors: Frans Faerber, Alfons Kemper, Per-Åke Alfons

1st Edition

1680833243, 978-1680833249

More Books

Students also viewed these Databases questions