C program Make sure it runs please and well documented You already have got some
ID: 3871450 • Letter: C
Question
C program Make sure it runs please and well documented
You already have got some idea about Memory Hierarchy. In the memory hierarchy if the processoricpu needs some data, it searches the data in the following way: Cache->Memory->Disk In this assignment you have to implement some kind of demo memory hierarchy using c code Assume that you have cache, memory and disk of size 4 blocks, 16 blocks and 256 blocks respectively. Each block of the cache, memory and disk contains 4 bytes of data (which is equivalent of the size of one integer). So, whenever processor requests for some data, the transfer is made in blocks To incorporate the scenario into your demo memory hierarchy code, you can define your cache, memory and disk in terms of array. Say for example: Int cachel4] Int memory[16]: Int disk[2561 Initially only the disk would have some value. The value of each array would be: disk: Value 01 2 3 4 5 6 7 8 9 1011 12255 memory Index 0 1 2 3 4 5 6 7 8 910 11 12 13 14 15 Value cache IndexExplanation / Answer
main() {
int cache[4],memory[16], disk[256], indexcache,indexMem,k,n, hit=0, totalInput=0,avgAccessTime=0;
char inp = ‘Y’; // Assuming atleast one input wil be there
// Initialize disk with default values
for (k=0; k<256; k++)
disk[k] = k;
while(inp = = ‘Y’ || inp == ‘y’)
{
printf(“Enter the data to be searched ……………!! ”);
scanf(“%d”, &n)
// Search in cache
Printf(“Searching in cache…………….!! ”);
indexCache = n%4;
If(cache[indexCache] == n)
{
printf (“It is a cache hit!!”);
hit++;
avgAccessTime = avgAccessTime+1; // time to access cache
goto NEXT;
}
indexMem = n%16
if(memory[indexMem] == n)
{
printf (“It is in memory!!”);
memHit++;
avgAccessTime = avgAccessTime +1 + 100; // time to access cache plus memory
goto NEXT;
}
If(disk[n] = = n)
{
Printf (“It is in disk!!”);
diskHit++:
avgAccessTime = avgAccessTime +1 +100 + 100000; // time to access cache plus memory plus disk
cache[indexCache] = disk[n];
memory[indexMem] = disk[n];
goto NEXT;
}
else
{
Printf(“DATA UNAVAILABLE!!!”);
avgAccessTime = avgAccessTime +1 +100 + 100000;
}
NEXT:
totalInput++;
printf(“Contents of cache:”);
for(i=0; i<4;i++)
printf (“%d %d”, i, cache[i]);
printf(“Contents of memory:”);
for(j=0; j<16;j++)
printf (“%d %d”, j, memory[j]);
Printf(“Do you want to search more…… ”);
Scranf(“%c”, &inp);
}
Printf(“HIT RATE: %d”, hit);
Printf(“MISS RATE: %d”, (totalInput – hit));
Printf(“Average Access Time: %d nanoseconds”, avgAccessTime);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.