Write a C Program to Implement the Memory Management Scheme Using First Fit

Aim:

To write a C Program to Implement the Memory Management Scheme Using First Fit.

Algorithm:

  1. First Fit Algorithm
  2. Get no. of Processes and no. of blocks.
  3. After that get the size of each block and process requests.
  4. Now allocate processes
  5. if(block size >= process size)
  6. else
  7. Display the processes with the blocks that are allocated to a respective process.
  8. Stop.

Exercise:

  • Given memory partition of 100 KB, 500 KB, 200 KB, 300 KB and 600 KB (in order), how would each of the first-fit, best fit and worst fit algorithms place processes of 212 KB, 417 KB, 112 KB and 426 KB (in order)? Which algorithm makes the most efficient use of memory? Either it is first fit.

Program:

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
int main()
{
int n,m,i,count=0,j,pn[100];
int p[100],size[100];
bool flag[100];
printf("ENTER THE NO PROCESS AND MEMOR :\n ");
scanf("%d%d",&n,&m);
printf("ENTER THE SIZE OF PROCESS \n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("ENTER THE SIZE OF MEMOR PARTION \n\n");
for(i=0;i<m;i++)
{
scanf("%d",&size[i]);
flag[i]=0;
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(p[i]<=size[j]&&flag[j]==0)
{
flag[j]=true;
pn[j]=i;
count++;
j=j+m;
}
}
}
printf("NO OF PROCESS CAN ACOMADATE :%d\n\n",count);
printf("MEMOR\tPROCESS\n");
for(i=0;i<m;i++ )
{
if(flag[i]==1)
{
printf("%d <-->%d\n",size[i],p[pn[i]]);
}
else
printf("%d\tMEMOR NOT ALLOCATED\n",size[i]);
}
return 0;
}

Execution:

Input:
4 5 212 417 112 426 100 500 200 300 600


Output:

ENTER THE NO PROCESS AND MEMOR :
 ENTER THE SIZE OF PROCESS 
ENTER THE SIZE OF MEMOR PARTION 

NO OF PROCESS CAN ACOMADATE :3

MEMOR	PROCESS
100	MEMOR NOT ALLOCATED
500 <-->212
200 <-->112
300	MEMOR NOT ALLOCATED
600 <-->417

Result:

Thus Memory Management Scheme using the First Fit program was executed Successfully.

Leave a Comment