Friday, 8 May 2015

Memory Management Schemes-Bestfit

#include<stdio.h>
#define MAX 5
struct
{
int startbyte;
int endbyte;
int occupied;
}a[MAX];

void initialize();
void input();
void bestfit();
void display();

int main()
{
initialize();
input();
printf("\n before best fit allocation \n");
display();
bestfit();
printf("\n After best fit allocation\n");
display();
}

void initialize()
{
int i;
for(i=0;i<MAX;i++)
a[i].occupied=0;
}
void input()
{
int i,option,choice;

for(i=0;i<MAX;i++)
{
printf("\n Enter The Start byte and Endbyte address ");
scanf("%d %d",&a[i].startbyte,&a[i].endbyte);
}
}
void bestfit()
{
int i,space,hole,loc,besthole=9999;
printf("\n New Entry's space requirement \n");
scanf("%d",&space);

for(i=0;i<MAX;i++)
{
if(a[i].occupied==0)
{
hole=a[i].endbyte-a[i].startbyte;

if(space<hole)
if(besthole>hole)
{
besthole=hole;
loc=i;
}
}
}
if(besthole==9999)
{
printf("\n Space not available");
exit(0);
}
else
{
a[loc].endbyte=a[loc].startbyte+space;
a[loc].occupied=1;
}
}
void display()
{
int i;
printf("\n----------------------");
printf("\n Index   StartByte      Endbyte");
printf("\n----------------------");
for(i=0;i<MAX;i++)
printf("\n%d      %d      %d",i,a[i].startbyte,a[i].endbyte);
printf("\n");
}



Output:
   
 Enter The Start byte and Endbyte address 100 200
 Enter The Start byte and Endbyte address 200 255
 Enter The Start byte and Endbyte address 255 300
 Enter The Start byte and Endbyte address 300 320
 Enter The Start byte and Endbyte address 320 400

 before best fit allocation
------------------------------------------
 Index   StartByte      Endbyte
-------------------------------------------
0      100      200
1      200      255
2      255      300
3      300      320
4      320      400

 New Entry's space requirement
15
 After best fit allocation
--------------------------------------
 Index   StartByte      Endbyte
----------------------------------------
0      100      200
1      200      255
2      255      300
3      300      315

4      320      400

No comments:

Post a Comment