 
- OS - Home
- OS - Needs
- OS - Overview
- OS - History
- OS - Components
- OS - Structure
- OS - Architecture
- OS - Services
- OS - Properties
- Process Management
- OS - Processes
- OS - Process Control Block
- OS - Operations on Processes
- OS - Inter Process Communication
- OS - Context Switching
- OS - Multi-threading
- Scheduling Algorithms
- OS - Process Scheduling
- Preemptive and Non-Preemptive Scheduling
- Scheduling Algorithms Overview
- FCFS Scheduling Algorithm
- SJF Scheduling Algorithm
- Round Robin Scheduling Algorithm
- HRRN Scheduling Algorithm
- Priority Scheduling Algorithm
- Multilevel Queue Scheduling
- Lottery Scheduling Algorithm
- OS - TAT & WAT
- Predicting Burst Time in SJF Scheduling
- Process Synchronization
- OS - Process Synchronization
- OS - Critical Section Problem
- OS - Critical Section Synchronization
- OS - Mutual Exclusion Synchronization
- OS - Semaphores
- OS - Counting Semaphores
- OS - Mutex
- OS - Turn Variable
- OS - Bounded Buffer Problem
- OS - Reader Writer Locks
- OS - Test and Set Lock
- OS - Peterson's Solution
- OS - Monitors
- OS - Sleep and Wake
- OS - Race Condition
- OS Deadlock
- Introduction to Deadlock in Operating System
- Conditions for Deadlock in Operating System
- Memory Management
- OS - Memory Management
- OS - Contiguous Memory Allocation
- OS - Non-Contiguous Memory Allocation
- OS - First Fit Algorithm
- OS - Next Fit Algorithm
- OS - Best Fit Algorithm
- OS - Worst Fit Algorithm
- OS - Fragmentation
- OS - Virtual Memory
- OS - Segmentation
- OS - Buddy System
- OS - Allocating Kernel Memory
- OS - Overlays
- Paging and Page Replacement
- OS - Paging
- OS - Demand Paging
- OS - Page Table
- OS - Page Replacement Algorithms
- OS - Optimal Page Replacement Algorithm
- OS - Belady's Anomaly
- OS - Thrashing
- Storage and File Management
- OS - File Systems
- OS - File Attributes
- OS - Structures of Directory
- OS - Linked Index Allocation
- OS - Indexed Allocation
- I/O Systems
- OS - I/O Hardware
- OS - I/O Software
- OS Types
- OS - Types
- OS - Batch Processing
- OS - Multiprocessing
- OS - Hybrid
- OS - Monolithic
- OS - Zephyr
- OS - Nix
- OS - Linux
- OS - Blackberry
- OS - Garuda
- OS - Tails
- OS - Clustered
- OS - Haiku
- OS - AIX
- OS - Solus
- OS - Tizen
- OS - Bharat
- OS - Fire
- OS - Bliss
- OS - VxWorks
- OS - Embedded
- OS - Single User
- Miscellaneous Topics
- OS - Security
- OS Questions Answers
- OS - Questions Answers
- OS Useful Resources
- OS - Quick Guide
- OS - Useful Resources
- OS - Discussion
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?
- Steps to Implement Worst Fit Algorithm
- C++ Code for Worst Fit Algorithm
- Advantages of Worst Fit Algorithm
- Disadvantages of Worst Fit Algorithm
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.