Assignment 06: Contiguous Memory Management

I. Objective & Theoretical Framework

Memory management is a primary function of an operating system, ensuring that multiple processes can reside in main memory simultaneously without interfering with one another. This assignment covers contiguous memory allocation techniques, where each process is contained in a single contiguous section of memory.

You will explore two foundational architectures:

When allocating memory dynamically (as in MVT), the OS must decide which free block to allocate. You will simulate these strategies[cite: 4]:

II. Prerequisite Knowledge & Resources

III. Starter Code & Partial Implementations

The following skeleton code provides the setup for calculating fragmentation in an MFT (Fixed Partition) environment. Use this to structure your calculations for total internal and external fragmentation[cite: 4].

#include <stdio.h>

int main() {
    int ms, bs, nob, ef, n, mp[10], tif = 0;
    int i, p = 0;

    printf("Enter the total memory available (in Bytes): ");
    scanf("%d", &ms);
    printf("Enter the block size (in Bytes): ");
    scanf("%d", &bs);

    nob = ms / bs;        // Calculate Total Number of Blocks
    ef = ms - nob * bs;   // Calculate Initial External Fragmentation

    printf("Enter the number of processes: ");
    scanf("%d", &n);

    for(i = 0; i < n; i++) {
        printf("Enter memory required for process %d (in Bytes): ", i+1);
        scanf("%d", &mp[i]);
    }

    printf("\nNo. of Blocks available in memory: %d\n", nob);
    printf("\nPROCESS\tMEMORY REQUIRED\tALLOCATED\tINTERNAL FRAGMENTATION\n");

    // [Insert Allocation and Internal Fragmentation Logic Here]

    // [Insert Final Fragmentation Print Statements Here]

    return 0;
}

IV. Step-by-Step Task List

  1. MFT Simulation: Complete the starter code above. Iterate through the processes. If the memory required is $\le$ the block size, print “YES” for allocated, and calculate the internal fragmentation for that block. Accumulate this into the Total Internal Fragmentation variable (tif).

  2. MVT Simulation: Write a new C program for MVT. Prompt for total memory. Use a while or for loop to continuously ask for new process memory requirements. If the requirement is $\le$ the remaining available memory, allocate it and subtract from the total. If not, declare the memory full and print the Total External Fragmentation.

  3. First-Fit Algorithm: Create a program that accepts an array of block sizes and an array of file (process) sizes. Loop through the files. For each file, loop through the blocks from the beginning and allocate the file to the very first block where block_size - file_size >= 0. Mark that block as allocated using a flag array (e.g., bf[j] = 1).

  4. Best-Fit Algorithm: Modify your First-Fit logic to create a Best-Fit simulation. Instead of stopping at the first fit, you must check every unallocated block to find the one that leaves the smallest positive remaining space.

V. Common Pitfalls & Debugging Strategies

VI. Real-World Case Study

Modern general-purpose operating systems rely almost entirely on Paging (which we will cover next) to solve the external fragmentation issues inherent in MVT architectures. However, contiguous memory allocation algorithms are far from obsolete. They are heavily utilized in user-space memory allocators (like the implementation of malloc() and free() in the C standard library) and in kernel SLAB allocators. Furthermore, Real-Time Operating Systems (RTOS) used in embedded hardware often utilize strict Best-Fit or First-Fit contiguous allocation to guarantee deterministic memory access times without the overhead of hardware page tables.

VII. Advanced Variant Tasks


VIII. Resources & Further Reading