Worst Fit Algorithm C Program

By | November 4, 2016

Let us learn how to implement Worst Fit Algorithm in C programming language. The memory management program for Worst Fit Algorithm uses Arrays.

What is Worst Fit Algorithm?

The Worst Fit Memory Allocation Algorithm allocates the largest free partition available in the memory that is sufficient enough to hold the process within the system.

It searches the complete memory for available free partitions and allocates the process to the memory partition which is the largest out of all.

This algorithm is not recommended to be implemented in the real world as it has many disadvantages.

A process entering first may be allocated the largest memory space but if another process of larger memory requirement is to be allocated, space cannot be found. This is a serious drawback here.

C Program To Implement Worst Fit Algorithm in OS


int main()
      int fragments[10], blocks[10], files[10];
      int m, n, number_of_blocks, number_of_files, temp, top = 0;
      static int block_arr[10], file_arr[10];
      printf("\nEnter the Total Number of Blocks:\t");
      printf("Enter the Total Number of Files:\t");
      printf("\nEnter the Size of the Blocks:\n");
      for(m = 0; m < number_of_blocks; m++) 
            printf("Block No.[%d]:\t", m + 1);
            scanf("%d", &blocks[m]);
      printf("Enter the Size of the Files:\n");
      for(m = 0; m < number_of_files; m++) 
            printf("File No.[%d]:\t", m + 1);
            scanf("%d", &files[m]);
      for(m = 0; m < number_of_files; m++)
            for(n = 0; n < number_of_blocks; n++)
                  if(block_arr[n] != 1)
                        temp = blocks[n] - files[m];
                        if(temp >= 0)
                              if(top < temp)
                                    file_arr[m] = n;
                                    top = temp;
                  fragments[m] = top;
                  block_arr[file_arr[m]] = 1;
                  top = 0;
      printf("\nFile Number\tFile Size\tBlock Number\tBlock Size\tFragment");
      for(m = 0; m < number_of_files; m++)
            printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, files[m], file_arr[m], blocks[file_arr[m]], fragments[m]);
      return 0;


C Program To Implement Worst Fit Algorithm For Memory Management

If you have any doubts or compilation errors in this C program to implement Worst Fit Memory Segment Algorithm in operating system, let us know about it in the comment section below.

Recommended Programs
C Program To Implement First Fit Algorithm For Memory Management
C Program To Implement Best Fit Algorithm For Memory Management
C Program To Implement Next Fit Algorithm For Memory Management
C Program For Least Recently Used Page Replacement Algorithm
C Program For Non Preemptive Shortest Job First Algorithm
C Program To Find Execution Time of a Program
C Program To Convert Decimal To Hexadecimal Number
C Program For Tower of Hanoi Algorithm
C Program To Find Permutations of a String
C Program To Evaluate Prefix Expression using Stack
C Program For Round Robin Scheduling Algorithm

3 thoughts on “Worst Fit Algorithm C Program

  1. Mahesh Asalkar

    Thanks for the explanation. It helped to understand the concept in a better way.


Let's Discuss