CSC264 ยท 2081 ยท 5 marks
Consider a swapping system in which memory consists of the following hole sizes in memory order: 15 MB, 2 MB, 10 MB, 6 MB, 8 MB and 20 MB. Which hole is taken for successive segment requests of: (a) 10 MB (b) 10 MB For first fit, next fit and best fit.
Consider a swapping system in which memory consists of the following hole sizes in memory order: 15 MB, 2 MB, 10 MB, 6 MB, 8 MB and 20 MB. Which hole is taken for successive segment requests of: (a) 10 MB (b) 10 MB For first fit, next fit and best fit. [5]
Memory Allocation: First Fit, Next Fit, Best Fit
Given
Holes in memory order:
| Hole # | Size |
|---|---|
| 1 | 15 MB |
| 2 | 2 MB |
| 3 | 10 MB |
| 4 | 6 MB |
| 5 | 8 MB |
| 6 | 20 MB |
Successive requests: (a) 10 MB, (b) 10 MB.
Solution
First Fit
Allocates the first hole large enough, scanning from the start each time.
(a) 10 MB: Hole 1 (15 MB) $\ge 10$ โ allocate. Hole 1 leftover $= 15 - 10 = 5$ MB.
(b) 10 MB: Scan from start: Hole 1 (5) โ, Hole 2 (2) โ, Hole 3 (10) $\ge 10$ โ allocate. Leftover $= 0$ MB.
Result: (a) Hole 1, (b) Hole 3.
Next Fit
Like First Fit but resumes scanning from the position of the last allocation.
(a) 10 MB: Start at Hole 1 (15) โ allocate. Pointer at Hole 1, leftover 5 MB.
(b) 10 MB: Resume from Hole 1: (5) โ, Hole 2 (2) โ, Hole 3 (10) โ allocate. Pointer at Hole 3.
Result: (a) Hole 1, (b) Hole 3.
(Since scanning resumes at the last hole which is now too small, next fit gives the same result as first fit here.)
Best Fit
Allocates the smallest hole that is large enough.
(a) 10 MB: Candidates $\ge 10$: Hole 1 (15, leftover 5), Hole 3 (10, leftover 0), Hole 6 (20, leftover 10). Smallest sufficient = Hole 3 (exact fit). Leftover 0 MB.
(b) 10 MB: Remaining candidates $\ge 10$: Hole 1 (15, leftover 5), Hole 6 (20, leftover 10). Smallest = Hole 1. Leftover 5 MB.
Result: (a) Hole 3, (b) Hole 1.
Final Comparison
| Request | First Fit | Next Fit | Best Fit |
|---|---|---|---|
| (a) 10 MB | Hole 1 (15 MB) | Hole 1 (15 MB) | Hole 3 (10 MB) |
| (b) 10 MB | Hole 3 (10 MB) | Hole 3 (10 MB) | Hole 1 (15 MB) |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.