Operating systems assignment Consider the following twodimen
Operating systems assignment.
Consider the following two-dimensional array: int X[32][32];
Suppose that a system has four page frames and each frame is 128 words (an integer occupies one word). Programs that manipulate the X array fit into exactly one page and always occupy page 0. The data are swapped in and out of the other three frames. The X array is stored in row-major order (i.e., X[0][1] follows X[0][0] in memory). Which of the two code fragments shown below will generate the lowest number of page faults? Explain and compute the total number of page faults.
Fragment A
for (int j = 0; j < 32; j++)
for (int i = 0; i < 32; i++) X[i][j] = 0;
Fragment B
for (int i = 0; i < 32; i++)
for (int j = 0; j < 32; j++) X[i][j] = 0;
Solution
In the above two code fragments shown Fragment B will generate the lowest number of page faults.
Explanation:
Fragment B since the code has more spatial locality than Fragment A. The inner loop causes only one page fault for every other iteration of the outer loop. There will only be 32 page faults.
Fragment A: Since a frame is 128 words, one row of the X array occupies 1/4 of a page (i.e., 32 words). The entire array fits into 32 × 32/128 = 8 frames. The inner loop of the code steps through consecutive rows of X for a given column. Thus every other reference to X [i ][j ] will cause a page fault.
The total number of page faults will be 32 × 32/2 = 512.
![Operating systems assignment. Consider the following two-dimensional array: int X[32][32]; Suppose that a system has four page frames and each frame is 128 word Operating systems assignment. Consider the following two-dimensional array: int X[32][32]; Suppose that a system has four page frames and each frame is 128 word](/WebImages/13/operating-systems-assignment-consider-the-following-twodimen-1016933-1761525544-0.webp)