Thursday 29 September 2011


IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME USING BEST FIT

AIM:

To write a program to implement dynamic memory allocation best fit.

ALGORITHM:

1. Start the program.

2. Enter the number of blocks.

3. Enter the base address and size for each block.

4. Enter the size of process.

5. Compute and allocate the smallest block which is large enough to hold the process request.

6. Stop the program.


PROGRAM:
#include<stdio.h>
void line()
{
int i;
printf("\n");
for(i=0;i<80;i++)
printf("-");
}
void main()
{
int bsize[20],i,n,j,temp,psize,baddr[20];
line();
printf("\n\t\tBEST FIT PLACEMENT ALGORITHM\n");
line();
printf("\nEnter the number of blocks:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter base address of block no %d:",i+1);
scanf("%d",&baddr[i]);
printf("\nEnter free block size no. %d:",i+1);
scanf("%d",&bsize[i]);
}
printf("\nEnter the size of the process to be fit:");
scanf("%d",&psize);
line();
printf("\nBEFORE ALLOCATION:\n");
printf("\nBlock No. \tBase Address \tBlock Size");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",i+1,baddr[i],bsize[i]);
line();
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(bsize[j]>bsize[j+1])
{
temp=bsize[j];
bsize[j]=bsize[j+1];
bsize[j+1]=temp;
temp=baddr[j];
baddr[j]=baddr[j+1];
baddr[j+1]=temp;
}
for(i=0;i<n;i++)
if(bsize[i]<psize)
continue;
else
{
bsize[i]-=psize;
baddr[i]+=psize;
break;
}
if(i==n)
printf("\nThe process has not been fit");
else
{
printf("\nAFTER ALLOCATION:\n");
printf("\nBlock No. \tBase Address \t Block Size");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",i+1,baddr[i],bsize[i]);
}
line();
printf("\n");
}


OUTPUT:
[root@localhost ~]# ./best

--------------------------------------------------------------------------------
BEST FIT PLACEMENT ALGORITHM

--------------------------------------------------------------------------------
Enter the number of blocks:4

Enter base address of block no 1:10

Enter free block size no. 1:45

Enter base address of block no 2:60

Enter free block size no. 2:75

Enter base address of block no 3:150

Enter free block size no. 3:100

Enter base address of block no 4:300

Enter free block size no. 4:140

Enter the size of the process to be fit:50

--------------------------------------------------------------------------------
BEFORE ALLOCATION:

Block No.       Base Address    Block Size
1               10              45
2               60              75
3               150             100
4               300             140
--------------------------------------------------------------------------------
AFTER ALLOCATION:

Block No.       Base Address     Block Size
1               10              45
2               110             25
3               150             100
4               300             140
--------------------------------------------------------------------------------
[root@localhost ~]#

0 comments:

Post a Comment