First Fit Memory Allocation

#include<stdio.h>

int fit[10];

void main()
{
    int a[10],b[10],i,n,m,j,sum=0,total=0;
    float util;
    printf("Enter number of memory portions ");
    scanf("%d",&n);
    printf("Enter available memory portions\n");
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    printf("Enter number of processes ");
    scanf("%d",&m);
    printf("Enter processes\n");
    for(i=0;i<m;i++)
        scanf("%d",&b[i]);
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            if(b[i]<=a[j]&&fit[j]==0)
            {
                fit[j]=b[i];
                break;
            }
        }
    }
    printf("\nFirst fit\n");
    for(i=0;i<n;i++)
    {
        printf("%d\t",fit[i]);
        sum=sum+fit[i];
    }
    printf("\n");
    for(i=0;i<n;i++)
    {
        printf("%d\t",a[i]);
        total=total+a[i];
    }
    printf("\n\nSum %d",sum);
    printf("\nTotal %d",total);
    util=((float)sum/total)*100;
    printf("\nPercentage Utilization is %f",util);
}

/*Output
Enter number of memory portions 6
Enter available memory portions
50 100 500 200 300 600
Enter number of processes 5
Enter processes
201 49 415 127 423

First fit
49 0 201 127 0 415
50 100 500 200 300 600

Sum 792
Total 1750
Percentage Utilization is 45.257141
*/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s