Learning outcomes
- Explain segmentation and the reasons to combine paging and segmentation.
Segmentation is a memory-management technique in which a program is divided into logical units of variable size, called segments. Each segment corresponds to a meaningful part of the program rather than an arbitrary fixed-size block. For example, a program may naturally be viewed as a collection of segments such as:
The main motivation for segmentation is that programmers think in terms of logical modules, not in terms of pages of equal size. A procedure is one logical unit, a stack is another, and a data area is another. Segmentation therefore reflects the programmer’s natural view of memory more closely than paging.
Suppose a program contains a code module of 40 KB, a data area of 12 KB, a stack of 8 KB, and a symbol table of 6 KB. In segmentation, these can exist as four separate segments. Their sizes are not equal, and they need not be adjacent in physical memory.
In segmentation, a logical address is not a single number. It is a pair:
Here:
This is very natural. For example, an instruction may refer to “byte 120 inside the stack segment” or “word 80 inside the code segment.”
Since segments are variable in size and may be stored anywhere in physical memory, the operating system keeps a segment table. Each entry in the segment table usually contains:
Some systems also store protection bits, valid bits, sharing information, and status bits.
| Segment No. | Meaning | Base | Limit |
|---|---|---|---|
| 0 | Code | 4000 | 1200 |
| 1 | Data | 9000 | 700 |
| 2 | Stack | 12500 | 500 |
| 3 | Symbols | 16000 | 300 |
When the CPU generates a logical address <s, d>, the MMU performs the following steps:
Segmentation is especially good for protection and sharing because each segment is a meaningful logical unit. For example:
So segmentation is not just about address translation. It also supports cleaner program organization and finer control.
Because segments are variable-sized, segmentation suffers from external fragmentation. Free memory may exist, but it may be scattered in small holes. Therefore:
Suppose the segment table contains:
Find the physical address of logical address <1, 250>.
Since 250 < 700, the reference is valid.
If the logical address were <1, 820>, then 820 > 700, so a trap would occur.
Paging and segmentation each solve a different problem:
So the natural next step is to combine them. That gives us paged segmentation.
In paged segmentation, a logical address typically has three parts:
Here:
Paged segmentation gives the best of both worlds:
The main drawback is complexity. There are now multiple levels of translation:
More tables mean more storage overhead and more translation work, unless hardware support such as TLB-style caching is used.
If a logical address is <2, 3, 100>, the system:
Virtual memory is a memory-management technique that allows a process to execute even when the entire process is not physically present in main memory. Only the currently needed parts of the process must be in RAM. The remaining parts can stay on secondary storage, usually disk.
In simple language, virtual memory creates the illusion that the process has access to a large, continuous memory space, even though physical memory is limited and only a portion of that space is resident in RAM at any time.
A process may have a large logical address space containing code, stack, heap, data, libraries, and memory-mapped regions. But at one moment, it may actually use only a small subset of that space. Virtual memory takes advantage of this fact.
| Characteristic | Meaning |
|---|---|
| Large logical space | Processes can have address spaces much larger than RAM. |
| Partial loading | Only needed pages or segments are kept in memory. |
| On-demand transfer | Missing parts are brought from backing store when referenced. |
| Illusion of continuity | The process experiences memory as one coherent address space. |
Virtual memory works well because programs usually show locality of reference. At any given time, the CPU tends to use:
That means the whole program does not need to be in memory all at once. If it did, virtual memory would be much less useful and much more painful.
The great advantage comes with a heavy warning label: if the system constantly moves pages between RAM and disk, performance can collapse. Disk access is enormously slower than memory access.
Demand paging is the most common implementation approach for paged virtual memory. In this scheme, a page is brought into main memory only when it is actually referenced.
So instead of loading all pages of a process at startup, the system begins with only a subset in RAM. The remaining pages stay on backing store until needed.
To support demand paging, each page-table entry includes a valid-invalid bit:
When a referenced page is marked invalid, the hardware generates a page-fault trap.
A page fault occurs when the CPU references a page that is part of the process’s logical address space but is not currently resident in main memory. This is not automatically an error. In demand paging, page faults are expected.
This step is important. After the page is brought into memory, the CPU must execute the instruction again as if nothing unusual happened. That is why page-fault handling is tightly integrated with hardware support and operating-system control.
Demand paging saves memory, but page faults are expensive because disk I/O is involved. Therefore performance depends strongly on the page-fault rate.
Let:
Then the effective access time increases dramatically even when p is small, because disk access is so slow compared with RAM access.
If normal memory access takes nanoseconds and page-fault service takes milliseconds, then one page fault is not a “small delay.” It is a giant traffic jam in CPU time.
In pure demand paging, no page is brought in until it is first referenced. Some systems, however, also use limited prepaging, where a few likely-needed pages are loaded in advance to reduce initial page faults.
The textbook excerpt on BSD notes that pages can be prepaged on startup and that pure demand paging can increase fault overhead. :contentReference[oaicite:3]{index=3}
Suppose a process has pages 0 to 5 in its logical address space. The CPU references page 4, but page 4 is not currently in RAM. This causes a legal page fault. The OS loads page 4 from disk and restarts the instruction.
Now suppose the CPU references page 9, but the process has no such page in its address space. This is not a normal demand-paging event. It is an illegal reference, and the OS raises an error or terminates the process.
| Topic | Main idea | Strength | Main weakness |
|---|---|---|---|
| Segmentation | Divide memory by logical program units | Natural organization, easy protection and sharing | External fragmentation |
| Paged Segmentation | Segment first, page later | Logical structure + better physical allocation | More complex translation |
| Virtual Memory | Logical memory larger than RAM | Runs larger programs, improves utilization | Depends heavily on page-fault behavior |
| Demand Paging | Load a page only when referenced | Efficient memory use | Fault handling is expensive |
Segmentation organizes memory according to logical program modules and uses a segment table with base and limit values.
Paged segmentation keeps the logical benefits of segmentation while using paging inside each segment to improve allocation efficiency.
Virtual memory allows only part of a process to remain in RAM while the rest stays on disk, creating the illusion of a large address space.
Demand paging implements virtual memory by loading a page only when a reference to that page actually occurs.