Wednesday, May 12, 2010

Memory Allocation Techniques using C program - First Fit, Best fit, Worst Fit

PROGRAM CODING:

#include
#include
int pr[10],np,nb;
struct memlist
{
int mempart;
struct memlist *next;
};
typedef struct memlist node;
node *head;
void create(node *p);
void firstfit(node *memlist);
void main()
{
int i,j,ch;
printf("\nenter the number of process");
scanf("%d",&np);
for(i=0;i{
printf("\nenter the %d process memory",i+1);
scanf("%d",&pr[i]);
}
printf("\nmemory block details");
head=(node *)malloc(sizeof(node));
create(head);
firstfit(head);
}
void firstfit(node *memlist)
{
int i,j,flag;
printf("\npid\tpr.mem\tbid\tblock\tnewblock");
for(i=0;i{
flag=0;
for(j=0;j{
if(pr[i]<=memlist->mempart)
{
printf("\n%d\t%d\t%d\t%d\t",i+1,pr[i],j+1,memlist->mempart);
memlist->mempart=memlist->mempart-pr[i];
printf("%d",memlist->mempart);
flag=1;
break;
}
memlist=memlist->next;
}
}
memlist=head;
if(flag==0)
{
printf("\n%d\t%d insufficient memory",i+1,pr[i]);
}
}
void create(node *memlist)
{
nb=1;
printf("enter the size ofthe block");
printf("\ntype -99 to end");
scanf("%d",&memlist->mempart);

if(memlist->mempart==-99)
{
memlist->next=NULL;
}
else
{
memlist->next=(struct memlist *)malloc(sizeof(node));
create(memlist->next);
nb++;
}
}











OUTPUT:

enter the number of process3

enter the 1 process memory100

enter the 2 process memory200

enter the 3 process memory300

memory block detailsenter the size ofthe block
type -99 to end300
enter the size ofthe block
type -99 to end200
enter the size ofthe block
type -99 to end100
enter the size ofthe block
type -99 to end-99

pid pr.mem bid block newblock
1 100 1 300 200
2 200 1 200 0
4 300 insufficient memory



















PROGRAM CODING:

#include
#include
int pr[10],np,nb;
struct memlist
{
int mempart2;
struct memlist *next;
};
typedef struct memlist node;
node *head;
void create(node *p);
void bestfit(node *memlist);
void main()
{
int i,j,ch;
printf("\nenter the number of process");
scanf("%d",&np);
for(i=0;i{
printf("\nenter the %d process memory",i+1);
scanf("%d",&pr[i]);
}
printf("\nmemory block details");
head=(node *)malloc(sizeof(node));
create(head);
bestfit(head);
}
void bestfit(node *memlist)
{
int i,k,j,flag,min,p;
printf("\nPID\tpr.mem\tBID\tblock\tnewblock");
printf("\n");
for(i=0;i{
flag=0;
p=0;
min=memlist->mempart2;
for(j=0;j{
if(((memlist->mempart2mempart2))||pr[i]>min)
{
min=memlist->mempart2;
p=j;
}
memlist=memlist->next;
}
memlist=head;
for(k=0;k{
memlist=memlist->next;
}
if(pr[i]<=memlist->mempart2)
{
printf("\n%d\t%d\t%d\t%d\t",i+1,pr[i],p+1,memlist->mempart2);
memlist->mempart2=memlist->mempart2-pr[i];
flag=1;
printf("%d",memlist->mempart2);
}
else
{
if(flag==0)
{
printf("\n%d\t%d insufficient memory",i+1,pr[i]);
}
}
memlist=head;
}
}
void create(node *memlist)
{
nb=1;
printf("enter the size ofthe block");
printf("\ntype -99 to end");
scanf("%d",&memlist->mempart2);

if(memlist->mempart2==-99)
{
memlist->next=NULL;
}
else
{
memlist->next=(struct memlist *)malloc(sizeof(node));
create(memlist->next);
nb++;
}
}#include
#include
int pr[10],np,nb;
struct memlist
{
int mempart2;
struct memlist *next;
};
typedef struct memlist node;
node *head;
void create(node *p);
void bestfit(node *memlist);
void main()
{
int i,j,ch;
printf("\nenter the number of process");
scanf("%d",&np);
for(i=0;i{
printf("\nenter the %d process memory",i+1);
scanf("%d",&pr[i]);
}
printf("\nmemory block details");
head=(node *)malloc(sizeof(node));
create(head);
bestfit(head);
}
void bestfit(node *memlist)
{
int i,k,j,flag,min,p;
printf("\nPID\tpr.mem\tBID\tblock\tnewblock");
printf("\n");
for(i=0;i{
flag=0;
p=0;
min=memlist->mempart2;
for(j=0;j{
if(((memlist->mempart2mempart2))||pr[i]>min)
{
min=memlist->mempart2;
p=j;
}
memlist=memlist->next;
}
memlist=head;
for(k=0;k{
memlist=memlist->next;
}
if(pr[i]<=memlist->mempart2)
{
printf("\n%d\t%d\t%d\t%d\t",i+1,pr[i],p+1,memlist->mempart2);
memlist->mempart2=memlist->mempart2-pr[i];
flag=1;
printf("%d",memlist->mempart2);
}
else
{
if(flag==0)
{
printf("\n%d\t%d insufficient memory",i+1,pr[i]);
}
}
memlist=head;
}
}
void create(node *memlist)
{
nb=1;
printf("enter the size ofthe block");
printf("\ntype -99 to end");
scanf("%d",&memlist->mempart2);

if(memlist->mempart2==-99)
{
memlist->next=NULL;
}
else
{
memlist->next=(struct memlist *)malloc(sizeof(node));
create(memlist->next);
nb++;
}
}




OUTPUT:

enter the number of process3

enter the 1 process memory300

enter the 2 process memory200

enter the 3 process memory500

memory block detailsenter the size ofthe block
type -99 to end500
enter the size ofthe block
type -99 to end200
enter the size ofthe block
type -99 to end300
enter the size ofthe block
type -99 to end-99

PID pr.mem BID block newblock

1 300 3 300 0
2 200 2 200 0
3 500 1 500 0









PROGRAM CODING:

#include
#include
int pr[10],np,nb;
struct memlist
{
int mempart2;
struct memlist *next;
};
typedef struct memlist node;
node *head;
void create(node *p);
void worstfit(node *memlist);
void main()
{
int i,j,ch;
printf("\nenter the number of process");
scanf("%d",&np);
for(i=0;i{
printf("\nenter the %d process memory",i+1);
scanf("%d",&pr[i]);
}
printf("\nmemory block details");
head=(node *)malloc(sizeof(node));
create(head);
worstfit(head);
}
void worstfit(node *memlist)
{
int i,k,j,flag,max,p;
printf("\nPID\tpr.mem\tBID\tblock\tnewblock");
printf("\n");
for(i=0;i{
flag=0;
p=0;
max=memlist->mempart2;
for(j=0;j{
if((memlist->mempart2>max)&&(pr[i]<=memlist->mempart2))
{
max=memlist->mempart2;
p=j;
}
memlist=memlist->next;
}
memlist=head;
for(k=0;k{
memlist=memlist->next;
}
if(pr[i]<=memlist->mempart2)
{
printf("\n%d\t%d\t%d\t%d\t",i+1,pr[i],p+1,memlist->mempart2);
memlist->mempart2=memlist->mempart2-pr[i];
flag=1;
printf("%d",memlist->mempart2);
}
else
{
if(flag==0)
{
printf("\n%d\t%d insufficient memory",i+1,pr[i]);
}
}
memlist=head;
}
}
void create(node *memlist)
{
nb=1;
printf("enter the size ofthe block");
printf("\ntype -99 to end");
scanf("%d",&memlist->mempart2);

if(memlist->mempart2==-99)
{
memlist->next=NULL;
}
else
{
memlist->next=(struct memlist *)malloc(sizeof(node));
create(memlist->next);
nb++;
}
}

OUTPUT:

enter the number of process3

enter the 1 process memory300

enter the 2 process memory200

enter the 3 process memory199

memory block detailsenter the size ofthe block
type -99 to end500
enter the size ofthe block
type -99 to end300
enter the size ofthe block
type -99 to end200
enter the size ofthe block
type -99 to end-99

PID pr.mem BID block newblock

1 300 1 500 200
2 200 2 300 100
3 199 1 200 1

No comments:

Post a Comment