IAE Academy

GATE 2023 CS Q.57: solution and answer

GATE 2023 · CSQ.57 · 2 marks · NAT

Consider the following two-dimensional array D 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 D. 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 ______.

See the question in the paper

Answer and solution

ANSWER

The code generates 4096 page faults.

Official answer: 4096Source: Official answer key, GATE 2023 CS (IIT Kanpur)

  1. Each page frame holds elements and a row has elements, so one page holds rows. The array occupies pages (taking D to start at a page boundary): page holds rows to .

  2. In D[j][i] the inner loop changes j, the row index. For a fixed i the references go down a column, through rows .

  3. Rows to are on page , so one pass over j references pages . The repeated references inside a block of 4 rows are on a page that was just used, so they are hits.

  4. Every pass over j therefore touches the pages in the same cyclic order , and the next value of i starts again at page .

  5. LRU with frames on a cyclic reference of distinct pages: the first pages to fill the frames. Page evicts page and page evicts page , so the frames hold . The next pass asks for page , which is a fault and evicts the least recently used page, page . Page is then also absent (fault, evicts page ), and so on. The page that LRU evicts is always the one needed soonest, so every change of page is a fault.

  6. Faults per value of i , one for each page; the other references in the pass are hits.

  7. Total faults .

KEY IDEA

The inner loop walks down a column of a row-major array, so it visits all 32 pages in a fixed cyclic order. LRU with fewer frames (30) than pages (32) on a cyclic reference faults on every page change.

WATCH OUT FOR

Assuming the inner loop reads row by row, which would give only 32 faults in total. Another slip is assuming that 30 of 32 frames means most pages stay resident, when LRU on a cycle evicts exactly the page needed next.

From the GATE 2023 CS paper. Every question of this paper · Download the full paper · All 6 Virtual memory questions, with notes

More Virtual memory questions