Memory Allocation Technique - CSU360 - Shoolini U

Practical 14: Write a C program to simulate the following contiguous memory allocation techniques: First fit, Best fit, and Worst fit

14. Memory Allocation Techniques

This section covers the theoretical concepts behind three primary memory allocation techniques used in operating systems: Worst Fit, Best Fit, and First Fit. Each technique utilizes a different strategy to manage free memory in a system and has its own advantages and disadvantages.

14.1. First Fit

First Fit allocates the first block that is big enough. This method scans memory from the beginning and allocates the first free block that fits the process. It is generally faster than Best and Worst Fit because it stops searching as soon as it finds a free block large enough.

Advantages:

Disadvantages:

14.1.1. Algorithm of First Fit
First Fit
Figure 14.1.1: Flow Diagram of First Fit
  1. Start Scan: Begin at the start of the list of memory blocks.
  2. Check Block Size: Examine each memory block in sequence to determine if it is free and large enough to accommodate the requested memory size.
  3. Allocate Memory:
    • If a suitable block is found:
      • Allocate the necessary memory from this block to the process.
      • If the block is exactly the size needed, mark it as fully occupied.
      • If the block is larger than needed, allocate the required amount at the beginning of the block and reduce the size of the block by the amount allocated.
    • If no suitable block is found after checking all available blocks:
      • Return a memory allocation error or attempt another memory management strategy, such as compaction or swapping.
  4. Update Memory List: Adjust the list of memory blocks to reflect the allocated memory and any remaining free space.
  5. Process Request Complete: End the allocation process once the memory has been successfully assigned or an error has been returned.
First Fit
Figure 14.1.1.1: Output of First Fit
First Fit
Figure 14.1.1.1: Output of First Fit
14.1.2. Implementation of First Fit in C
#include <stdio.h>

#define max 25
void main()
{
    int frag[max], b[max], f[max], i, j, nb, nf, temp;
    static int bf[max], ff[max];
    printf("\n\tMemory Management Scheme - First Fit");
    printf("\nEnter the number of blocks:");
    scanf("%d", &nb);
    printf("Enter the number of files:");
    scanf("%d", &nf);
    printf("\nEnter the size of the blocks:-\n");
    for (i = 1; i <= nb; i++)
    {
        printf("Block %d:", i);
        scanf("%d", &b[i]);
    }
    printf("Enter the size of the files :-\n");
    for (i = 1; i <= nf; i++)
    {
        printf("File %d:", i);
        scanf("%d", &f[i]);
    }
    for (i = 1; i <= nf; i++)
    {
        for (j = 1; j <= nb; j++)
        {
            if (bf[j] != 1)
            {
                temp = b[j] - f[i];
                if (temp >= 0)
                {
                ff[i] = j;
                    break;
                    }
            }
        }
        frag[i] = temp;
        bf[ff[i]] = 1;
    }
    printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
    for (i = 1; i <= nf; i++)
        printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i, f[i], ff[i], b[ff[i]], frag[i]);    
}

14.2 Best Fit

Best Fit allocates the smallest free block that is adequate to accommodate the process's demands. By doing this, it attempts to minimize the size of the unallocated space which reduces the likelihood of unusable small free spaces after allocation.

Advantages:

Disadvantages:

14.2.1. Algorithm of Best Fit
Best Fit
Figure 14.2.1: Flow Diagram of Best Fit
  1. Initialize Variables: Start with no block selected and set the size of the smallest suitable block found to be infinite (or a very large number).
  2. Scan Memory Blocks: Go through each block of memory:
    • If the block is free and its size is greater than or equal to the requested size and smaller than the smallest found so far, update the smallest block variable to this block's size and record its location.
  3. Check for Suitable Block:
    • If a suitable block has been found:
      • Allocate the required amount of memory from this block.
      • If the block size is exactly what is needed, mark it as fully occupied.
      • If the block is larger than needed, allocate the necessary amount and reduce the size of the block by that amount, leaving the remainder as a smaller free block.
    • If no suitable block is found (smallest block variable remains infinite):
      • Return a memory allocation error or consider using other memory management techniques, such as compaction or using a different allocation strategy.
  4. Update Memory List: Modify the list of memory blocks to reflect the new allocation and any changes in block sizes.
  5. Complete Request: End the process once memory has been successfully allocated or an error message has been issued.
Best Fit
Figure 14.2.1.1: Best Fit
Best Fit
Figure 14.2.1.1: Best Fit
14.2.2. Implementation of Best Fit in C
#include <stdio.h>

#define max 25
void main()
{
    int frag[max], b[max], f[max], i, j, nb, nf, temp, lowest = 10000;
    static int bf[max], ff[max];
    
    printf("\nEnter the number of blocks:");
    scanf("%d", &nb);
    printf("Enter the number of files:");
    scanf("%d", &nf);
    printf("\nEnter the size of the blocks:-\n");
    for (i = 1; i <= nb; i++)
    {
        printf("Block %d:", i);
        scanf("%d", &b[i]);
    }
    printf("Enter the size of the files :-\n");
    for (i = 1; i <= nf; i++)
    {
        printf("File %d:", i);
        scanf("%d", &f[i]);
    }
    for (i = 1; i <= nf; i++)
    {
        for (j = 1; j <= nb; j++)
        {
            if (bf[j] != 1)
            {
                temp = b[j] - f[i];
                if (temp >= 0)
                    if (lowest > temp)
                    {
                        ff[i] = j;

                        lowest = temp;
                    }
            }
        }
        frag[i] = lowest;
        bf[ff[i]] = 1;
        lowest = 10000;
    }
    printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
    for (i = 1; i <= nf && ff[i] != 0; i++)
        printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i, f[i], ff[i], b[ff[i]], frag[i]);    
}

14.3 Worst Fit

The Worst Fit allocation method scans the list of free blocks and allocates the largest available block to the process. Its primary objective is to reduce the size of the largest free block. This approach is based on the assumption that a larger block will be more likely to accommodate future process requests, thereby minimizing the number of times a process cannot be allocated due to insufficient memory.

Advantages:

Disadvantages:

14.3.1 Algorithm of Worst Fit
Worst Fit
Figure 14.3.1: Flow Diagram of Worst Fit
  1. Initialize Variables: Start with no block selected and set the size of the largest block found to zero.
  2. Scan Memory Blocks: Go through each block of memory:
    • If the block is free and its size is larger than the current largest block that has been found and is capable of accommodating the requested size, update the largest block variable to this block's size and record its location.
  3. Allocate Memory:
    • If a suitable block (largest block) has been found:
      • Allocate the required amount of memory from this block.
      • If the block is exactly the size needed, mark it as fully occupied.
      • If the block is larger than needed, allocate the necessary amount from the beginning or end of the block and reduce the size of the block by the amount allocated, leaving the remainder as a free block.
    • If no suitable block is found:
      • Return a memory allocation error or consider using other memory management techniques, such as compaction or using a different allocation strategy.
  4. Update Memory List: Modify the list of memory blocks to reflect the new allocation and any changes in block sizes.
  5. Complete Request: End the process once memory has been successfully allocated or an error message has been issued.
Worst Fit
Figure 14.3.1.1: Worst Fit
Worst Fit
Figure 14.3.1.1: Worst Fit
14.3.2 Implementation of Worst Fit in C
#include <stdio.h>
#define max 25
void main()
{
    int frag[max], b[max], f[max], i, j, nb, nf, temp, highest = 0;
    static int bf[max], ff[max];
    printf("\n\tMemory Management Scheme - Worst Fit");
    printf("\nEnter the number of blocks:");
    scanf("%d", &nb);
    printf("Enter the number of files:");
    scanf("%d", &nf);
    printf("\nEnter the size of the blocks:-\n");
    for (i = 1; i <= nb; i++)
    {
        printf("Block %d:", i);
        scanf("%d", &b[i]);
    }
    printf("Enter the size of the files :-\n");
    for (i = 1; i <= nf; i++)
    {
        printf("File %d:", i);
        scanf("%d", &f[i]);
    }
    for (i = 1; i <= nf; i++)
    {

        for (j = 1; j <= nb; j++)
        {
            if (bf[j] != 1) // if bf[j] is not allocated
            {
                temp = b[j] - f[i];
                if (temp >= 0)
                    if (highest < temp)
                    {
                        ff[i] = j;
                        highest = temp;
                    }
            }
        }
        frag[i] = highest;
        bf[ff[i]] = 1;
        highest = 0;
    }
    printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
    for (i = 1; i <= nf; i++)
        printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i, f[i], ff[i], b[ff[i]], frag[i]);

}

Image Reference