Segments
A program uses three pieces of memory: code, heap and stack, with an unused gap in between. Base and bounds moves the whole thing, gap and all. Segmentation places each piece on its own, so the gap costs nothing.
The MMU, the part of the CPU that translates addresses, keeps one base and one size per segment, plus which way it grows. The kernel writes these registers before it runs the program. Only the kernel may change them.
Translating addresses
Address 100 starts with bits 00, so it is in the code segment, at offset 100. The MMU checks 100 is below the size, 2 KB, then adds the base, 32 KB: physical address 32868.
Address 4200 starts with bits 01: the heap. The offset is what is left, 4200 minus 4096, which is 104. It is below 3 KB, so the MMU adds the heap's base, 34 KB: physical address 34920.
Address 15360 starts with 11: the stack, offset 3072. The stack grows down, so the MMU counts back from the top of a 4 KB segment: 3072 minus 4096 is -1024. Base 28 KB minus 1 KB: physical 27648.
Address 7168 starts with 01: the heap, offset 3072. But the heap is only 3 KB, so offsets stop at 3071. The MMU refuses the read and traps to the kernel, which ends the program. This is a segmentation fault.
Managing free space
Our program has ended, so the kernel takes back its segments. Code and heap sat side by side and become one 5 KB hole; the stack leaves a 2 KB hole. The kernel keeps every hole in a list, the free list.
A new 8 KB segment arrives. 16 KB is free, but the biggest hole is 6 KB, and a segment must be one unbroken piece. So the request must wait. This waste between used blocks is called external fragmentation.
A 4 KB segment arrives. First fit would take the first hole big enough, at 16K. Best fit takes the smallest hole that fits: 5 KB at 32K. The kernel splits it: 4 KB used, 1 KB stays free.
Program B ends. Its 4 KB sits between the 6 KB hole and the 2 KB hole. The kernel checks the neighbours of every freed block and merges free ones, so three small holes become one 12 KB hole. This is coalescing.
The waiting 8 KB segment tries again. The 12 KB hole is the only one big enough, so it goes at 16K and 4 KB stays free. The kernel writes the new base and size into that program's segment table.
What happens
;; step 1/11 · pieces virtual address space (16 KB) physical memory (64 KB) 0K- 2K code 0K-16K kernel 2K- 4K unused 26K-28K stack 4K- 7K heap 32K-34K code 7K-14K unused gap 34K-37K heap 14K-16K stack base and bounds: all 16 KB in one block, gaps included segmentation: only the 7 KB in use takes memory
The segment table, the four addresses and their translations are the worked example from OSTEP chapter 16: a pretend 16 KB address space with 14-bit addresses in a 64 KB machine. Programs B and C and the two new segments are added for the free-space part. Today x86-64 and RISC-V systems manage memory with paging, the next lesson, which uses fixed-size pages so free space is just a list of equal pieces. On x86 segmentation cannot be switched off, so systems such as Linux set every segment to cover all of memory. The name segmentation fault stayed.