Consider the following two-dimensional array in the programming language, which is stored in row-major order:
int D[128][128];
Demand paging is used for allocating memory and each physical page frame holds elements of the array The Least Recently Used page-replacement policy is used by the operating system. A total of physical page frames are allocated to a process which executes the following code snippet:
for (int i = 0; i < 128; i++)
for (int j = 0; j < 128; j++)
D[j][i] *= 10;
The number of page faults generated during the execution of this code snippet is _______________.
View worked solution
Answer: 4096
To find the total number of page faults, let us analyze the array layout, page allocation, and the memory access pattern.
1. Array Layout and Page Mapping:
- The array
D[128][128]is stored in row-major order and contains integer elements. - Each physical page frame holds elements.
- Number of rows per page = rows.
- Thus, the array is divided into pages:
- Page contains rows to .
- Page contains rows to .
- Page contains rows to .
2. Memory Access Pattern:
The nested loops access the array in column-major order: D[j][i].
For any fixed column , as goes from to , the pages are accessed in the following sequence: This means for each of the columns, the program accesses pages in sequence.
3. Page Fault Analysis (LRU with 30 Frames):
- For the first column (), pages to are loaded into the available frames, causing page faults.
- When page is accessed, it replaces page (LRU). When page is accessed, it replaces page . This results in page faults for the first column.
- At the start of the second column (), page is accessed, which is no longer in memory. It is loaded, replacing page . Next, page is accessed and loaded, replacing page .
- Because the working set of pages is larger than the allocated frames, this cyclic access pattern causes thrashing. Every single distinct page access in every column results in a page fault.
Thus, for each of the columns, there are exactly page faults.
Free practice · Your answers on this page are not saved.
