![]() |
VOOZH | about |
Prerequisite : Partition Allocation Methods
In the first fit, the partition is allocated which is first sufficient from the top of Main Memory.
Example :
Input : blockSize[] = {100, 500, 200, 300, 600};
processSize[] = {212, 417, 112, 426};
Output:
Process No. Process Size Block no.
1 212 2
2 417 5
3 112 2
4 426 Not Allocated
Implementation:
1- Input memory blocks with size and processes with size.
2- Initialize all memory blocks as free.
3- Start by picking each process and check if it can
be assigned to current block.
4- If size-of-process <= size-of-block if yes then
assign and check for next process.
5- If not then keep checking the further blocks.
Below is an implementation of above steps.
Output :
Process No. Process Size Block no.
1 212 2
2 417 5
3 112 2
4 426 Not Allocated
Time complexity of First Fit algorithm is O(n*m), where n is the number of processes and m is the number of memory blocks. The outer for loop runs for n times and the inner for loop runs for m times.
Auxiliary Space of First Fit algorithm is O(n), where n is the number of processes. The allocation array is used to store the block number allocated to the process, which takes a space of O(n).