In contiguous allocation, each process is placed in a single continuous block of memory. The question is how to divide memory among processes.

Fixed Partitions (MFT)

In multiprogramming with a fixed number of tasks (MFT), memory is divided in advance into a number of fixed-size partitions, which may be of equal or different sizes. Each partition holds exactly one process. When a partition is free, a process from the input queue is loaded into it. The degree of multiprogramming is bounded by the number of partitions. A process smaller than its partition wastes the rest of the partition, which is internal fragmentation, and a process larger than the largest partition cannot run. MFT was used in IBM OS/360.

Definition: External fragmentation. Free memory that is broken into small non-contiguous holes, so that a request cannot be satisfied even though the total free memory is large enough.

Variable Partitions (MVT)

In multiprogramming with a variable number of tasks (MVT), partitions are created dynamically to fit each process exactly. The OS keeps a table of used and free blocks (holes). When a process arrives, a hole large enough is found and the process is placed in it; the rest of the hole remains free. When a process ends, its block becomes a hole, which is merged with adjacent holes. MVT has no internal fragmentation, but over time memory becomes broken into many small holes: external fragmentation.

Placement Strategies

To choose a hole for a process of size n, first fit takes the first hole that is big enough, which is fast. Best fit takes the smallest hole that is big enough, which leaves the smallest leftover but must search the whole list. Worst fit takes the largest hole, leaving a large leftover that may be useful. Simulations show that first fit and best fit are better than worst fit in time and use of space.

Fragmentation and Compaction

Internal fragmentation is wasted space inside an allocated partition. External fragmentation is wasted space between partitions. The 50-percent rule states that with first fit, for N allocated blocks, about 0.5N blocks are lost to fragmentation. Compaction solves external fragmentation by moving all processes together so that the free memory forms one large hole. It is possible only if relocation is dynamic (execution-time binding), and it is costly in time. Paging and segmentation avoid the need for a single contiguous block.

Table 4.2: MFT and MVT

Feature MFT MVT
Partitions Fixed in number and size Created as needed
Internal fragmentation Yes No
External fragmentation Little Yes
Degree of multiprogramming Fixed Varies

Example 4.2: First fit, best fit and worst fit

Problem: Free holes are 100, 500, 200, 300 and 600 KB in order. Place processes of 212, 417, 112 and 426 KB by first fit, best fit and worst fit.

Solution:

  1. First fit: 212 → 500 (288 left), 417 → 600 (183 left), 112 → 288, 426 must wait.
  2. Best fit: 212 → 300, 417 → 500, 112 → 200, 426 → 600. All placed.
  3. Worst fit: 212 → 600 (388 left), 417 → 500, 112 → 388, 426 must wait.

Result: Best fit places all four processes; first and worst fit leave the 426 KB process waiting.

#include <stdio.h>
void fit(const char *name, int mode) {
    int hole[] = {
        100, 500, 200, 300, 600}, p[] = {212, 417, 112, 426
    };
    printf("%s: ", name);
    for (int i = 0; i < 4; i++) {
        int k = -1;
        for (int j = 0; j < 5; j++) {
            if (hole[j] < p[i]) continue;
            if (k == -1 || (mode == 1 && hole[j] < hole[k]) ||
                (mode == 2 && hole[j] > hole[k])) k = j;
            if (mode == 0) break;
        }
        if (k == -1) printf("%d->wait ", p[i]);
        else { printf("%d->%d ", p[i], hole[k]); hole[k] -= p[i]; }
    }
    printf("\n");
}
int main(void) {
    fit("First", 0); fit("Best ", 1); fit("Worst", 2);
    return 0;
}

Output

First: 212->500 417->600 112->288 426->wait
Best : 212->300 417->500 112->200 426->600
Worst: 212->600 417->500 112->388 426->wait

Common mistakes

  • Confusing internal fragmentation (inside a partition) with external (between partitions).
  • Thinking that best fit is always the best choice. It is slower and leaves tiny holes.
  • Assuming compaction is possible with compile-time or load-time binding.

Key takeaways

  • MFT uses fixed partitions and suffers internal fragmentation.
  • MVT uses variable partitions and suffers external fragmentation.
  • First, best and worst fit choose a hole.
  • Compaction joins the holes, but needs dynamic relocation.

Practice questions

  1. What is internal fragmentation?
  2. How does best fit choose a hole?
  3. What is compaction?

Answers to the practice questions

  1. Wasted space inside an allocated partition that the process does not use.
  2. It takes the smallest hole that is large enough.
  3. Moving processes together so that free memory forms one large block.

Sources

  • Official BCA syllabus Operating Systems, course code 25BCA402DS03, Unit IV