A big page table is split in two. The top bits of an address pick an entry in the outer table, which points to an inner table; the next bits pick an entry in that, which holds the frame. Inner tables that nothing uses simply do not exist.
What to build
Translate 16-bit addresses with a two-level page table.
Requirements
- An address has 16 bits: the top 4 bits are the outer index, the next 4 bits the inner index, the low 8 bits the offset (pages are 256 bytes).
- The outer table has 16 entries; each is
-1(no inner table) or the number of an inner table. Each inner table has 16 entries; each is-1(page not in memory) or a frame number. - For each address print
no inner tableif the outer entry is-1,page faultif the inner entry is-1, otherwise the physical addressframe * 256 + offset.
Input
The first line has t (1 ≤ t ≤ 4), the number of inner tables. The second line has the 16 outer entries. Then t lines of 16 inner entries each. Then a line with q (1 ≤ q ≤ 30) and a line with q addresses (0 to 65535).
Output
One line per address as described.
Constraints
Frame numbers are from 0 to 255.
Example
Input:
1
0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
3 -1 7 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
4
0 300 512 4100
Output:
768
page fault
1792
no inner table
Why it works
Each level is a table lookup that uses a few address bits, so the hardware does two memory reads instead of one. The saving is in space: with 16 outer entries and only the inner tables a process really uses, a mostly empty address space costs a few small tables, not one huge one.
Examples
1 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3 -1 7 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 4 0 300 512 4100
768 page fault 1792 no inner table
2 0 -1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 200 3 255 8192 65535
255 page fault no inner table
Hints
Hint 1
With 16-bit addresses, outer = address / 4096, inner = (address / 256) % 16, offset = address % 256.
Hint 2
outer_table[outer] is -1 or the number of an inner table. Then inner[table][inner] is -1 or a frame.
Hint 3
The physical address is frame * 256 + offset.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
int main(void) {
int t, outer[16], inner[4][16], q, a;
scanf("%d", &t);
for (int i = 0; i < 16; i++) scanf("%d", &outer[i]);
for (int k = 0; k < t; k++)
for (int i = 0; i < 16; i++) scanf("%d", &inner[k][i]);
scanf("%d", &q);
for (int i = 0; i < q; i++) {
scanf("%d", &a);
int o = a / 4096, x = (a / 256) % 16, offset = a % 256;
if (outer[o] < 0) {
printf("no inner table\n");
} else if (inner[outer[o]][x] < 0) {
printf("page fault\n");
} else {
printf("%d\n", inner[outer[o]][x] * 256 + offset);
}
}
return 0;
}
▶ Open this solution in the compiler, then press Visualize to watch it run step by step