0% found this document useful (0 votes)
3 views

10. Memory Allocation Methods

The document contains three implementations of memory allocation algorithms in C: First Fit, Best Fit, and Worst Fit. Each algorithm allocates memory blocks to processes based on different strategies to minimize fragmentation. The code examples demonstrate how each algorithm works by taking user input for block sizes and process sizes, and then displaying the allocation results.

Uploaded by

meena meenu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

10. Memory Allocation Methods

The document contains three implementations of memory allocation algorithms in C: First Fit, Best Fit, and Worst Fit. Each algorithm allocates memory blocks to processes based on different strategies to minimize fragmentation. The code examples demonstrate how each algorithm works by taking user input for block sizes and process sizes, and then displaying the allocation results.

Uploaded by

meena meenu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

i.

First Fit
#include<stdio.h>
void firstFit(int blockSize[], int m, int processSize[], int n)
{
int i, j;
int allocation[n];
for(i = 0; i< n; i++)
{
allocation[i] = -1;
}
for (i = 0; i< n; i++)
{
for (j = 0; j < m; j++)
{
if (blockSize[j] >= processSize[i])
{
allocation[i] = j;
blockSize[j] -= processSize[i];
break;
}
}
}
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i< n; i++)
{
printf(" %i\t\t\t", i+1);
printf("%i\t\t\t\t", processSize[i]);
if (allocation[i] != -1)
printf("%i", allocation[i] + 1);
else
printf("Not Allocated");
printf("\n");
}
}
int main()
{
int m;
int n;
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
m = sizeof(blockSize) / sizeof(blockSize[0]);
n = sizeof(processSize) / sizeof(processSize[0]);
firstFit(blockSize, m, processSize, n);
return 0 ;
}

ii. Best Fit

#include<stdio.h>
void main()
{
int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
static int barray[20],parray[20];
printf("\n\t\t\tMemory Management Scheme - Best Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of processes:");
scanf("%d",&np);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block no.%d:",i);
scanf("%d",&b[i]);
}
printf("\nEnter the size of the processes :-\n");
for(i=1;i<=np;i++)
{
printf("Process no.%d:",i);
scanf("%d",&p[i]);
}
for(i=1;i<=np;i++)
{
for(j=1;j<=nb;j++)
{
if(barray[j]!=1)
{
temp=b[j]-p[i];
if(temp>=0)
if(lowest>temp)
{
parray[i]=j;
lowest=temp;
}
}
}
fragment[i]=lowest;
barray[parray[i]]=1;
lowest=10000;
}
printf("\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment");
for(i=1;i<=np &&parray[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,p[i],parray[i],b[parray[i]],fragment[i]);
}

iii. Worst Fit

#include<stdio.h>
void implimentWorstFit(int blockSize[], int blocks, int processSize[], int
processes)
{
int allocation[processes];
int occupied[blocks];
for(int i = 0; i< processes; i++){
allocation[i] = -1;
}
for(int i = 0; i< blocks; i++){
occupied[i] = 0;
}
for (int i=0; i< processes; i++)
{
int indexPlaced = -1;
for(int j = 0; j < blocks; j++)
{
if(blockSize[j] >= processSize[i] && !occupied[j])
{
if (indexPlaced == -1)
indexPlaced = j;
else if (blockSize[indexPlaced] <blockSize[j])
indexPlaced = j;
}
}
if (indexPlaced != -1)
{
allocation[i] = indexPlaced;
occupied[indexPlaced] = 1;
blockSize[indexPlaced] -= processSize[i];
}
}
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i< processes; i++)
{
printf("%d \t\t\t %d \t\t\t", i+1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n",allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
int main()
{
int blockSize[] = {100, 50, 30, 120, 35};
int processSize[] = {40, 10, 30, 60};
int blocks = sizeof(blockSize)/sizeof(blockSize[0]);
int processes = sizeof(processSize)/sizeof(processSize[0]);
implimentWorstFit(blockSize, blocks, processSize, processes);
return 0;
}

You might also like