program for MFT algoritham using c-languge


MFT ALGORITHM
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 10
int main()
{
int ma,bs,ps,tmp,sbn[MAX]={0},ebn[MAX]={0},count=0,i=0,k,ifrag[MAX]={0};
char ch;
clrscr();
printf("\nEnter total memory available (in MB)");
scanf("%d",&ma);
printf("\nEnter size of each block(in MB)");
scanf("%d",&bs);
while(ma)
{
printf("\nDo u have a program(y/n)");
fflush(stdin);
scanf("%c",&ch);
if((ch!='y') && (ch!='Y'))
{
printf("\nMemory available %d MB",ma);
break;
}
printf("\nEnter the size of program(inMB)");
scanf("%d",&ps);
if(ps>ma)
{
printf("\nMemory required %d\nBUt\nMemory available:%d",ps,ma);
break;
}
count++;
if(!i)
{
sbn[i]=0;
ebn[i]=(ceil((float)ps/bs))-1;
}
else
{
sbn[i]=ebn[i-1]+1;
ebn[i]=sbn[i]+(ceil((float)ps/bs))-1;
}
tmp=((ceil((float)ps/bs)*bs));
ifrag[i]=tmp-ps;
i++;
ma-=tmp;
printf("\nMemory allocated%dMB\nMemory available%dMB",tmp,ma);
printf("\nBlocls\tnum of blocks\tInternal fragmentation(inMB)");
for(k=0;k<count;k++)
printf("\n\n%d-%d\t\t%d\t\t%d",sbn[k],ebn[k],ebn[k]-sbn[k]+1,ifrag[k]);
}
getch();
return 1;
}

2 comments:

  1. a meaningfull mft with process removal is as follows:-
    MFT 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;
    }

    ReplyDelete