#include<stdio.h>
#include<conio.h>
struct memory
{
int ind;
int prn;
int stpt;
int space;
struct memory *nxt;
}*start,*ptr,*ptrc,*loc,*locp,*np;
int ttl_space,rem_space;
int chk_avail(int num,int mem)
{
if(mem > rem_space)
return 0;
else
{
ptr = start;
while(ptr != NULL)
{
if(ptr->ind == 1 && ptr->prn == num)
return 0;
if(ptr->ind == 0 && ptr->space >= mem)
return 1;
ptr = ptr->nxt;
}
return 0;
}
}
void getstats()
{
ptr = start;
printf("\n Process Starting Point Memory Allocated \n");
printf(" ---------------------------------------------------------- \n");
while(ptr != NULL)
{
if(ptr->ind == 1)
printf("\n\t%d\t\t",ptr->prn);
else
printf("Free Space\t\t\n",ptr->prn);
printf("%d\t\t\t%d\n",ptr->stpt,ptr->space);
ptr = ptr->nxt;
}
printf("\n\t\t Total Memory Space : %d",ttl_space);
printf("\n\t\t Total Allocated Memory : %d",ttl_space-rem_space);
printf("\n\t\t Total Available Memory : %d",rem_space);
}
void first_fit(int mem)
{
loc = start;
locp = NULL;
while(loc != NULL)
{
if(loc->ind == 0 && loc->space >= mem)
break;
locp = loc;
loc = loc->nxt;
}
}
void best_fit(int mem)
{
int small = ttl_space;
loc = start;
locp = NULL;
while(loc != NULL)
{
if(loc->ind == 0 && loc->space >= mem && loc->space - mem < small )
{
ptrc = locp;
ptr = loc;
small = loc->space - mem;
}
locp = loc;
loc = loc->nxt;
}
locp = ptrc;
loc = ptr;
}
void worst_fit(int mem)
{
int big = 0;
loc = start;
locp = NULL;
while(loc != NULL)
{
if(loc->ind == 0 && loc->space >= mem && loc->space - mem > big )
{
ptrc = locp;
ptr = loc;
big = loc->space - mem;
}
locp = loc;
loc = loc->nxt;
}
locp = ptrc;
loc = ptr;
}
void allocate()
{
int cho,res,nm,mm;
do
{
printf("\n Enter process number : ");
scanf("%d",&nm);
printf("\n Enter memory space reqd. : ");
scanf("%d",&mm);
res = chk_avail(nm,mm);
}while(res == 0);
printf("\n Therefore, Process is unique and Reqd. Memory is availiable. ");
printf("\n\n\t By which method you want to allocate Memory ?? ");
printf("\n 1. First Fit\n 2. Best Fit\n 3. Worst Fit");
printf("\n\n\t Choose your Option : ");
scanf("%d",&cho);
switch(cho)
{
case 1 : first_fit(mm); break;
case 2 : best_fit(mm); break;
case 3 : worst_fit(mm); break;
default: printf("\n\n\t Try again later !! "); break;
}
if(cho>=1&&cho<=3)
{
np = new memory;
np->ind = 1;
np->prn = nm;
np->stpt = loc->stpt;
np->space = mm;
np->nxt = NULL;
if(loc == start)
{
np->nxt = start;
start = np;
}
else
{
locp->nxt = np;
np->nxt = loc;
}
if(loc->space != np->space)
{
loc->stpt = np->stpt + np->space;
loc->space = loc->space - np->space;
}
else
{
np->nxt = loc->nxt;
delete loc;
}
rem_space -= np->space;
getstats();
}
}
void deallocate()
{
int res,nm;
printf("\n Enter process number to be deallocated : ");
scanf("%d",&nm);
res = chk_avail(nm,ttl_space);
if(res != 0)
printf("\n\t\t Sorry !! Can't be deleted !!! \n");
else
{
locp = NULL;
loc = start;
while(loc != NULL)
{
if(loc->ind == 1 && loc->prn == nm)
break;
locp = loc;
loc = loc->nxt;
}
rem_space += loc->space;
loc->ind = 0;
locp = start;
loc = start->nxt;
while(loc != NULL)
{
if(locp->ind == 0 && loc->ind == 0 )
{
locp-> space += loc->space;
locp->nxt = loc->nxt;
delete loc;
loc = locp->nxt;
}
else
{
locp = loc;
loc = loc->nxt;
}
}
printf("\n\t Therefore, The Given Memory Space has been deallocated \n");
getstats();
}
}
void main()
{
int cho;
do
{
clrscr();
printf("\n Enter the maximum space in memory : ");
scanf("%d",&ttl_space);
}while(ttl_space<=0 || ttl_space >= 10000);
rem_space = ttl_space;
np = new memory;
np->stpt = np->ind = 0;
np->space = ttl_space;
np->nxt = NULL;
start = np;
do
{
getch();
clrscr();
printf("\n\t What would you like to do now in ?? \n");
printf("\n1. Allocate Memory by Inserting Process ");
printf("\n2. Deallocate Memory by Removing Process ");
printf("\n\n\t Choose your option : ");
scanf("%d",&cho);
switch(cho)
{
case 1 : allocate(); break;
case 2 : deallocate(); break;
default: printf("\n\t\t Thanks for Using The Program \n"); break;
}
}while(cho>=1&&cho<=2);
getch();
}
No comments:
Post a Comment