First fit in C

First Fit

The operating system uses different memory management schemes to optimize resource allocation. The responsibility of these schemes is to allocate memory chunks based on the demand by the operating system. The three most commonly used allocation schemes are first to fit, best fit, and worst fit.

The first fit memory allocation scheme checks the empty memory block in a sequential manner. This means that the memory block found empty at the first attempt is checked for size; if the size is not less than the required size, it is allocated. One of the biggest issues in this memory allocation scheme is, when a process is allocated to a comparatively larger space than needed, it creates huge chunks of memory space.

Read Also – First Fit algorithm in C++

Program for First Fit Algorithm in C

void main()
	//declaration and initialization of variables
int blocksize[10], process_size[10], block_no, process_no, flags[10], allocate[10], i, j;
	for(i = 0; i < 10; i++)

		flags[i] = 0;
		allocate[i] = -1;
	    //code to define block size
               printf("\nEnter block size: ");
	   for(i = 0; i < block_no; i++)
               scanf("%d", &blocksize[i]);
             //code to define processes
              printf("\nEnter the number of processes: ");
 	       scanf("%d", &process_no);

              printf("Enter the number of blocks: ");

              scanf("%d", &block_no);

	printf("\nEnter size of each process: ");
	for(i = 0; i < process_no; i++)
		scanf("%d", &process_size[i]);
	//allocate as per first fit
                for(i = 0; i < process_no; i++)         
		for(j = 0; j < block_no; j++) if(flags[j] == 0 && blocksize[j] >= process_size[i])
				allocation[j] = i;
				flags[j] = 1;
	//code to show details for allocation

	printf("\nBlock no.\tsize\t\tprocess no.\t\tsize");
	for(i = 0; i < block_no; i++)
		printf("\n%d\t\t%d\t\t", i+1, blocksize[i]);
		if(flags[i] == 1)

			printf("Not allocated");
Output: Enter the number of blocks: 3 Enter the size of each block: 12 7 4 Enter the number of processes: 3 Enter the size of each process: 7 4 9 Block no. Size Process No. Size 1 12 1 7 2 7 2 4 3 4 Not Allocated

Please Login/Signup to comment