MVT C program

#include<stdio.h>
#define M 100
void allocate();
void deallocate();
void display();
int tm,os,tavtm,avtm,ch,ps,psa[M],start[M],end[M],hstart[M],hend[M],holes[M];
int p=1,i=1,pc=0,hc=1,k=1,flag=0;

main()
{
    printf("\nMultiProgramming With Variable No. of Tasks \n\n");
    printf("\nEnter Total Memory(in MB) : ");
    scanf("%d",&tm);
    printf("\n\nEnter The Size Of O.S(in MB) : ");
    scanf("%d",&os);
    avtm=tm-os;
    tavtm=avtm;
    printf("\n\nTotal Memory Available For Program Allocation(in MB) : %d ",avtm);
    hstart[hc]=1;
    hend[hc]=avtm;
    holes[hc]=avtm;
    do{
     printf("\n\n1.Allocate\n2.Deallocate\n3.Display\n4.Exit");
     printf("\n\nEnter Your choice : ");
     scanf("%d",&ch);
     switch(ch)
     {
                case 1:
                    allocate();
                    break;

                case 2:
                    deallocate();
                    break;

                case 3:
                    display();
                    break;

                case 4:
                    exit(0);

                default:
                    printf("\nInvalid Choice ");
        }
     }while(ch!=4);

}
void allocate()
{
    printf("\n\nEnter Size of Program(in MB): ");
    scanf("%d",&ps);
    printf("\n\nAccording To BestFit Algorithm");
    for (i=1;i<=hc;i++)
        if (holes[i]>=ps)
            break;
    if(i==hc+1)
        printf("\n\nMemory Not Enough For Your Program ");
    else
    {
        k=i;
        for (i=k+1;i<=hc;i++)
        {
            if(holes[i]>=ps&&holes[i]<holes[k])
                    k=i;
        }
        pc++;
        psa[pc]=ps;
        start[pc]=hstart[k];
        end[pc]=hstart[k]+ps-1;
        hstart[k]=end[pc]+1;
        holes[k]=holes[k]-ps;
        if(hstart[k]==hend[k]+1)
            hstart[k]=hend[k]=holes[k]=-1;
        avtm-=ps;
        printf("\n\nProgram Allocated : %d->%d  ",start[pc],end[pc]);
        if(!avtm)
            hc=0;

    }
}
void deallocate()
{
    printf("\n\nEnter The Program ID U Want To Delete : ");
    scanf("%d",&ch);
    if(ch>pc)
    {
        printf("\n\n No Such Program Exists  ");
        return;
    }
    printf("\n\nA Hole Is Created B/W Memory Locations  %d & %d",start[ch],end[ch]);
    hc++;
    hstart[hc]=start[ch];
    hend[hc]=end[ch];
    holes[hc]=psa[ch];
    avtm=avtm+psa[ch];
    start[ch]=end[ch]=psa[ch]=-1;
}
void display()
{
    printf("\n\nPrograms With their Locations : ");
    printf("\n\nPnum\t\tMemloc\tSize");
    printf("\n---------\t-------\t--------");
    for(i=1;i<=pc;i++)
        printf("\n%d\t\t%d->%d\t%d",i,start[i],end[i],psa[i]);
    printf("\n\nThe Memory Available Is %d MB",avtm);
    if(hc)
    {
        printf("\n\nHoles Formed In Memory With Their Locations \n");
        printf("\n\nHnum\t\tMemloc\tSize");
        printf("\n---------\t------\t--------");
        for(i=1;i<=hc;i++)
            printf("\n\n%d\t\t%d->%d\t%d",i,hstart[i],hend[i],holes[i]);
    }
    printf("\n\nThe External Fragmentation Is %d MB",avtm);
}
SHARE

About Santhosh

    Blogger Comment
    Facebook Comment

2 comments: