Monday, 21 October 2013

mem

#include<stdio.h>
void accept(int n,int nh,int holes[20],int p[20]);
void firstfit(int n,int nh,int holes[20],int p[20]);
void bestfit(int n,int nh,int holes[20],int p[20]);
void worstfit(int n,int nh,int holes1[20],int p[20]);
void nextfit(int n,int nh,int holes1[20],int p[20]);
void main()
{
    int ch,n,nh,holes[20],p[20];
    printf("\nEnter no. of processes: ");
    scanf("%d",&n);
    printf("\nEnter no. of holes: ");
    scanf("%d",&nh);
    accept(n,nh,holes,p);
    do
    {
        printf("\nMENU:\n\n1.First fit\n2.Best Fit\n3.Worst Fit\n4.Next fit\n5.Re-enter processes and holes\n6.Exit\n\n\tEnter your choice: ");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1: firstfit(n,nh,holes,p);
                break;
            case 2: bestfit(n,nh,holes,p);
                break;           
            case 3: worstfit(n,nh,holes,p);
                break;           
            case 4: nextfit(n,nh,holes,p);
                break;           
            case 5: printf("\nEnter no. of processes: ");
                scanf("%d",&n);
                printf("\nEnter no. of holes: ");
                scanf("%d",&nh);
                accept(n,nh,holes,p);           
            case 6: break;
            default:printf("\nWrong choice entered\n");
                break;
        }
    }while(ch!=6);
}
//Here, we accept the size of the processes and the size of the holes
void accept(int n,int nh,int holes[20],int p[20])
{
    int i;
    for(i=0;i<n;i++)
    {
        printf("\nEnter size of process %d: ",i+1);
        scanf("%d",&p[i]);
    }
    for(i=0;i<nh;i++)
    {
        printf("\nEnter size of hole %d: ",i+1);
        scanf("%d",&holes[i]);
    }
}
void firstfit(int n,int nh,int holes1[20],int p[20])
{
    int i,j,holes[20];
//We transfer the hole sizes to a new array so that they don't get changed in the main function.
    for(i=0;i<nh;i++)
        holes[i]=holes1[i];
    for(i=0;i<n;i++)
    {
        for(j=0;j<nh;j++)
        {
            if(p[i]<=holes[j])    //We find the first hole that fits, and place the process in it.
            {
                printf("\nProcess %d fits into hole %d",i+1,j+1);               
                holes[j]=holes[j]-p[i];
                break;
            }
        }
        if(j==nh)
            printf("\nProcess %d does not fit into any hole",i+1);   
    }
    printf("\n\nHoles at the end of fitting all processes: ");
    for(i=0;i<nh;i++)
        printf("\nHole %d: %d",i+1,holes[i]);
}
//This is a variation on first fit. In next fit, we resume the search from the last hole that was filled. Eg., if process 1 filled hole 2, then process 2 will begin searching for a hole that fits, starting from hole 2.
void nextfit(int n,int nh,int holes1[20],int p[20])
{
    int i,j=0,holes[20],x;
//Variable x is used to hold a value such that when j reaches x-1, it has traversed the whole array. Since the first process has to traverse the array from 0 to nh-1, we initialize x with nh
    x=nh;
//We transfer the hole sizes to a new array so that they don't get changed in the main function.   
    for(i=0;i<nh;i++)
        holes[i]=holes1[i];
    for(i=0;i<n;i++)
    {
        for(;j<nh;)
        {
            if(p[i]<=holes[j])
            {
                printf("\nProcess %d fits into hole %d",i+1,j+1);               
                holes[j]=holes[j]-p[i];
                x=j;
                break;           
            }
//If following condition is satisfied, whole array has been traversed.
            else if(j==x-1)
            {
                j=nh;
                break;
            }
            else j=(j+1)%nh;
        }
        if(j==nh)
            printf("\nProcess %d does not fit into any hole",i+1);   
    }
    printf("\n\nHoles at the end of fitting all processes: ");
    for(i=0;i<nh;i++)
        printf("\nHole %d: %d",i+1,holes[i]);
}


//In bestfit, we find the smallest hole that can hold the given process.
void bestfit(int n,int nh,int holes1[20],int p[20])
{
    int i,j,index[20],cnt,min,holes[20];
    for(i=0;i<nh;i++)
        holes[i]=holes1[i];
    for(i=0;i<n;i++)
    {
        cnt=0;
        for(j=0;j<nh;j++)
        {
            if(p[i]<=holes[j])
            {
                index[cnt++]=j;    //We store the index of all holes that the process can fit it.
            }
        }
        if(cnt==0)
        {
            printf("\nProcess %d does not fit into any holes",i+1);       
            continue;
        }
//Using the following, we find the smallest hole that can hold the process, and place the process in that hole
        min=index[0];
        for(j=1;j<cnt;j++)
        {
            if(holes[index[j]]<holes[min])
                min=index[j];
        }       
        printf("\nProcess %d fits into hole %d",i+1,min+1);               
        holes[min]=holes[min]-p[i];
    }
    printf("\n\nHoles at the end of fitting all processes: ");
    for(i=0;i<nh;i++)
        printf("\nHole %d: %d",i+1,holes[i]);
}





//In worstfit, we find the largest hole that can fit the process
void worstfit(int n,int nh,int holes1[20],int p[20])
{
    int i,j,index[20],max,holes[20];
    for(i=0;i<nh;i++)
        holes[i]=holes1[i];
    for(i=0;i<n;i++)
    {
//Using the following, we find the largest hole
        max=0;
        for(j=1;j<nh;j++)
        {
            if(holes[j]>holes[max])
                max=j;
        }
//Here, we check whether the largest hole is big enough to hold the process. If yes, we allocate it. If not, the process does not fit in any holes.
        if(holes[max]>p[i])
        {
            holes[max]=holes[max]-p[i];
            printf("\nProcess %d fits into hole %d",i+1,max+1);
        }
        else printf("\nProcess %d does not fit into any holes",i+1);
    }
    printf("\n\nHoles at the end of fitting all processes: ");
    for(i=0;i<nh;i++)
        printf("\nHole %d: %d",i+1,holes[i]);
}

No comments:

Post a Comment