#include<stdio.h>
#define M 100
void allocate();
void display();
int ps[30],ch,avtm,os,tm,i,j,k,n,ts,tinf=0,pts[30],inf[30],start[30],end[30],npart[30],tavtm,c=1;
main()
{
printf("\nMultiProgramming With Fixed No. of Tasks \n\n");
printf("\nEnter Total Memory(in MB) : ");
scanf("%d",&tm);
printf("\nEnter The Size Of O.S(in MB) : ");
scanf("%d",&os);
avtm=tm-os;
printf("\nTotal Memory Available For Program Allocation(in MB) : %d ",avtm);
printf("\n\n1.Equal Partition\n\n2.Unequal Partition\n\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\nEnter The No Of Partitions: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
pts[i]=avtm/n;
printf("\n\nEach Partition Size Is : %d",pts[1]);
break;
case 2:
printf("\n\nEnter The No Of Partitions: ");
scanf("%d",&n);
printf("\n\nEnter The Size of %d Partitions(EACH) : ",n);
for(i=1;i<=n;i++)
scanf("%d",&pts[i]);
break;
case 3:
exit(0);
default:
printf("\n\nInvalid Choice ");
}
printf("\n\nEnter the no. of processes : ");
scanf("%d",&n);
do{
printf("\n\n1.Allocate\n2.Display\n3.Exit");
printf("\n\nEnter Your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
allocate();
break;
case 2:
display();
break;
case 3:
exit(0);
default:
printf("\n\nInvalid Choice ");
}
}while(ch!=4);
}
void allocate()
{
tavtm=avtm;
for(i=1,j=i;i<=n;i++)
{
if(tavtm==0)
{
printf("\n\nThere is No Further Memory For Process%d",i);
ps[i]=0;
break;
}
printf("\n\nEnter Size of Program(in MB): ");
scanf("%d",&ps[i]);
if(tavtm<ps[i])
printf("\n\nTotal Available Memory %d MB only ",tavtm);
else
{
if(ps[i]<=pts[j])
{
start[i]=j;
end[i]=j;
npart[i]=1;
printf("\nProcess Is Allocated In Partition %d",j);
inf[i]=pts[j]-ps[i];
printf("\n\nInternal fragmentation for process%d is : %d ",i,inf[i]);
tinf=tinf+inf[i];
tavtm=tavtm-pts[j];
j++;
}
else
{
ts=ps[i];
c=1;
while(ts>pts[j])
{
ts=ts-pts[j];
tavtm=tavtm-pts[j];
c++;
}
tavtm=tavtm-pts[j];
inf[i]=pts[j]-ts;
tinf=tinf+inf[i];
npart[i]=c;
start[i]=end[i-1]+1;
end[i]=start[i]+npart[i];
printf("\nProcess Is Allocated In Partitions : ");
for(k=1;k<=c;k++)
printf("%d ",j++);
printf("\n\nInternal fragmentation for process%d is : %d ",i,inf[i]);
}
}
}
printf("\n\nThe Total Fragmentation is : %d MB",tinf);
}
void display()
{
printf("\n\nPrograms With their Partitions And Fragmentation : ");
printf("\n\nPnum\tAllocatedMemory\t\tNoOfPart\tPartitions\tInternalFragmentation");
printf("\n-----------------------------------------------------------------------------------");
for(k=1;k<=n;k++)
printf("\n%d\t\t%d\t\t%d\t\t(%d->%d)/%d\t\t%d",k,ps[k],npart[k],start[k],end[k],n,inf[k]);
printf("\n\nThe Total Fragmentation Is : %d MB",tinf);
}
#define M 100
void allocate();
void display();
int ps[30],ch,avtm,os,tm,i,j,k,n,ts,tinf=0,pts[30],inf[30],start[30],end[30],npart[30],tavtm,c=1;
main()
{
printf("\nMultiProgramming With Fixed No. of Tasks \n\n");
printf("\nEnter Total Memory(in MB) : ");
scanf("%d",&tm);
printf("\nEnter The Size Of O.S(in MB) : ");
scanf("%d",&os);
avtm=tm-os;
printf("\nTotal Memory Available For Program Allocation(in MB) : %d ",avtm);
printf("\n\n1.Equal Partition\n\n2.Unequal Partition\n\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\nEnter The No Of Partitions: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
pts[i]=avtm/n;
printf("\n\nEach Partition Size Is : %d",pts[1]);
break;
case 2:
printf("\n\nEnter The No Of Partitions: ");
scanf("%d",&n);
printf("\n\nEnter The Size of %d Partitions(EACH) : ",n);
for(i=1;i<=n;i++)
scanf("%d",&pts[i]);
break;
case 3:
exit(0);
default:
printf("\n\nInvalid Choice ");
}
printf("\n\nEnter the no. of processes : ");
scanf("%d",&n);
do{
printf("\n\n1.Allocate\n2.Display\n3.Exit");
printf("\n\nEnter Your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
allocate();
break;
case 2:
display();
break;
case 3:
exit(0);
default:
printf("\n\nInvalid Choice ");
}
}while(ch!=4);
}
void allocate()
{
tavtm=avtm;
for(i=1,j=i;i<=n;i++)
{
if(tavtm==0)
{
printf("\n\nThere is No Further Memory For Process%d",i);
ps[i]=0;
break;
}
printf("\n\nEnter Size of Program(in MB): ");
scanf("%d",&ps[i]);
if(tavtm<ps[i])
printf("\n\nTotal Available Memory %d MB only ",tavtm);
else
{
if(ps[i]<=pts[j])
{
start[i]=j;
end[i]=j;
npart[i]=1;
printf("\nProcess Is Allocated In Partition %d",j);
inf[i]=pts[j]-ps[i];
printf("\n\nInternal fragmentation for process%d is : %d ",i,inf[i]);
tinf=tinf+inf[i];
tavtm=tavtm-pts[j];
j++;
}
else
{
ts=ps[i];
c=1;
while(ts>pts[j])
{
ts=ts-pts[j];
tavtm=tavtm-pts[j];
c++;
}
tavtm=tavtm-pts[j];
inf[i]=pts[j]-ts;
tinf=tinf+inf[i];
npart[i]=c;
start[i]=end[i-1]+1;
end[i]=start[i]+npart[i];
printf("\nProcess Is Allocated In Partitions : ");
for(k=1;k<=c;k++)
printf("%d ",j++);
printf("\n\nInternal fragmentation for process%d is : %d ",i,inf[i]);
}
}
}
printf("\n\nThe Total Fragmentation is : %d MB",tinf);
}
void display()
{
printf("\n\nPrograms With their Partitions And Fragmentation : ");
printf("\n\nPnum\tAllocatedMemory\t\tNoOfPart\tPartitions\tInternalFragmentation");
printf("\n-----------------------------------------------------------------------------------");
for(k=1;k<=n;k++)
printf("\n%d\t\t%d\t\t%d\t\t(%d->%d)/%d\t\t%d",k,ps[k],npart[k],start[k],end[k],n,inf[k]);
printf("\n\nThe Total Fragmentation Is : %d MB",tinf);
}
Made it much more simpler the MFT process with removal:-
ReplyDeleteMFT WITH REMOVAL OF PROCESS:-
#include
#include
#include
#define max 20
struct memorychunk
{
int size_of_process;
int process_id;
} process[max];
int main()
{
int memorysize,partitions,size,no_of_processes,i,internal_fragmentation[10],total_internal_fragmentation=0,remaining_size,rid;
char answer;
printf("enter the size of the total memory available:");
scanf("%d",&memorysize);
printf("\nenter the number of partitions required to be made:");
scanf("%d",&partitions);
size=(memorysize/partitions);
printf("\ntotal size of each partition is:%d",size);
printf("\nenter the number of processes that are ready to occupy the main memory:");
scanf("%d",&no_of_processes);
for(i=0;i<no_of_processes;i++)
{
printf("\nenter the size of the process%d being entered :",i);
scanf("%d",&process[i].size_of_process);
printf("\nenter the process id:");
scanf("%d",&process[i].process_id);
if(process[i].size_of_process<=size)
{
printf("\nmemory allocated");
internal_fragmentation[i]=(size-process[i].size_of_process);
printf("\ninternal fragmentation of process %d is %d",i,internal_fragmentation[i]);
remaining_size=memorysize-process[i].size_of_process;
memorysize=remaining_size;
total_internal_fragmentation=(total_internal_fragmentation+internal_fragmentation[i]);
}
else
{
printf("memory not allocated for process %d due to overbound of size",i);
}
}
printf("\nDo you want any process to be removed? press Y to delete and press N to continue");
//getchar(answer);
scanf("%c",&answer);
scanf("%c",&answer);
if(answer=='Y'||answer=='y')
{
printf("\nenter the process id :");
scanf("%d",&rid);
for(i=0;i<no_of_processes;i++)
{
if(rid==process[i].process_id)
{
remaining_size=remaining_size+process[i].size_of_process;
process[i].size_of_process=0;
printf("\nthe available space after removal of process is%d",remaining_size);
}
}
}
printf("\nthe total internal fragmentation of the total memory before removal of the process is%d",total_internal_fragmentation);
return 0;
}