Memory Allocation Scheme- First Fit , Worse Fit, Best Fit

Let’s delve into the memory allocation methods for fixed partitions: First Fit, Worst Fit, and Best Fit.

First Fit Allocation:
In this technique, the operating system searches through the list of free memory blocks, starting from the beginning, until it finds a block large enough to accommodate the memory request from a process.

Once a suitable block is found, it is split into two parts: the portion allocated to the process and the remaining free block.
Advantages:
Simple and efficient search algorithm.
Minimizes memory fragmentation.
Fast allocation of memory.
Disadvantages:
Poor performance in highly fragmented memory.
May allocate larger blocks than needed, leading to inefficient memory utilization.

Example: Consider allocating processes with sizes 212K, 417K, 112K, and 426K to memory blocks of sizes 100K, 500K, 200K, 300K, and 600K respectively:

Process 1: Allocated to Block 2 (size 212K).
Process 2: Allocated to Block 5 (size 417K).
Process 3: Allocated to Block 2 (size 112K).
Process 4: Not Allocated
Process No.     Process Size    Block no.
 1                      212                 2
 2                      417                 5
 3                      112                 2
 4                      426                 Not Allocated
Free Blocks
100->176->200->300->183->
//*********************************************************************
// C Program First Fit Implementation
//*********************************************************************
#include <stdio.h>
#include <string.h>

void firstFit(int blockSize[], int m, int processSize[], int n) {
    int allocation[n];
    memset(allocation, -1, sizeof(allocation));

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (blockSize[j] >= processSize[i]) {
                allocation[i] = j;
                blockSize[j] -= processSize[i];
                break;
            }
        }
    }

    printf("\nProcess No.\tProcess Size\tBlock no.\n");
    for (int i = 0; i < n; i++) {
        printf(" %d\t\t%d\t\t", i + 1, processSize[i]);
        if (allocation[i] != -1)
            printf("%d", allocation[i] + 1);
        else
            printf("Not Allocated");
        printf("\n");
    }

    printf("Free Blocks\n");
    for (int i = 0; i < m; i++) printf("%d->",blockSize[i]);
}

int main() {
    int blockSize[] = {100, 500, 200, 300, 600};
    int processSize[] = {212, 417, 112, 426};
    int m = sizeof(blockSize) / sizeof(blockSize[0]);
    int n = sizeof(processSize) / sizeof(processSize[0]);
    firstFit(blockSize, m, processSize, n);
    return 0;
}

Worst Fit Allocation:
In this method, the process traverses the entire memory and always searches for the largest available hole/partition.
The process is then placed in that hole/partition.
Advantages:
Large internal fragmentation allows other small processes to utilize leftover space.
Disadvantages:
Slow process due to traversing all partitions.
Significant internal fragmentation.

Note:If a large process claims the largest available partition, it can leave substantial unused space.

Example: Consider allocating processes with sizes 212K, 417K, 112K, and 426K to memory blocks of sizes 100K, 500K, 200K, 300K, and 600K respectively:

Process 1: Allocated to Block 5 (size 212K).
Process 2: Allocated to Block 2 (size 417K).
Process 3: Allocated to Block 5 (size 112K).
Process 4: Not Allocated
Process No.     Process Size    Block no.
 1                          212             5
 2                          417             2
 3                          112             5
 4                          426             Not Allocated
Free Blocks
100->83->200->300->276->

//*********************************************************************
// C Program Worst Fit Implementation
//*********************************************************************
#include <stdio.h>
#include <string.h>

void worstFit(int blockSize[], int m, int processSize[], int n) {
    int allocation[n];
    memset(allocation, -1, sizeof(allocation));

    for (int i = 0; i < n; i++) {
        int wstIdx = -1;
        for (int j = 0; j < m; j++) {
            if (blockSize[j] >= processSize[i]) {
                if (wstIdx == -1 || blockSize[wstIdx] < blockSize[j])
                    wstIdx = j;
            }
        }
        if (wstIdx != -1) {
            allocation[i] = wstIdx;
            blockSize[wstIdx] -= processSize[i];
        }
    }

    printf("\nProcess No.\tProcess Size\tBlock no.\n");
    for (int i = 0; i < n; i++) {
        printf(" %d\t\t%d\t\t", i + 1, processSize[i]);
        if (allocation[i] != -1)
            printf("%d", allocation[i] + 1);
        else
            printf("Not Allocated");
        printf("\n");
    }
    printf("Free Blocks\n");
    for (int i = 0; i < m; i++) printf("%d->",blockSize[i]);
}

int main() {
    int blockSize[] = {100, 500, 200, 300, 600};
    int processSize[] = {212, 417, 112, 426};
    int m = sizeof(blockSize) / sizeof(blockSize[0]);
    int n = sizeof(processSize) / sizeof(processSize[0]);
    worstFit(blockSize, m, processSize, n);
    return 0;
}
   
Best Fit Allocation:
In Best Fit, the operating system searches for the block closest in size to the memory request from the process.
It allocates the process to the smallest sufficient partition among the free available partitions.
Advantages:
Minimizes wastage of memory space.
Suitable for scenarios where memory requirements vary significantly.
Disadvantages:
Time-consuming due to searching for the closest-sized block.

Example: Consider allocating processes with sizes 212K, 417K, 112K, and 426K to memory blocks of sizes 100K, 500K, 200K, 300K, and 600K respectively:

Process 1: Allocated to Block 4 (size 212K).
Process 2: Allocated to Block 2 (size 417K).
Process 3: Allocated to Block 3 (size 112K).
Process 4: Allocated to Block 5 (size 426K).

Process No.     Process Size    Block no.
 1                      212                     4
 2                      417                     2
 3                      112                     3
 4                      426                     5
Free Blocks
100->83->88->88->174->

//*********************************************************************
// C Program Best  Fit Implementation
//*********************************************************************
#include <stdio.h>
#include <string.h>

void bestFit(int blockSize[], int m, int processSize[], int n) {
    int allocation[n];
    for (int i = 0; i < n; i++)
        allocation[i] = -1;

    for (int i = 0; i < n; i++) {
        int bestIdx = -1;
        for (int j = 0; j < m; j++) {
            if (blockSize[j] >= processSize[i]) {
                if (bestIdx == -1)
                    bestIdx = j;
                else if (blockSize[bestIdx] > blockSize[j])
                    bestIdx = j;
            }
        }
        if (bestIdx != -1) {
            allocation[i] = bestIdx;
            blockSize[bestIdx] -= processSize[i];
        }
    }

    printf("\nProcess No.\tProcess Size\tBlock no.\n");
    for (int i = 0; i < n; i++) {
        printf(" %d\t\t%d\t\t", i + 1, processSize[i]);
        if (allocation[i] != -1)
            printf("%d", allocation[i] + 1);
        else
            printf("Not Allocated");
        printf("\n");
    }
    printf("Free Blocks\n");
    for (int i = 0; i < m; i++) printf("%d->",blockSize[i]);
}
int main() {
    int blockSize[] = {100, 500, 200, 300, 600};
    int processSize[] = {212, 417, 112, 426};
    int m = sizeof(blockSize) / sizeof(blockSize[0]);
    int n = sizeof(processSize) / sizeof(processSize[0]);
    bestFit(blockSize, m, processSize, n);
    return 0;
}

Comments

Popular posts from this blog

CSL 204 OPERATING SYSTEM LAB KTU IV SEM CSE

FCFS and SJF

Shared Memory Inter-Process Communication ( IPC)