×

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for each partition of the First Fit program. The operating system has three memory management schemes: First Fit, Worst Fit, and Best Fit.

What is First Fit?

 Empty memory blocks are checked sequentially with the first matching memory allocation. This indicates that the memory block size found to be empty on the first try is being checked. However, if the size is greater than or equal to the required size, it will be allocated.

Advantage:

  • This is the fastest search algorithm because it doesn't need to search every block.

Disadvantages:

  • The main drawback of First Fit is that other processes cannot use the extra memory. Allocating memory creates large chunks of disk space.

Memory Management Plan for the First Fit

The First Fit memory management scheme compares the size of the first process to the size of the first block when checking blocks. Only this block is allocated if its size is smaller than the first block. If not, go to the second block and continue processing until all processes are allocated.

The partition that is first sufficient from the top of the Main Memory is allocated during the first fit.

It has the advantage of being the quickest search because it only searches the first block, which is sufficient to assign a process.

Even if space could be allocated, it might have issues with processes taking up space.Take process number 4 (with a size of 426) as an example. It does not receive memory.However, we could allocate the memory if we used best fit allocation to allocate blocks 4 (of size 300) to process 1, 2 to process 2, 3 to process 3, and 5 to process 4.

First Fitting Algorithm

Step 1: Get started.

Step 2: First, determine the number of processes and blocks.

Step 3: Allocate the process if the block size is larger than the process size. Otherwise, proceed to the next block.

Step 4: View the process in blocks and allocate resources to each.

Step 5: Stop. We talked about how it works and the algorithms.

Here is the First Fit program in C:

#include<stdio.h>
void main()
{
	int bsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j;
	for(i = 0; i< 10; i++)
	{
		flags[i] = 0;
		allocation[i] = -1;
	}
	printf("Enter no. of blocks: ");
	scanf("%d", &bno);
	printf("\nEnter size of each block: ");
	for(i = 0; i<bno; i++)
		scanf("%d", &bsize[i]);
	printf("\nEnter no. of processes: ");
	scanf("%d", &pno);
	printf("\nEnter size of each process: ");
	for(i = 0; i<pno; i++)
		scanf("%d", &psize[i]);
//display allocation details
	printf("\nBlock no.\tsize\t\tprocess no.\t\tsize");
for(i = 0; i<bno; i++)
	{
		printf("\n%d\t\t%d\t\t", i+1, bsize[i]);
		if(flags[i] == 1)
			printf("%d\t\t\t%d",allocation[i]+1,psize[allocation[i]]);
		else
			printf("Not allocated");
	}
}


Output:

Enter no. of blocks: 3
Enter size of each block. 10
15
20
Enter no. of processes: 3
Enter size of each process : 14
12 
18
Block no.             size                   process no.               size
1                           10                     not allowed               
2                           15                    1                                  14
3                           20                    2                                  12

One of themost important functions of an operating system is memory allocation. Memory is also required for various processes that consume CPU processing time. This memory is allocated to various processes as needed. The three most common memory allocation schemes are best t fit, initial fit, and worst fit, what is Best Fit?

The operating system looks for the bestmemory allocation scheme for process p1: An empty block of memorythat can accommodate process P1. After allocating process p1 from all blocks, the memory waste is minimal. This plan is the most effective strategy because it provides optimal memory allocation. However, finding the optimal memory allocation can be time-consuming.

The first-fit algorithm is the easiest way to have all processesshare memory blocks. Pointers accept requests to allocate blocks of memoryto subsequent processes, and the algorithm keeps track of all available blocks in memory. The pointer then searches for the first largestfree memory block in the process and passes this memory block to the next process. This creates two partitions, one for the hall and one for the process.


Related Topics

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

3 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.

Bubble sort in C

Bubble sort in C Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor...

4 minutes read.

Typecast vs. typedef in C

Typecast vs. typedef in C Typecast In the C programming language, converting the data type from one form to another is known as type casting or the type conversion. It is a...

5 minutes read.

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 minutes read.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

4 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.

Recursion in C

Recursion is a programming technique that allows the programmer to call the function within the same function. The function which calls the same function is known as recursive function but a...

1 minute read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.Generally, as we know there are three main components of while loop:1. The...

3 minutes read.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

While Loop in C programming examples

Introduction There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs. The following statement describes how to use/add header file...

22 minutes read.