First, Best and Worst Fit
First, Best and Worst Fit
Recorded by Dr. Krithika L.B. Slot m4-fits
Placing processes in variable-size holes with First, Best and Worst Fit, the internal and external fragmentation that results, and compaction.
By the end of this session you should be able to:
In contiguous allocation each process occupies a single contiguous block of memory. Memory is divided into two parts — the OS (usually low memory, with the interrupt vector) and user processes. Protection uses the relocation and limit registers of Session 4.1.
Memory is split into fixed-size partitions; each holds one process. Simple, but the degree of multiprogramming is bounded by the number of partitions, and space inside a partition is wasted. (IBM OS/360 MFT.)
The OS keeps a table of free holes. A new process gets a hole large enough; the remainder becomes a smaller hole. When a process ends its block is freed and merged with adjacent holes.
Given a request of size n and a list of free holes, which hole should be used?
Allocate the first hole that is big enough. Stop searching as soon as one is found — fast.
Allocate the smallest hole that is big enough. Must search the whole list (unless sorted). Leaves the smallest leftover.
Allocate the largest hole. Must search the whole list. Leaves the largest leftover, hoping it stays useful.
| Process | First Fit | Best Fit | Worst Fit |
|---|---|---|---|
| 212 | 500 → 288 left | 300 → 88 left | 600 → 388 left |
| 417 | 600 → 183 left | 500 → 83 left | 500 → 83 left |
| 112 | 288 → 176 left | 200 → 88 left | 388 → 276 left |
| 426 | must wait | 600 → 174 left | must wait |
Here only Best Fit places all four. In simulations, first fit and best fit both beat worst fit in time and storage use; first fit is usually faster.
Enough total free memory exists, but it is split into non-contiguous holes, none large enough. In the example above, First Fit has 100 + 176 + 200 + 300 + 183 = 959 KB free but cannot place 426 KB.
Memory allocated to a process is slightly larger than requested (blocks come in fixed units); the unused part inside the block is wasted. E.g. a 18 462-byte request in a hole of 18 464 bytes: the 2 bytes are not worth tracking.
Statistical analysis of first fit shows that for N allocated blocks, about 0.5 N blocks are lost to fragmentation — one-third of memory may be unusable.
450 KB free in three holes; a 300 KB request fails.
Move P2 and P3 down, updating their relocation registers.
One 450 KB hole — the request fits.
First, Best and Worst Fit
Recorded by Dr. Krithika L.B. Slot m4-fits
Fragmentation and compaction
Recorded by Dr. Krithika L.B. Slot m4-fragmentation
Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.
Answer each question in your own words first, then reveal the model answer.
Revealed 0First fit: 200. Best fit: 70. Worst fit: 200.
Internal — it is inside an allocated block.
Addresses were fixed when the process was loaded; moving it would break every address. Compaction needs execution-time relocation.
Worst fit, in both speed and storage utilisation.
The real cure for external fragmentation is to stop requiring contiguity. Paging cuts both memory and processes into fixed-size pieces.
Continue to 4.3 — Paging with a TLB →