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

Best Fit Program Using Memory Allocation-1

The document describes three different memory allocation algorithms - best fit, first fit, and worst fit. The best fit algorithm assigns each process to the memory block that minimizes fragmentation. It finds the block with the smallest remaining space after allocation for each process. The first fit algorithm assigns each process to the first block that is large enough. The worst fit algorithm assigns each process to the block that leaves the largest remaining space, maximizing fragmentation. It finds the block with the largest remaining space after allocation for each process.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views

Best Fit Program Using Memory Allocation-1

The document describes three different memory allocation algorithms - best fit, first fit, and worst fit. The best fit algorithm assigns each process to the memory block that minimizes fragmentation. It finds the block with the smallest remaining space after allocation for each process. The first fit algorithm assigns each process to the first block that is large enough. The worst fit algorithm assigns each process to the block that leaves the largest remaining space, maximizing fragmentation. It finds the block with the largest remaining space after allocation for each process.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Best fit program using Memory Allocation

#include<stdoio.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]);

}
Out put:

Memory Management Scheme - Best Fit

Enter the number of blocks:5

Enter the number of processes:5

Enter the size of the blocks:-

Block no.1:200

Block no.2:300

Block no.3:400

Block no.4:500

Block no.5:600

Enter the size of the processes :-

Process no.1:250

Process no.2:365

Process no.3:456

Process no.4:567

Process no.5:645
Process_no Process_size Block_no Block_size Fragment

1 250 2 300 50

2 365 3 400 35

3 456 4 500 44

4 567 5 600 33

--------------------------------

Process exited after 58.08 seconds with return value 0

Press any key to continue . . .

First fit: program using Memory Allocation

#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]);

for(i = 0; i < pno; i++) //allocation as per first fit

for(j = 0; j < bno; j++)

if(flags[j] == 0 && bsize[j] >= psize[i])

allocation[j] = i;

flags[j] = 1;

break;

//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: 8

10

12

Enter no. of processes: 3

Enter size of each process: 56

14

12

Block no. size process no. size

1 8 Not allocated

2 10 Not allocated
3 12 3 12

--------------------------------

Process exited after 18 seconds with return value 3

Press any key to continue . . .

Worst Fit program using Memory Allocation

#include<stdio.h>

#include<conio.h>

#define max 25
void main()

int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;

static int bf[max],ff[max];

printf("\nEnter the number of blocks:");

scanf("%d",&nb);

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

scanf("%d",&nf);

printf("\nEnter the size of the blocks:-\n");

for(i=1;i<=nb;i++)

printf("Block %d:",i);

scanf("%d",&b[i]);

printf("Enter the size of the files:-\n");

for(i=1;i<=nf;i++){
printf("File %d:",i);

scanf("%d",&f[i]);

for(i=1;i<=nf;i++)

for(j=1;j<=nb;j++)

if(bf[j]!=1) //if bf[j] is not allocated

temp=b[j]-f[i];

if(temp>=0)

if(highest<temp)

ff[i]=j;

highest=temp;

}
Output:

Enter the number of blocks:4

Enter the number of files:4

Enter the size of the blocks:-

Block 1:40

Block 2:56

Block 3:76

Block 4:56

Enter the size of the files:-

File 1:25

File 2:43

File 3:54

File 4:65
File_no File_size Block_no Block_size Fragment

1 25 3 76 51

2 43 2 56 13

3 54 4 56 2

4 65 0 4 0

frag[i]=highest;

bf[ff[i]]=1;
highest=0;

printf("\nFile_no \tFile_size \tBlock_no \tBlock_size \


tFragment");

for(i=1;i<=nf;i++)

printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);

getch();

You might also like