First Fit Algorithm in OS

First Fit in Operating System

The operating system uses different memory management schemes to optimize memory/resource block allocation to different processes. We will look at one of such memory allocation processes in OS called First Fit in Operating System. In this approach we allot the first available space that is large enough to accommodate the process.

first

Different types of Memory Allocations in OS?

The four most commonly used allocation schemes are

How first fit works?

Whenever a process (p1) comes with memory allocation request the following happens –

  • OS sequentially searches available memory blocks from the first index
  • Assigns the first memory block large enough to accommodate process

Whenever a new process P2 comes, it does the same thing. Search from the first index again.

First Fit Allocation in OS

Working Example for First Fit

Example: As shown on the right/below image

Memory blocks available are : {100, 50, 30, 120, 35}

Process P1, size: 20

  • OS Searches memory from sequentially from starting
  • Block 1 fits, P1 gets assigned to block 1

Process P2, size: 60

  • OS Searches memory sequentially from block 1 again
  • Block 1 is unavailable, Block 2 and 3 can’t fit
  • Block 4 fits, p2 assigned to block 4

Process P3, size: 70

  • OS Searches memory sequentially from block 1 again
  • Block 1 is unavailable, Block 2, 3 can’t fit. Block 4 unavailable, Block 5 can’t fit
  • P3 remains unallocated

Similarly, P4 is assigned to block 2

Advantages

  • Easy to implement
  • OS can allocate processes quickly as algorithm to allocate processes will be quick as compared to other methods (Best Fit, Worst Fit, Next Fit etc)

Disadvantages

  • Causes huge internal fragmentation
  • Smarter allocation may be done by best-fit algorithm
  • High chances of unallocated for some processes due to poor algorithm
  • More overhead as compared to next fit

First Fit Programs

  1. Program in C | C++ | Java | Python

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription