Chapter 8: Memory Management

Chapter 8: Memory Management · Study Questions
CacheBase and limitBindingSwappingPlacementBitmapPagingTLBPage-table structuresSegmentation
Sercan KÜLCÜ

Multiple Choice · Introduction

Question 1: Not the Memory Manager’s Job

Which of the following does the operating system’s memory-management component not do?

D: Choosing the next process is the CPU scheduler’s job. Memory management covers free/used tracking, allocation and reclamation, protection, relocation and swapping.

Classic Question · Memory Hardware

Question 2: Two-Level Cache

The L1 cache responds in 1 ns and has a 90% hit rate. On an L1 miss, L2 is consulted: L2 takes 10 ns, and 80% of the requests reaching it hit. On an L2 miss, DRAM is accessed (100 ns). Given that the levels are consulted in order, what is the effective access time? What would happen without L2?

Show answer

Every access goes to L1; 10% of them go on to L2, and 20% of those go down to DRAM:

EAT = 1 + 0.10 × (10 + 0.20 × 100) = 1 + 0.10 × 30 = 4 ns.

Without L2: 1 + 0.10 × 100 = 11 ns. Without a cache every access would take 100 ns; two levels cut the average 25-fold.

Misses dominate the average: cutting the L1 miss rate from 10% to 5% lowers EAT to 1 + 0.05 × 30 = 2.5 ns.

Step-by-Step Trace · Memory Hardware

Question 3: Direct-Mapped Cache

The direct-mapped cache code is run with this address sequence (64 B lines, 4 sets, initially empty). Which accesses are hits? What are the values of hit and miss after the run?

set 0—set 1—set 2—set 3—

64 B lines, 4 sets, direct mapped: block b can go only to set b mod 4.

address 0 → block 0 → set 0set 0block 0missset 1—set 2—set 3—

Set 0 is empty → miss, block 0 is loaded.

address 40 → block 0 → set 0set 0block 0hitset 1—set 2—set 3—

Set 0 already holds block 0 → hit. 40 is in the same 64-byte line as 0: spatial locality.

address 72 → block 1 → set 1set 0block 0set 1block 1missset 2—set 3—

Set 1 is empty → miss, block 1 is loaded.

address 256 → block 4 → set 0set 0block 4missset 1block 1set 2—set 3—

Set 0 holds block 0 → miss; block 4 evicts it (conflict miss).

address 8 → block 0 → set 0set 0block 0missset 1block 1set 2—set 3—

Set 0 holds block 4 → miss; block 0 evicts it (conflict miss).

address 100 → block 1 → set 1set 0block 0set 1block 1hitset 2—set 3—

Set 1 already holds block 1 → hit. 100 is in the same line as 72 (64–127).

address 130 → block 2 → set 2set 0block 0set 1block 1set 2block 2missset 3—

Set 2 is empty → miss, block 2 is loaded.

address 264 → block 4 → set 0set 0block 4missset 1block 1set 2block 2set 3—

Set 0 holds block 0 → miss; block 4 evicts it (conflict miss).

set 0block 4set 1block 1set 2block 2set 3—

Result: 2 hits, 6 misses (25%). Addresses 0, 256 and 264 map to the same set and kept evicting each other; a 2-way set-associative cache would have avoided some of these misses.

int addr[8] = {0, 40, 72, 256, 8, 100, 130, 264};
int tag[4] = {-1, -1, -1, -1};
int hit = 0, miss = 0;
for (int i = 0; i < 8; i++) {
  int block = addr[i] / 64;
  int set = block % 4;
  if (tag[set] == block) hit++;
  else { tag[set] = block; miss++; }
}

Output Question · Address Space

Question 4: Base and Limit

A process’s base (relocation) register is 30000 and its limit register is 12000. The MMU first compares the logical address with the limit, then adds the base. The process accesses logical addresses 0, 5000, 11999 and 12000 in turn. What are the results?

logical< limit?12000+ base30000physicalno → trap
Show answer
Logical< 12000?Result
0yes30000
5000yes35000
11999yes41999
12000notrap

Valid logical addresses are 0 … limit − 1, i.e. 0–11999; since 12000 = limit, a protection fault occurs and the kernel takes over (SIGSEGV on Unix). Because the comparison uses the logical address, the result does not depend on the value of the base.

Multiple Choice · Address Space

Question 5: Binding Time

The operating system can move a running process elsewhere in memory after compaction or swapping. For this to be possible, when must address binding be done?

C: Addresses bound at compile or load time are fixed; they become invalid when the process moves. With execution-time binding every access is translated using the current base or page-table values; moving a process only updates a register or a table.

Multiple Choice · Binding and Loading

Question 6: Lazy Binding

In a dynamically linked program, what changes in memory after printf is called for the first time (lazy binding, ELF)?

B: On the first call the PLT jumps to the resolver via the address in the GOT; ld.so finds the symbol and writes the real address into the GOT entry. Code pages never change, so they stay shareable between processes; later calls follow the PLT → GOT → printf path.

Output Question · Swapping

Question 7: Swap Time

A 200 MB process is written to disk by standard swapping and later brought back. The disk transfer rate is 50 MB/s; latencies are ignored. How long does the round trip take? Why is a process with pending I/O not swapped out?

Show answer

Out 200 / 50 = 4 s, in 4 s: total 8 s. This shows why moving a whole process is unacceptable today; modern systems swap out only cold pages.

If a process is swapped out and another is loaded in its place, the pending DMA transfer writes to the old physical address, i.e. into another process’s memory. Remedy: do not swap such a process, or do the I/O through kernel buffers.

A process swapped back in may be placed at a different address, which requires execution-time binding.

Step-by-Step Trace · Contiguous Allocation

Question 8: Placement Algorithms

The free holes, in address order, are 150, 400, 80, 250, 600 KiB. Requests arrive in this order: 230, 120, 380, 90 KiB. Where do first fit, best fit and worst fit place each request? In which of them does a request fail?

First fit15040080250600Best fit15040080250600Worst fit15040080250600

Holes in address order: 150, 400, 80, 250, 600 KiB. Orange: the hole chosen at that step; the boxes show the remaining size.

First fit15017080250600Best fit1504008020600Worst fit15040080250370

230 K: first fit takes the first hole that is big enough, 400; best fit takes the smallest of those that fit (400, 250, 600), 250 (20 left); worst fit takes 600.

First fit3017080250600Best fit304008020600Worst fit15028080250370

120 K: first fit and best fit both use 150 (30 left). Worst fit uses the current largest hole, 400.

First fit3017080250220Best fit30208020600Worst fit15028080250370

380 K: first fit uses 600, best fit uses 400 (20 left). For worst fit the largest hole is 370: no room, although the total free space is enough.

First fit308080250220Best fit30208020510Worst fit15028080250280

90 K: first fit uses 170 (80 left), best fit uses 600, the only hole that fits, and worst fit uses 370. Only worst fit failed to satisfy a request.

for (each request r) {
  first = first hole with h >= r;
  best  = smallest hole with h >= r;
  worst = largest hole;
  if (chosen h < r) no room;
  else shrink the chosen hole by r;
}

Multiple Choice · Contiguous Allocation

Question 9: The Unsatisfiable Request

In a variable-partition system the free holes are 6, 9 and 4 KiB. Although the total free space is 19 KiB, a 12 KiB request cannot be satisfied. What is this situation called, and what is the direct remedy?

B: There is free space but it is not contiguous: external fragmentation. Compaction shifts the processes to one end and gathers the holes into a single block (this requires execution-time binding); the lasting solution is non-contiguous allocation (paging). Placement algorithms only influence fragmentation; they do not repair an existing situation.

Output Question · Free-Space Management

Question 10: Searching a Bitmap

The bitmap of an 18-unit memory is in the figure (units start at 0, 1 = used). With first fit, from which unit does a 3-unit request start? Where does a following 4-unit request go? Can a 5-unit request be satisfied after that?

1010210401610800101112001400161
Show answer

Zero runs: [2] (1 unit), [4–5] (2), [8–10] (3), [13–16] (4).

1010210401610800101112001400161
  • 3 units: 2 and 4–5 are too small; the first run that fits starts at 8 (units 8–10 fill up).
  • 4 units: the remaining runs are [2], [4–5], [13–16]; only 13 fits.
  • 5 units: only 3 free units remain in total, and the longest run is 2 → cannot be satisfied. Even at the start there was no run longer than 4.

Looking for k units means looking for k consecutive zeros: this linear scan is the bitmap’s weak point.

Multiple Choice · Free-Space Management

Question 11: Bitmap Size

Memory of 512 MiB is tracked in a bitmap with 4 KiB allocation units. How many bytes does the bitmap occupy?

A: The number of units is 229 / 212 = 217; each unit takes 1 bit → 217 / 8 = 214 bytes = 16 KiB. C mistakes the number of bits (128 Ki) for bytes. With 1 KiB units the bitmap would be 4 times larger, and internal fragmentation per unit would shrink.

Multiple Choice · Paging

Question 12: Decimal Address Translation

The page size is 2 KiB. In the page table, page 4 maps to frame 7. What is the physical address of logical address 9000?

C: p = ⌊9000 / 2048⌋ = 4, d = 9000 − 4 · 2048 = 808. Physical = 7 · 2048 + 808 = 15144. B forgets the offset (it gives only the frame start); D adds the entire logical address to the frame start. The offset does not change; only the page number is replaced by the frame number.

Output Question · Paging

Question 13: Hexadecimal Address Translation

A 32-bit virtual address, 4 KiB pages. In the page table, entry 0x3A is valid with frame 0x1C2; entry 0x3B is invalid. (a) How many bits are the page number and the offset? (b) Which physical address does 0x0003A7F4 translate to? (c) What happens when 0x0003B010 is accessed?

Show answer

(a) 4 KiB = 212 → offset 12 bits, page number 32 − 12 = 20 bits.

(b) The last 3 hex digits are the offset: d = 0x7F4, p = 0x0003A. Physical = frame ‖ offset = 0x1C27F4.

(c) p = 0x3B, the entry is invalid → the hardware raises a trap (page fault). The operating system decides whether this is an invalid access or a page that is simply not loaded yet.

In hexadecimal each digit is 4 bits; since the page size is a power of 2, division and remainder become bit shifting and masking.

Multiple Choice · Paging

Question 14: Page-Table Size

32-bit virtual address, 8 KiB pages, 4-byte page-table entries. How much space does a single-level (linear) page table take per process?

D: The number of pages is 232 / 213 = 219 entries; 219 × 4 B = 221 B = 2 MiB. C is the correct value for 4 KiB pages. For 100 processes that is 200 MiB; this is the reason for multi-level tables.

Step-by-Step Trace · TLB

Question 15: TLB Hits and Misses

4 KiB pages, a 3-entry LRU TLB. The addresses 0x1A04, 0x0123, 0x1FFC, 0x2008, 0x4010, 0x0F00, 0x2ABC, 0x6000 are accessed in order. Which are TLB hits? What are the physical addresses?

TLB empty; 3 entries, LRUTLB (old → new):

Page table: 0→5, 1→9, 2→3, 3→7, 4→1, 6→4. The TLB has 3 entries and starts empty; when room is needed, the least recently used entry is evicted.

0x1A04 → p = 1, d = 0xA04miss → 0x9A04TLB (old → new):1 → 9

0x1A04: page 1 is not in the TLB → miss; frame 9 is read from the table.

0x0123 → p = 0, d = 0x123miss → 0x5123TLB (old → new):1 → 90 → 5

0x0123: page 0 is not in the TLB → miss; frame 5 is read from the table.

0x1FFC → p = 1, d = 0xFFChit → 0x9FFCTLB (old → new):0 → 51 → 9

0x1FFC: page 1 is in the TLB → hit, the page table is not consulted.

0x2008 → p = 2, d = 0x008miss → 0x3008TLB (old → new):0 → 51 → 92 → 3

0x2008: page 2 is not in the TLB → miss; frame 3 is read from the table.

0x4010 → p = 4, d = 0x010miss (page 0 evicted) → 0x1010TLB (old → new):1 → 92 → 34 → 1

0x4010: page 4 is not in the TLB → miss; frame 1 is read from the table; the TLB is full, so page 0 is evicted.

0x0F00 → p = 0, d = 0xF00miss (page 1 evicted) → 0x5F00TLB (old → new):2 → 34 → 10 → 5

0x0F00: page 0 is not in the TLB → miss; frame 5 is read from the table; the TLB is full, so page 1 is evicted.

0x2ABC → p = 2, d = 0xABChit → 0x3ABCTLB (old → new):4 → 10 → 52 → 3

0x2ABC: page 2 is in the TLB → hit, the page table is not consulted.

0x6000 → p = 6, d = 0x000miss (page 4 evicted) → 0x4000TLB (old → new):0 → 52 → 36 → 4

0x6000: page 6 is not in the TLB → miss; frame 4 is read from the table; the TLB is full, so page 4 is evicted. Total 2 hits / 8 accesses.

p = addr / 4096;  d = addr % 4096;
if (p is in the TLB) {          /* hit */
  f = TLB[p];  make p the newest;
} else {                 /* miss */
  f = page_table[p];
  if (TLB is full) evict the oldest entry;
  add (p, f) to the TLB;
}
physical = f * 4096 + d;

Output Question · TLB

Question 16: Effective Access Time

Memory access takes 80 ns, a TLB lookup 10 ns, with a single-level page table and a TLB hit ratio of 95%. (a) What is the effective access time? (b) What hit ratio is needed to bring it down to 92 ns? (c) How long would a miss take if the table had four levels?

Show answer

Hit: 10 + 80 = 90 ns. Miss: 10 + 80 (table) + 80 (data) = 170 ns.

(a) EAT = 0.95 · 90 + 0.05 · 170 = 85.5 + 8.5 = 94 ns.

(b) 90α + 170(1 − α) ≤ 92 → 170 − 80α ≤ 92 → α ≥ 0.975: at least 97.5%.

(c) On a miss, 4 table accesses + data: 10 + 4 · 80 + 80 = 410 ns. In a multi-level table a miss is very expensive; that is why hardware also caches the intermediate levels, and the hit ratio matters even more.

Output Question · Page-Table Structures

Question 17: Two-Level Paging

32-bit address, 4 KiB pages, two-level table: 10-bit top index (PT1), 10-bit second index (PT2), 12-bit offset; each table has 1024 entries × 4 B = 4 KiB. (a) What are PT1, PT2 and the offset for 0x01402A9F? (b) A process uses the lowest 12 MiB and the topmost 4 MiB of its address space. How many page tables are needed, and how many KiB in total?

Show answer 0000000101PT1 = 50000000010PT2 = 2101010011111offset = 0xA9F

(a) 0x01402A9F = 0000 0001 0100 0000 0010 1010 1001 1111 → PT1 = 5, PT2 = 2, offset = 0xA9F.

(b) Each second-level table covers 1024 × 4 KiB = 4 MiB: 3 for the lowest 12 MiB, 1 for the top 4 MiB, plus 1 top-level table = 5 tables = 20 KiB. A linear table would be 4 MiB.

Subtables for unused regions are never created. Cost: on a TLB miss, 2 table accesses + 1 data access.

Multiple Choice · Page-Table Structures

Question 18: Inverted Page Table

Which statement is true for an inverted page table?

A: There is a single table in the system, its size is proportional to physical memory and independent of the number of processes; the index of an entry is the frame number. Cost: a hash table is needed for lookup, and since a frame holds only one virtual page, shared memory gets harder. B describes the linear (per-process) table.

Output Question · Segmentation

Question 19: Segment Table

Segment table (base, limit): 0 → (1400, 600), 1 → (6300, 300), 2 → (4100, 1000), 3 → (3200, 500). Logical addresses in (segment, offset) form: (0, 430), (1, 310), (2, 999), (3, 500). Find the physical addresses.

Show answer
AddressOffset < limit?Result
(0, 430)430 < 6001400 + 430 = 1830
(1, 310)310 ≥ 300trap
(2, 999)999 < 10004100 + 999 = 5099
(3, 500)500 ≥ 500trap

Segmentation is a separate base-limit pair for each segment. Valid offsets are 0 … limit − 1; offset = limit is a trap too. Without the check, address (1, 310) would access 6610, outside the segment.

Multiple Choice · Real Architectures

Question 20: Segmentation on x86-64

What is effectively left of segmentation in x86-64 long mode?

B: In long mode the bases of CS, DS, ES, SS are treated as 0 by the hardware and limits are not checked: the address space is flat, and protection is done with page-table bits. Only the FS and GS bases are used: for thread-local storage (TLS) in user space, for per-CPU data in the kernel. CS also carries the privilege level.

The End

Chapter 8: Memory Management: Study Questions
Lecture slides: Chapter 8: Memory Management · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.