Paging and segmentation allow a process to be placed in memory in pieces that need not be next to each other, which removes external fragmentation.
Paging
In paging, physical memory is divided into fixed-size blocks called frames, and logical memory into blocks of the same size called pages, usually 4 KB. A process's pages can be loaded into any free frames. A page table for each process gives the frame number of each page. A logical address is split into a page number p and a page offset d. The page number indexes the page table to find the frame number f, and the physical address is f × page size + d. With page size 2ⁿ, the low n bits of the address are the offset and the rest are the page number. Paging has no external fragmentation, but on average half a page is lost per process to internal fragmentation.
Definition: Paging. A memory-management scheme in which logical memory is divided into fixed-size pages and physical memory into frames of the same size, with a page table mapping each page to a frame.
Translation Look-aside Buffer
Since the page table is in memory, every access would need two memory references. A small, fast associative cache called the translation look-aside buffer (TLB) holds recently used page-table entries. If the page is found in the TLB (a hit), the frame is known at once. If not (a miss), the page table is used and the entry is added to the TLB. The effective access time is EAT = h × (TLB + memory) + (1 − h) × (TLB + 2 × memory), where h is the hit ratio.
Segmentation
Segmentation supports the programmer's view of memory as a set of logical units of different sizes, such as the main program, functions, the stack and arrays. Each segment has a number and a length. A logical address is a pair (segment number s, offset d). A segment table gives, for each segment, its base (starting physical address) and limit (length). If d < limit, the physical address is base + d; otherwise a trap occurs. Segmentation makes sharing and protection natural, since a whole segment such as a code library can be shared or marked read-only. But since segments vary in size, it suffers external fragmentation. Some systems combine the two, as segmentation with paging.
Table 4.3: Paging and segmentation
| Feature | Paging | Segmentation |
|---|---|---|
| Unit size | Fixed (pages) | Variable (segments) |
| View | System's view | Programmer's view |
| Address | Page number, offset | Segment number, offset |
| Fragmentation | Internal | External |
Example 4.3: Paging and segmentation addresses
Problem: (a) Page size is 1 KB. Page 2 is in frame 5. Find the physical address of logical address 2100. (b) Segment 1 has base 6300 and limit 400. Find the physical address of (1, 53) and of (1, 500).
Solution:
- 2100 / 1024: page 2, offset 2100 − 2048 = 52. Physical = 5 × 1024 + 52 = 5172.
- (1, 53): 53 < 400, so physical = 6300 + 53 = 6353.
- (1, 500): 500 ≥ 400, so a trap occurs.
Result: (a) 5172, (b) 6353 and a trap.
#include <stdio.h>
int main(void) {
int ps = 1024, table[] = {3, 7, 5, 1}, la = 2100;
int p = la / ps, d = la % ps;
printf("page %d offset %d -> frame %d -> physical %d\n",
p, d, table[p], table[p] * ps + d);
int base = 6300, limit = 400, offs[] = {53, 500};
for (int i = 0; i < 2; i++) {
if (offs[i] < limit)
printf("(1,%d) -> %d\n", offs[i], base + offs[i]);
else printf("(1,%d) -> trap\n", offs[i]);
}
return 0;
}
Output
page 2 offset 52 -> frame 5 -> physical 5172
(1,53) -> 6353
(1,500) -> trap
Common mistakes
- Thinking that paging causes external fragmentation. It causes internal fragmentation only.
- Forgetting to check the segment limit in segmentation.
- Dividing the address by the wrong page size.
Key takeaways
- Paging splits memory into fixed pages and frames, mapped by a page table.
- Physical address = frame × page size + offset.
- The TLB caches page-table entries.
- Segmentation uses variable segments with base and limit, and supports sharing.
Practice questions
- What is a frame?
- What is the TLB?
- How is a segment address translated?
Answers to the practice questions
- A fixed-size block of physical memory into which a page is loaded.
- A fast cache of recently used page-table entries.
- If the offset is less than the segment limit, physical address = base + offset; otherwise a trap.
Sources
- Official BCA syllabus Operating Systems, course code 25BCA402DS03, Unit IV
