Question
Consider the following two-dimensional array in the C 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 512 elements of the array . The Least Recently Used (LRU) page-replacement policy is used by the operating system. A total of 30 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 _______.
Official answer and solution
- Official answer
- 4096
- From
- Official GATE 2023 answer key
Solution
Trace the memory layout of and the column-major access pattern against 30 page frames under LRU.
- Array size: integer elements stored in row-major order.
- Page size: each page frame holds elements, which corresponds to exactly rows of array .
- Total pages needed by : pages ( to ).
- Page contains rows .
- Memory allocation: 30 physical page frames are allocated ().
- Code access pattern:
The outer loop runs for to (columns), and the inner loop runs for to (rows), accessing in column-major order. - In each column pass (fixed ):
- As increases from to , it accesses row , then (all on page ), then rows (page ), , up to rows (page ).
- Thus, every column pass accesses all 32 pages in sequence: .
- LRU cache behavior:
Because the working set is 32 pages and the frame buffer holds only 30 pages, by the time and are accessed, and have been evicted from memory. When the next column pass begins at , it misses, evicting the next oldest page. Thus, every single page access in every column pass results in a page fault! - Total page faults:
Each column pass accesses 32 pages, generating 32 page faults. With 128 column iterations:
4096
Assuming only the first column faults and subsequent columns hit, missing that LRU thrashes completely because .
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2023 Computer Science and Information Technology (CS)
- Question
- 57 of 65
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.