It is not a homework question. It came in my semester exam today.
This code fragment computes the average of each table colum t[i][j] 0<=i<18 ; 0<=j<1024
for (j = 0; j < 1024; i++) {
temp = 0;
for (i = 0; i < 18; i++) {
temp += temp + t[i][j];
}
cout << temp/18;
}
Variables are 32-bit floating point values.
Variables i, j, temp are stored in processor register(so no need of memory reference to access temp. Main memory is word addressable and paged containing 17 frames, each of size 1024 words and one word is 4 bytes. Page replacement policy is LRU.
Determine the number of page faults to execute the given program fragment? Ans: 18432
How to compute it?
The layout of this array in memory is
[0, 1, 2, 3, 4, 5, 6, 7, 8]which isHere the memory address difference between
array[1][0]andarray[2][0]is 3;So given an array
a[18][1024]. The difference betweena[i][j]anda[i+1][j]is 1024 bytes(The size of a page fault). So, each time your inner loop triggers that causes a page fault. Your inner loop triggers 18*1024 times(18432).