Algorithm and Implementation of contiguous allocation techniques: Worst- Fit using c
In order to implement Best fit contiguous allocation techniques there is certain requirement first we need Block size, Number of Block that is m, Number of Process is n, Size of each and every process
Let Blocksize is the array of size m.
Let SizeofProcess is the array of size n.
Let AllocationStatus is an array of size n.
it is the function we are going to use Best_fitCAT(int Blocksize[ ],int SizeofProcess[ ],AllocationStatus[ ])
1- Initilze the Blocksize with size and SizeofProcess with size.
2- Initialize all AllocationStatus as free.
Note we going to use -1 to indicate the status empty otherwise index of block
3- First take one process and check for the block in which it can be assigned with maximum internal fragmentation. for each block
- If SizeofProcess <= BlockSize then
- Store that index in temp variable and the find index of next worst fitted block after that
- Assign the Block to the process and update the status of process in AllocationStatus,
- Reduce the Size of Block by Sizeof Process
- Check for next process.
5- check is allocation status for all process is allocated then Print otherwise simple not allocated
Now lets implement the above algorithm in C.
void Worst_fitCAT(int Blocksize[ ],int SizeofProcess[ ],int AllocationStatus[ ],int m,int n)
int worstplace = -1;
for (int j=0; j<m; j++)
if (Blocksize[j] >= SizeofProcess[i])
if (worstplace == -1)
worstplace = j;
else if (Blocksize[worstplace] < Blocksize[j])
worstplace = j;
// If we find a block for current process
if (worstplace != -1)
// allocate block to process
AllocationStatus[i] = worstplace;
// Reduce the block size.
Blocksize[worstplace] -= SizeofProcess[i];
printf(“Process is allocated to following blocksn”);
printf(“Process %d is not Allocated n”,j+1);
printf(“Process %d is Allocated to %dn”,j+1,AllocationStatus[j]+1);
printf(“Enter the number of Blockn”);
printf(“now enter the size of block”);
printf(“Enter the number of Processn”);
printf(“now enter the size required by Process”);