Sub topic 2
Contiguous Allocation and Placement Strategies
This chapter assumes you know binary and hexadecimal arithmetic and powers of two. It also assumes you know the roles of the CPU, main memory and disk, what a context switch is, and how to read short Python. You should also remember the distinction between logical and physical addresses from the previous chapter.
What contiguous allocation means
In contiguous allocation, each process occupies one unbroken block of physical memory. The block holds the process's code, data and stack together. This is the simplest memory-management scheme, and it is the baseline that paging and segmentation were designed to improve on.
Memory is divided into two regions:
- A low region for the operating system, often next to the interrupt vector.
- The rest, which the OS hands out to user processes.
The scheme has to answer three questions:
- How does hardware translate and protect addresses?
- How is user memory divided?
- Which free block does a new process get?
Protection with base and limit registers
Dynamic relocation uses two hardware registers per running process:
- The base register, also called the relocation register, holds the physical address where the process starts.
- The limit register holds the size of the process's logical address space.
The CPU generates a logical address that starts at 0. The memory-management hardware checks it against the limit. If the check passes, the hardware adds the base:
if logical < limit: physical = base + logical
else: trap to the OS (addressing error)For example, take base = 30000 and limit = 12000:
| Logical address | Check | Result |
|---|---|---|
| 0 | 0 < 12000 | physical 30000 |
| 11999 | 11999 < 12000 | physical 41999 |
| 12000 | 12000 is not below 12000 | trap |
The valid logical range is 0 to limit − 1. Forgetting this off-by-one is a common mistake in exam answers.
The dispatcher loads base and limit during a context switch. Only privileged instructions can change them, so a user process cannot enlarge its own region. The check happens on every memory reference, so it is done in hardware.
Because addresses are bound at run time, the OS can move a process. It copies the block elsewhere and updates the base register. This ability makes compaction possible, and the fragmentation chapter uses it.
Fixed (static) partitions
In fixed partitioning, memory is divided into partitions at system start. The sizes may be equal or unequal. Each partition holds exactly one process.
- The number of partitions caps the degree of multiprogramming, the number of processes in memory at once.
- A process larger than the largest partition cannot run, however much total memory is free.
- A process smaller than its partition wastes the rest of that partition. That waste is internal fragmentation, equal to partition size minus process size.
With unequal partitions, the OS can keep one queue per partition. Each process goes to the smallest partition that fits it. The alternative is a single queue from which the OS picks the best available partition. Per-partition queues can leave a large partition idle while a small one has a long queue.
Fixed partitions are simple and fast, but they waste memory. Few modern systems use them.
Variable (dynamic) partitions
In variable partitioning, the OS creates a partition of exactly the size a process requests. Nothing is wasted inside a partition, so there is no internal fragmentation in theory.
The OS keeps a list of holes, which are free blocks. The life cycle works like this:
- A process arrives needing
nbytes. The OS searches the hole list for a hole of at leastnbytes. - If the hole is larger, it is split. One part goes to the process and the remainder stays on the list as a smaller hole.
- When a process ends, its block becomes a hole. If it is adjacent to other holes, the OS coalesces them into one larger hole.
Over time, many small holes appear between allocated blocks. This is external fragmentation, covered in the next chapter. The placement strategy affects how quickly it develops.
Placement strategies
This is the dynamic storage-allocation problem: given a list of holes, which one should satisfy a request of size n? There are three standard answers:
- First-fit takes the first hole that is big enough. The search stops as soon as one is found.
- Best-fit takes the smallest hole that is big enough. It searches the whole list unless the list is sorted by size.
- Worst-fit takes the largest hole. It also searches the whole list unless the list is sorted.
A fourth variant, next-fit, is first-fit that resumes searching where the previous search ended instead of at the list head.
| Strategy | Search cost | Leftover after split | Typical weakness |
|---|---|---|---|
| First-fit | Stops early, usually fastest | Varies | Small holes pile up near the start of the list |
| Best-fit | Full scan (or sorted list) | Smallest possible | Leaves many tiny, unusable holes |
| Worst-fit | Full scan (or sorted list) | Largest possible | Uses up the large holes that big requests need |
| Next-fit | Stops early | Varies | Spreads allocations evenly and tends to break up large holes at the end of memory |
Simulation studies show that first-fit and best-fit use storage better than worst-fit. First-fit is generally faster. Worst-fit sounds sensible because a large leftover hole is still useful, but it destroys the large holes that future large requests need.
Worked example
This is the standard GATE-style setup. Variable partitioning starts with holes of 100, 500, 200, 300 and 600 KB, in address order. Four processes arrive in order, needing 212, 417, 112 and 426 KB. Each allocation splits a hole, and the remainder stays in the same place in the list.
First-fit
- 212 goes into the 500 hole, leaving 288.
- 417 goes into the 600 hole, leaving 183.
- 112 goes into the 288 hole, leaving 176.
- 426 has no hole of at least 426 KB. It must wait.
Best-fit
- 212 goes into 300, leaving 88.
- 417 goes into 500, leaving 83.
- 112 goes into 200, leaving 88.
- 426 goes into 600, leaving 174.
Worst-fit
- 212 goes into 600, leaving 388.
- 417 goes into 500, leaving 83.
- 112 goes into 388, leaving 276.
- 426 has no fit, because the largest hole is 300. It must wait.
Best-fit is the only strategy that places all four processes here. This is a property of this input, not a general rule. Other inputs favor first-fit, and exam questions are built to show this. Never assume one strategy is always best.
The following program reproduces these results:
def allocate(holes, requests, strategy):
"""Place each request; return (hole index or None per request, final holes)."""
holes = list(holes)
placed = []
for size in requests:
fits = [i for i, h in enumerate(holes) if h >= size]
if not fits:
placed.append(None)
continue
if strategy == "first":
i = fits[0]
elif strategy == "best":
i = min(fits, key=lambda j: holes[j])
else: # worst-fit
i = max(fits, key=lambda j: holes[j])
holes[i] -= size
placed.append(i)
return placed, holes
holes = [100, 500, 200, 300, 600]
requests = [212, 417, 112, 426]
for s in ("first", "best", "worst"):
print(s, allocate(holes, requests, s))
# first ([1, 4, 1, None], [100, 176, 200, 300, 183])
# best ([3, 1, 2, 4], [100, 83, 88, 88, 174])
# worst ([4, 1, 4, None], [100, 83, 200, 300, 276])The min and max calls return the first index on ties, so the earliest hole wins a tie. Exam questions usually assume the same rule.
Strengths and limits of contiguous allocation
Strengths:
- Translation is cheap: one comparison and one addition.
- Hardware needs only two registers.
- Sequential physical layout is easy to reason about.
Limits:
- A process needs one hole big enough for its whole size. Enough total free memory is not sufficient if it is scattered.
- Processes cannot easily grow. A neighbor may block the space after them.
- Sharing code between processes is awkward, because each has its own contiguous block.
- Fragmentation, internal or external, wastes memory.
These limits motivate paging, which removes the requirement for one contiguous block.
Exercise
Holes of 200, 400, 150 and 350 KB exist in that address order. Requests of 180, 90 and 300 KB arrive in that order. Which of first-fit, best-fit and worst-fit fails to place all three requests, and what is the final hole list for first-fit?
Show answer
Worst-fit fails. It puts 180 in the 400 hole (220 left), then 90 in the 350 hole (260 left), and the largest remaining hole is 260, so the 300 request cannot be placed. First-fit places 180 in 200 (20 left), 90 in 400 (310 left) and 300 in the 310 hole (10 left). Its final holes are 20, 10, 150 and 350 KB in address order. Best-fit also places everything, using the 200, 150 and 350 holes, and leaves 20, 400, 60 and 50 KB.
Exercise
A process uses base = 24000 and limit = 8000. What happens for logical addresses 7999 and 8000?
Show answer
Logical address 7999 is below the limit, so the physical address is 24000 + 7999 = 31999. Logical address 8000 is not below the limit, so the hardware traps to the OS with an addressing error. Valid logical addresses run from 0 to 7999.