Sub topic 2
Contiguous Allocation and Placement Strategies
Prerequisites
Assume you know binary and hexadecimal numbers, basic process concepts, and the difference between CPU, main memory, and disk. You should also know that a process uses logical addresses, while RAM uses physical addresses.
How Contiguous Allocation Works
In contiguous allocation, each process occupies one continuous region of physical memory. The operating system records where the region begins and how large it is.
A base register stores the first physical address assigned to a process. A limit register stores the size of its allowed region. For a logical address x:
- Check that
0 ≤ x < limit. - Compute the physical address as
base + x.
If the check fails, the process tried to access memory outside its region. The hardware raises a protection fault.
The operating system changes the base and limit registers during a context switch. This prevents one process from reading or overwriting another process's region.
Fixed and Variable Partitions
With fixed partitioning, memory is divided into partitions of predetermined sizes. One process occupies one partition. If a process needs less space than its partition, the unused part is internal fragmentation. The space exists inside an allocated partition but cannot be used by another process.
With variable partitioning, the operating system creates a region sized for each process. This reduces internal fragmentation, but freed regions become separated holes. These unusable gaps produce external fragmentation.
| Partitioning method | Main benefit | Main problem |
|---|---|---|
| Fixed | Simple allocation and protection | Internal fragmentation |
| Variable | Better fit for process sizes | External fragmentation |
Placement Strategies
When a process requests memory, the operating system searches available holes.
- First-fit chooses the first hole large enough. It is usually fast because the search can stop early.
- Best-fit chooses the smallest hole that is large enough. It may leave many tiny holes and often requires examining more holes.
- Worst-fit chooses the largest hole. The remaining hole may still be large enough for another process, but the strategy can waste large regions.
Consider holes of sizes 100, 500, 200, 300, and 600 KB. Allocate requests of 212, 417, 112, and 426 KB in that order.
| Strategy | 212 KB | 417 KB | 112 KB | 426 KB |
|---|---|---|---|---|
| First-fit | 500 | 600 | remaining 288 | cannot allocate |
| Best-fit | 300 | 500 | 200 | 600 |
| Worst-fit | 600 | 500 | remaining 388 | cannot allocate |
The result depends on the order of both holes and requests. For exam questions, update every hole after each allocation. Do not reuse the original hole sizes.
Compaction
Compaction moves allocated regions together so that separated holes form one large hole. It can reduce external fragmentation, but moving processes costs time and requires relocation support. The operating system must also update references or use addresses that remain valid after movement.
Exercise
Exercise
Holes are 120 KB, 350 KB, and 500 KB. Using best-fit, where are requests of 300 KB and then 100 KB placed?
Show answer
The 300 KB request uses the 350 KB hole, leaving 50 KB. The 100 KB request uses the 120 KB hole, leaving 20 KB.