Skip to content
Official PYQ · 2023

GATE CS 2023 Q47 — Memory Management and Virtual Memory

Explain with AI

Consider the following two-dimensional array D\text{D} in the C\text{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 512512 elements of the array D.\text{D}. The Least Recently Used (LRU)\text{(LRU)} page-replacement policy is used by the operating system. A total of 30\text{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 _______________.

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 128×128=16,384128 \times 128 = 16,384 integer elements.
  • Each physical page frame holds 512512 elements.
  • Number of rows per page = 512 elements128 elements/row=4\frac{512 \text{ elements}}{128 \text{ elements/row}} = 4 rows.
  • Thus, the array is divided into 3232 pages:
    • Page 00 contains rows 00 to 33.
    • Page 11 contains rows 44 to 77.
    • …\dots
    • Page 3131 contains rows 124124 to 127127.

2. Memory Access Pattern:

The nested loops access the array in column-major order: D[j][i].

For any fixed column ii, as jj goes from 00 to 127127, the pages are accessed in the following sequence: 0,0,0,0⏟j=0..3,1,1,1,1⏟j=4..7,…,31,31,31,31⏟j=124..127\underbrace{0, 0, 0, 0}_{j=0..3}, \underbrace{1, 1, 1, 1}_{j=4..7}, \dots, \underbrace{31, 31, 31, 31}_{j=124..127} This means for each of the 128128 columns, the program accesses pages 0,1,2,…,310, 1, 2, \dots, 31 in sequence.

3. Page Fault Analysis (LRU with 30 Frames):

  • For the first column (i=0i = 0), pages 00 to 2929 are loaded into the 3030 available frames, causing 3030 page faults.
  • When page 3030 is accessed, it replaces page 00 (LRU). When page 3131 is accessed, it replaces page 11. This results in 3232 page faults for the first column.
  • At the start of the second column (i=1i = 1), page 00 is accessed, which is no longer in memory. It is loaded, replacing page 22. Next, page 11 is accessed and loaded, replacing page 33.
  • Because the working set of 3232 pages is larger than the 3030 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 128128 columns, there are exactly 3232 page faults.

Total Page Faults=128×32=4096\text{Total Page Faults} = 128 \times 32 = 4096

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Memory Management and Virtual Memory questions