- 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 - First Fit Algorithm
The First Fit Algorithm is a memory allocation strategy used in contiguous memory allocation systems. This chapter will explain the First Fit Algorithm, how it works, and its implementation in operating systems.
- What is First Fit Algorithm?
- Steps to Implement First Fit Algorithm
- C++ Code for First Fit Algorithm
- Advantages of First Fit Algorithm
- Disadvantages of First Fit Algorithm
What is First Fit Algorithm?
The First Fit Algorithm allocates the first available block of memory that is large enough to accommodate the process. Meaning, the algorithm scans the memory from the starting and allocates the first block that is big enough for the processes need. If no suitable block is found, the process is marked as "Not Allocated". So the process needs to wait until a suitable block becomes free.
Example
Consider the following example where we have three memory blocks of sizes 6, 12, and 25 units, and three processes of sizes 12, 25, and 30 units respectively. We will allocate memory to these processes using the First Fit Algorithm.
Input : blockSize[] = {6, 12, 25}; processSize[] = {12, 25, 30}; Output: Process No. Process Size Block no. 1 12 2 2 25 3 3 30 Not Allocated; The process size 30 cannot be allocated as there is no block large enough to accommodate it.
Steps to Implement First Fit Algorithm
The First Fit Algorithm can be implemented using the following steps:
- Initialize a pointer to keep track of the last allocated block.
- For each process, start searching for a suitable hole from the beginning of the memory.
- If a suitable hole is found, allocate the process to that hole.
- On reaching the end of the memory, if no suitable hole is found, mark the process as "Not Allocated".
C++ Code for First Fit Algorithm
Below is a sample C++ code that demonstrates the implementation of the First Fit Algorithm −
#include <iostream> using namespace std; void firstFit(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++) { for (int j = 0; j < m; j++) { if (blockSize[j] >= processSize[i]) { allocation[i] = j; blockSize[j] -= processSize[i]; break; } } } cout << "Process No.\tProcess Size\tBlock no." << endl; for (int i = 0; i < n; i++) { cout << " " << i + 1 << "\t\t" << processSize[i] << "\t\t"; if (allocation[i] != -1) cout << allocation[i] + 1; else cout << "Not Allocated"; cout << endl; } } int main() { int blockSize[] = {6, 12, 25}; int processSize[] = {12, 25, 30}; int m = sizeof(blockSize) / sizeof(blockSize[0]); int n = sizeof(processSize) / sizeof(processSize[0]); firstFit(blockSize, m, processSize, n); return 0; } The output of the above code will be −
Process No. Process Size Block no. 1 12 2 2 25 3 3 30 Not Allocated
Advantages of First Fit Algorithm
The First Fit Algorithm is considered as a simple and efficient memory allocation strategy. Some of its advantages are:
- Speed − The First Fit Algorithm is generally faster than other algorithms like Best Fit and Worst Fit because it allocates the first suitable block it finds.
- No Complex Data Structures − The First Fit Algorithm does not require any complex data structures or any other complex calculations.
- Reduced Fragmentation − The First Fit Algorithm leaves larger gaps in memory compared to other algorithms. This will help in reducing fragmentation.
Disadvantages of First Fit Algorithm
Sometimes, the First Fit Algorithm is not preferred, due to the following drawbacks −
- Wasted Memory − The First Fit Algorithm can lead to wastage of memory as it may leave large unusable gaps in memory.
- Poor Utilization − The First Fit Algorithm is considered as worst in term of memory utilization compared to other algorithms like Best Fit and Worst Fit.
- Not Optimal − The First Fit Algorithm does not guarantee optimal memory allocation. It may lead to situations where a process cannot be allocated memory even though there is enough total memory available.
Conclusion
The First Fit Algorithm is a simple algorithm for memory allocation in operating systems. It is fast and easy to implement. The idea behind the algorithm is to allocate the first available block of memory that is large enough to accommodate the process. However, it comes with certain drawbacks like wasted memory and poor utilization.