slightly change this c program but make it do the same exact thing . #include <stdio.h>…
- User
slightly change this c program but make it do the same exact thing .
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <fcntl.h> #include <unistd.h> #include <sys/types.h> #include <sys/mman.h> #include <sys/stat.h> #include <errno.h>
/* Global definitions. */ #define PAGE_SIZE 256 // Page size, in bytes. #define PAGE_ENTRIES 256 // Max page table entries. #define PAGE_NUM_BITS 8 // Page number size, in bits. #define FRAME_SIZE 256 // Frame size, in bytes. #define FRAME_ENTRIES 256 // Number of frames in physical memory. #define MEM_SIZE (FRAME_SIZE * FRAME_ENTRIES) // Mem size, in bytes. #define TLB_ENTRIES 16 // Max TLB entries.
/* Global variables. */ int virtual; int page_number; int offset; int physical; int frame_number; int value; int page_table[PAGE_ENTRIES]; int tlb[TLB_ENTRIES][2]; int tlb_front = -1; int tlb_back = -1; char memory[MEM_SIZE]; int mem_index = 0;
/* Statistics variables. */ int fault_counter = 0; int tlb_counter = 0; int address_counter = 0; float fault_rate; float tlb_rate;
/* Functions declarations. */ int get_physical(int virtual); int get_offset(int virtual); int get_page_number(int virtual); void initialize_page_table(int n); void initialize_tlb(int n); int consult_page_table(int page_number); int consult_tlb(int page_number); void update_tlb(int page_number, int frame_number); int get_frame();
int main(int argc, char argv[]) { / File I/O variables. / char in_file; char* out_file; char* store_file; char* store_data; int store_fd; char line[8]; FILE* in_ptr; FILE* out_ptr;
/* Initialize page_table, set all elements to -1. */ initialize_page_table(-1); initialize_tlb(-1);
/* Get command line arguments. */ if (argc != 4) { printf("Enter input, output, and store file names!");
exit(EXIT_FAILURE); } /* Else, proceed execution. / else { / Get the file names from argv[]. */ in_file = argv[1]; out_file = argv[2]; store_file = argv[3];
/* Open the address file. */ if ((in_ptr = fopen(in_file, "r")) == NULL) {
printf("Input file could not be opened.\n");
exit(EXIT_FAILURE); }
/* Open the output file. / if ((out_ptr = fopen(out_file, "a")) == NULL) { / If fopen fails, print error and exit. */ printf("Output file could not be opened.\n");
exit(EXIT_FAILURE); }
/* store file / / Map the store file to memory. */ store_fd = open(store_file, O_RDONLY);
store_data = mmap(0, MEM_SIZE, PROT_READ, MAP_SHARED, store_fd, 0); /* Check that the mmap call succeeded. */ if (store_data == MAP_FAILED) { close(store_fd); printf("Error mmapping the backing store file!"); exit(EXIT_FAILURE); }
/* Loop through the input file one line at a time. / while (fgets(line, sizeof(line), in_ptr)) { / Read a single address from file, assign to virtual. Increment address counter. */ address_counter++;
- Assistant