Virtual Memory Manager code, not calculating the physical address correctly

68 Views Asked by At

I am working on a school project. The project is to implement a virtual memory manager with TLB in C.The input file is list of decimal numbers that are treated as 16-bit inputs with the fist 8-bits being the offset, and the last 8-bits being the page number. My groups current revision on the code is able to correctly read the virtual address and offset, however does not calculate the physical address or value. The value is gotten from a BACKING_STORE.bin file. Prior revisions of the code did retrieve the right value. Here is the current code:

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

// Define global variables for logical address, physical address, and signed byte value
int* logical_address;
int physical_address;
char signed_byte_value; // extracted from BACKING_STORE.bin
int index_;

// Define constants for page size, page table size, TLB size, and physical memory size
#define PAGE_SIZE 256
#define PAGE_TABLE_SIZE 256
#define TLB_SIZE 16
#define PHYSICAL_MEMORY_SIZE 256

// Define structures for page table and TLB
typedef struct {
    int page_num;
    int frame_num;
    int valid;
} PageTableEntry;

typedef struct {
    int page_num;
    int frame_num;
    int valid;
} TLBEntry;

// Declare global arrays for page table, TLB, and physical memory
PageTableEntry page_table[PAGE_TABLE_SIZE];
TLBEntry TLB[TLB_SIZE];
char physical_memory[PHYSICAL_MEMORY_SIZE][PAGE_SIZE];

// Declare global counters for page faults and TLB hits
int page_faults = 0;
int TLB_hits = 0;
int num_of_addresses = 0;

// Function to read data from addresses.txt
void read_data() {
    FILE* addresses_file = fopen("addresses.txt", "r"); // Open addresses.txt in read mode

    if (addresses_file == NULL) {
        printf("Error opening addresses.txt file.\n");
        exit(1);
    }
    logical_address = (int*)malloc(sizeof(int)*1000);
    while(!feof(addresses_file)){
        // Read logical_address from addresses.txt
        fscanf(addresses_file, "%d", &logical_address[num_of_addresses]);
        num_of_addresses++;
    }

    fclose(addresses_file); // Close addresses.txt file
}

// Function to write data to output files (out1.txt, out2.txt, and out3.txt)
void write_data() {
    FILE* out1_file = fopen("output.txt", "a"); // Open out1.txt in append mode
    

    if (out1_file == NULL) {
        printf("Error opening output files.\n");
        exit(1);
    }

    // Write logical_address, physical_address, and signed_byte_value to out1.txt, out2.txt, and out3.txt
    fprintf(out1_file, "Logical Address: %d  Physical Address: %d  Value: %d\n", logical_address[index_], physical_address, signed_byte_value);

    fclose(out1_file); // Close out1.txt file
    
}

int main() {
    index_ = 0;
    // Initialize page table
    for (int i = 0; i < PAGE_TABLE_SIZE; i++) {
        page_table[i].page_num = i;
        page_table[i].frame_num = -1;
        page_table[i].valid = 0;
    }

    // Initialize TLB
    for (int i = 0; i < TLB_SIZE; i++) {
        TLB[i].page_num = -1;
        TLB[i].frame_num = -1;
    }

    // Read addresses from addresses.txt using read_data() function
    read_data();
    int TLB_index = 0;
    int frame_num;

    // Loop through each logical address
for (int j = 0; j < num_of_addresses; j++) {
    // Extract page number and offset from logical address
    int page_num = (logical_address[j] >> 8) & 0xFF;
    int offset = logical_address[index_] & 0xFF;

    // Search TLB for page number
    int found_in_TLB = 0;
    for (int i = 0; i < TLB_SIZE; i++) {
        if (TLB[i].page_num == page_num) {
            frame_num = TLB[i].frame_num;
            found_in_TLB = 1;
            TLB_index = i;
            TLB_hits++;
            break;
        }
    }

    // If page number is not found in TLB, search page table
    if (frame_num == -1) {
        // Check if page number is valid in page table
        if (page_table[page_num].valid == 1) {
            frame_num = page_table[page_num].frame_num;
        } else {
            // Page fault occurred, update page table and physical memory
            page_faults++;

            // Read page data from BACKING_STORE.bin
            FILE* backing_store = fopen("BACKING_STORE.bin", "rb");
            if (backing_store == NULL) {
                printf("Error opening BACKING_STORE.bin file.\n");
                exit(1);
            }

            // Seek to the appropriate page in BACKING_STORE.bin
            fseek(backing_store, page_num * PAGE_SIZE, SEEK_SET);

            // Read page data from BACKING_STORE.bin to physical memory
            fread(physical_memory[PHYSICAL_MEMORY_SIZE - 1], sizeof(char), PAGE_SIZE, backing_store);

            // Update page table and frame number
            frame_num++;
            page_table[page_num].frame_num = frame_num;
            page_table[page_num].valid = 1;

            // Update TLB
            TLB[TLB_index].page_num = page_num;
            TLB[TLB_index].frame_num = frame_num;
        }
    }

    // Calculate physical address using page number and offset
    physical_address = (frame_num * PAGE_SIZE) + offset;

    // Get signed byte value from physical memory
    signed_byte_value = physical_memory[frame_num][offset];

    // Write data to output files
    write_data();

    // Update TLB with most recently used page
    if (frame_num != -1) {
        TLB[TLB_index].page_num = page_num;
        TLB[TLB_index].frame_num = frame_num;
        TLB[TLB_index].valid = 1;
    }
    printf("Offset:%d\n", offset);
    // Increment logical address index
    index_++;
}

    printf("Page Faults: %d\n", page_faults);
    printf("TLB Hits: %d\n", TLB_hits);

    return 0;
}

Here is a sample of the input:

16916
62493
30198
53683
40185
28781
24462
48399
64815
18295
12218
22760
57982
27966
54894
38929

Here is a sample of the expected 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
Virtual address: 48399 Physical address: 1807 Value: 67
Virtual address: 64815 Physical address: 2095 Value: 75
Virtual address: 18295 Physical address: 2423 Value: -35
Virtual address: 12218 Physical address: 2746 Value: 11
Virtual address: 22760 Physical address: 3048 Value: 0
Virtual address: 57982 Physical address: 3198 Value: 56
Virtual address: 27966 Physical address: 3390 Value: 27
Virtual address: 54894 Physical address: 3694 Value: 53
Virtual address: 38929 Physical address: 3857 Value: 0

Here is a sample of the actual output:

Logical Address: 16916  Physical Address: 20  Value: 0
Logical Address: 62493  Physical Address: 29  Value: 0
Logical Address: 30198  Physical Address: 246  Value: 0
Logical Address: 53683  Physical Address: 179  Value: 0
Logical Address: 40185  Physical Address: 249  Value: 0
Logical Address: 28781  Physical Address: 109  Value: 0
Logical Address: 24462  Physical Address: 142  Value: 0
Logical Address: 48399  Physical Address: 15  Value: 0
Logical Address: 64815  Physical Address: 47  Value: 0
Logical Address: 18295  Physical Address: 119  Value: 0
Logical Address: 12218  Physical Address: 186  Value: 0
Logical Address: 22760  Physical Address: 232  Value: 0
Logical Address: 57982  Physical Address: 126  Value: 0
Logical Address: 27966  Physical Address: 62  Value: 0
Logical Address: 54894  Physical Address: 110  Value: 0
Logical Address: 38929  Physical Address: 17  Value: 0
Logical Address: 32865  Physical Address: 97  Value: 0

Thank you for time and assistance.

0

There are 0 best solutions below