Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Program I have a program written here about memory hierachy. Your job is to add a function to the code that will add and

C Program

I have a program written here about memory hierachy. Your job is to add a function to the code that will add and print out the miss rate and hit rate. Test the program after you add the code and make sure the program runs.

#include

#include

#include

/* Global arrays */

int *cache;

int *memory;

/* The methods. */

void display(int *arr, int size);

int diskIni(int size, int index);

void cacheIni(int size,int dsize,int index);

void memIni(int size,int dsize,int index);

bool done();

void missHit(int index, int csize);

void accessTime(int cac, int mem, int d);

/* The main method. */

int main(){

int csize, msize, index, dsize, i, ans;

printf("Enter the disk size: ");

scanf("%d", &dsize);

printf("Enter the cache size: ");

scanf("%d", &csize);

printf("Enter the memory size: ");

scanf("%d", &msize);

/* Checks if disk>memory>cache. */

if(!((dsize > msize) && (msize > csize))){

printf(" Input the disk, memory and cache size again. ");

printf("Disk should be greater than memory and memory should be greater than cache in size. ");

printf("Enter the disk size: ");

scanf("%d", &dsize);

printf("Enter the cache size: ");

scanf("%d", &csize);

printf("Enter the memory size: ");

scanf("%d", &msize);

}

/* Allocates csize and mszie of elements of the size. */

cache = malloc(sizeof(*cache) * csize);

memory = malloc(sizeof(*memory) * msize);

/* Initialize cache and memory. */

printf(" Before: ");

for(i = 0; i < csize; i++){

cache[i] = -1;

}

display(cache,csize);

for(i = 0; i < msize; i++){

memory[i] = -1; //Assume -1 is null

}

display(memory,msize);

/* Loop getting index values and storing in cache and memory*/

i = 0;

do{

printf(" Enter the index to search: ");

scanf("%d", &index);

printf(" Print if miss or hit: ");

missHit(index,csize);

printf(" Cache Output: ");

cacheIni(csize,dsize,index);

printf(" Memory Output: ");

memIni(msize,dsize,index);

printf(" Do you want to end the iteration? If yes enter 1, otherwise enter any number: ");

scanf("%d",&ans);

if(ans == 1){

accessTime(csize,msize,dsize);

break;

}

}while(done());

return 0;

}

/* Calculates the access time. */

void accessTime(int cac, int mem, int d){

int csize, msize, dsize;

printf(" Enter the disk size: ");

scanf("%d", &dsize);

printf("Enter the cache size: ");

scanf("%d", &csize);

printf("Enter the memory size: ");

scanf("%d", &msize);

int aCac = 1 * (csize/cac);

int aMem = 100 * (msize/mem);

int aD = 100000 * (dsize/d);

printf(" Access time for the cache in nanosecond: %d ", aCac);

printf("Access time for the memory in nanosecond: %d ", aMem);

printf("Access time for the disk in nanosecond: %d ", aD);

}

/* Check for miss or hit. */

void missHit(int index, int csize){

int i; int count = 0;

for(i = 0; i < csize; i++){

if(cache[i] == index)

count++;

}

if(count != 0)

printf("Hit! ");

else

printf("Miss ");

}

void memIni(int size,int dsize,int index){

int j;

int copyValue = index % size;

printf("Before: ");

display(memory,size);

printf("After: ");

for(j = 0; j < size; j++){

if(j == copyValue)

memory[j] = diskIni(dsize, index);//-1 is NULL

}

display(memory,size);

}

void cacheIni(int size,int dsize,int index){

int j;

int copyValue = index % size;

printf("Before: ");

display(cache,size);

printf("After: ");

for(j = 0; j < size; j++){

if(j == copyValue)

cache[j] = diskIni(dsize, index);//-1 is NULL

}

display(cache,size);

}

/* Prints out the array. */

void display(int *arr, int size) {

int i;

printf(" Index: ");

for (i = 0; i < size; i++) {

printf("%3d ",i);

}

printf(" Value: ");

for (i = 0; i < size; i++) {

printf("%3d ", arr[i]);

}

printf(" ---------------------------------------------------- ");

}

/* Returns the value at index so that it can be copied into cache and memory. */

int diskIni(int size, int index){

int d[size];

int i;

for(i = 0; i < size; i++){

d[i] = i;

}

return d[index];

}

bool done(){

return true;

}

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

Objects And Databases International Symposium Sophia Antipolis France June 13 2000 Revised Papers Lncs 1944

Authors: Klaus R. Dittrich ,Giovanna Guerrini ,Isabella Merlo ,Marta Oliva ,M. Elena Rodriguez

2001st Edition

3540416641, 978-3540416647

More Books

Students also viewed these Databases questions

Question

2. How much time should be allocated to the focus group?

Answered: 1 week ago

Question

1. Where will you recommend that she hold the focus group?

Answered: 1 week ago