Implementation of memory allocation policy

/*
* Title: Implementation of memory allocation policy
* Name: Akshay Thakare
* Roll No: 6790
*/

#include

void print(int a[][2],int p[],int n,int m)
{
	int i,j,f=0;
	for(i=1;i<=m;i++)
	{
		f=0;
		for(j=1;j<=n;j++)
			if(a[j][1]==i)
			{
				printf("Program P%d stored in %dk\n",i,a[j][0]);
				f=1;
			}
		if(!f)
			printf("Program P%d not stored\n",i);
	}
}

void firstfit(int a[][2],int p[],int n,int m)
{
	int i,j;
	for(i=1;i<=m;i++)
	{
		for(j=1;j<=n;j++)
		{
			if(p[i]<a[j][0]&&!a[j][1])
			{
				a[j][1]=i;
				break;
			}
		}
	}
	printf("****FIRST FIT****\n");
	print(a,p,n,m);
}

void bestfit(int a[][2],int p[],int n,int m)
{
	int i,j,min;
	for(i=1;i<=m;i++)
	{
		min=19;
		for(j=1;jp[i]&&a[j][0]<a[min][0]&&!a[j][1])
				min=j;
		a[min][1]=i;
	}
	printf("\n****BEST FIT****\n");
	print(a,p,n,m);
}

void worstfit(int a[][2],int p[],int n,int m)
{
	int i,j,max,f;
	for(i=1;i<=m;i++)
	{
		f=1;
		max=1;
		for(j=1;jp[i]&&a[j][0]>=a[max][0]&&!a[j][1])
			{	
				max=j;
				f=0;
			}
		if(!f)	
			a[max][1]=i;
	}
	printf("\n****WORST FIT****\n");
	print(a,p,n,m);
}

int main()
{
	int i,j,n,m,a[20][2]={0},p[20]={0};
	a[19][0]=10000;
	printf("Enter no. of memeory partition : ");
	scanf("%d",&n);
	printf("Enter no. of program to be load : ");
	scanf("%d",&m);
	printf("Enter size of each partition\n");
	for(i=1;i",i);
		scanf("%d",&a[i][0]);
	}
	printf("Enter size of each program\n");
	for(i=1;i",i);
		scanf("%d",&p[i]);
	}
	firstfit(a,p,n,m);
	for(i=1;i<=n;i++)
		a[i][1]=0;
	bestfit(a,p,n,m);
	for(i=1;i100
2-->500 
3-->200
4-->300
5-->600
Enter size of each program
P1-->212
P2-->417
P3-->112
P4-->426
****FIRST FIT****
Program P1 stored in 500k
Program P2 stored in 600k
Program P3 stored in 200k
Program P4 not stored

****BEST FIT****
Program P1 stored in 300k
Program P2 stored in 500k
Program P3 stored in 200k
Program P4 stored in 600k

****WORST FIT****
Program P1 stored in 600k
Program P2 stored in 500k
Program P3 stored in 300k
Program P4 not stored

*/
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