Worst Fit Algorithm Program In C

The processes need empty memory slots during processing time. This memory is allocated to the processes by the operating system which decides depending on the free memory and the demanded memory by the process in execution.

In the case of the worst fit memory allocation scheme, the operating system searches for free memory blocks demanded by the operating system. An empty block is assigned to the processes as soon as the CPU identifies it. The scheme is also said as the worst fit memory management scheme as sometimes a process is allocated a memory block which is much larger to the actual demand resulting in a huge amount of wasted memory.

The operating system conducts this memory allocation using an algorithm.

Program code for Worst Fit Memory management Scheme using C:

int frag[10], blocks[10], process[10];
 int m, n, block_number, process_number, temp, top = 0;
 static int block_arr[10], process_arr[10];
//Fill the number of blocks


 printf("\nEnter the Total Number of Blocks:\t");
 

scanf("%d",&block_number);

//Fill the number of processes
 
printf("Enter the Total Number of Processes:\t");

 
scanf("%d",&process_number);

//Fill the size of the blocks

printf("\nEnter the Size of the Blocks:\n");
for(m = 0; m < block_number; m++) 
      {

            printf("Block No.[%d]:\t", m + 1);


            scanf("%d", &blocks[m]);
      }
//Fill the size of the processes

printf("Enter the Size of the processes:\n");
      for(m = 0; m < process_number; m++) 
      {

        printf("process No.[%d]:\t", m + 1);

      scanf("%d", &processes[m]);

      }
//memory allocation to the processes
for(m = 0; m < process_number; m++)
      {
            for(n = 0; n < process_number; n++) { if(block_arr[n] != 1) { temp = blocks[n] - process[m]; if(temp >= 0)
                        {
                              if(top < temp)
                              {
                                   process_arr[m] = n;
                                    top = temp;

                              }
                        } 
                  }


                  frag[m] = top;
                  block_arr[process_arr[m]] = 1;
                  top = 0;

            }   
      }
//printing result after memory allocation
    printf("\nProcess Number\tProcess Size\tBlock Number\tBlock Size\tFragment");
      for(m = 0; m < process_number; m++)
      {

            printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, process[m], process_arr[m], blocks[process_arr[m]], fragments[m]);
      }


      printf("\n");
      return 0;
}

Output

Enter the total number of blocks:
5
Enter total number of processes
4
Enter the size of the blocks:
Block number [1]: 5
Block number [2]: 4
Block number [3]: 3
Block number [4]: 6
Block number [5]: 7
Enter the size of the processes:
Process number [1]: 1
Process number [2]: 3
Process number [3]: 5
Process number [4]: 3

Process Number Process SizeBlock NumberBlock SizeFragment
01476
13050
25050
33050

Please Login/Signup to comment