Reputation: 95
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?
Upvotes: 4
Views: 1818
Reputation: 7249
int array[3][3] = {{0, 1, 2},
{3, 4, 5},
{6, 7, 8}};
The layout of this array in memory is [0, 1, 2, 3, 4, 5, 6, 7, 8]
which is
array[0][0]
array[0][1]
array[0][2]
array[1][0]
array[1][1]
array[1][2]
array[2][0]
array[2][1]
array[2][2]
Here the memory address difference between array[1][0]
and array[2][0]
is 3;
So given an array a[18][1024]
. The difference between a[i][j]
and a[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).
Upvotes: 5