Operating System - Worst Fit Algorithm



The Worst Fit Algorithm is a memory allocation strategy used in contiguous memory allocation systems. This chapter will explain the Worst Fit Algorithm, how it works, and its implementation in operating systems.

What is Worst Fit Algorithm?

The Worst Fit Algorithm is just the opposite of the best fit algorithm. Here, the memory manager allocates the largest available block to the process that requests memory. The main idea behind this approach is to leave smaller holes that may be more suitable for future processes. Sometimes, if all the available blocks are smaller than the process size, then the process is marked as "Not Allocated". So the process needs to wait until a suitable block becomes available.

Example

Consider the following example where we have five memory blocks of sizes 6, 5, 50, 20, and 16 units, and five processes of sizes 4, 10, 15, 20, and 23 units respectively. We will allocate memory to these processes using the Worst Fit Algorithm.

Input :
blockSize[] = { 6, 5, 50, 20, 16};
processSize[] = {4, 10, 15, 20, 23};

Output:
Process No.     Process Size    Block no.
    1              4               50
    2              10              20
    3              15              16
    4              20              Not Allocated;
    5              23              Not Allocated;

When process 1 comes to memory allocation, it is allocated to block of size 50, as it is the largest block available. Similarly, process 10 is allocated to block of size 20, and process 15 is allocated to block of size 16.

The process sizes 20 and 23 cannot be allocated as there is no block large enough to accommodate them.

Steps to Implement Worst Fit Algorithm

The Worst Fit Algorithm can be implemented using the following steps −

  • Initialize a pointer to keep track of the last allocated block.
  • For each process, search the entire memory to find the largest available block that is large enough to hold the process.
  • If a suitable block is found, allocate the process to that block.
  • After searching the entire memory, if no suitable block is found, mark the process as "Not Allocated".

C++ Code for Worst Fit Algorithm

Below is a sample C++ code that demonstrates the implementation of the Worst Fit Algorithm −

#include <iostream>
using namespace std;

void worstFit(int blockSize[], int m, int processSize[], int n) {
    int allocation[n];
    int originalBlockSize[m];
    
    // Copy original block sizes
    for (int i = 0; i < m; i++)
        originalBlockSize[i] = blockSize[i];
    
    // Initialize allocation array
    for (int i = 0; i < n; i++)
        allocation[i] = -1;

    // Worst Fit 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] = -1;
        }
    }

    cout << "Process No.\tProcess Size\tAllocated Block Size" << endl;
    for (int i = 0; i < n; i++) {
        cout << " " << i + 1 << "\t\t\t" << processSize[i] << "\t\t\t\t";
        if (allocation[i] != -1)
            cout << originalBlockSize[allocation[i]]; // print original size
        else
            cout << "Not Allocated";
        cout << endl;
    }
}

int main() {
    int blockSize[] = {6, 5, 50, 20, 16};
    int processSize[] = {4, 10, 15, 20, 23};
    int m = sizeof(blockSize) / sizeof(blockSize[0]);
    int n = sizeof(processSize) / sizeof(processSize[0]);

    worstFit(blockSize, m, processSize, n);
    return 0;
}

The output of the above code will be −

Process No.     Process Size    Allocated Block Size
    1              4               50
    2              10              20
    3              15              16
    4              20              Not Allocated
    5              23              Not Allocated

Advantages of Worst Fit Algorithm

The Worst Fit Algorithm have following advantages −

  • Reduces External Fragmentation − Worst Fit Algorithm allocates the largest available block to a process. This way it leaves smaller holes that may be more suitable for future processes.
  • Simplicity − The Worst Fit Algorithm is simple to implement and understand.
  • Better Utilization of Memory − The worst Fit Algorithm help in better utilization of memory.

Disadvantages of Worst Fit Algorithm

Worst Fit Algorithm also comes with some disadvantages:

  • Increased Internal Fragmentation − By allocating the largest block to a process, most of the time it leads memory wastage and internal fragmentation.
  • Slower Allocation − The Worst Fit Algorithm requires searching the entire memory to find the largest block. This process can be slower compared to other algorithms like First Fit.
  • Not Suitable for All Scenarios − The Worst Fit Algorithm may not be suitable for all types of memory allocation patterns.

Conclusion

The Worst Fit Algorithm is a memory allocation strategy that allocates the largest available block to a process. The main idea behind this approach is to leave smaller holes that may be more suitable for future processes. This approach reduces external fragmentation but it can lead to increased internal fragmentation and slower allocation times. The Worst Fit Algorithm is simple to implement and understand.

Advertisements