#include<stdio.h>
int main()
{
int arr[10]={5,6,7,4,9,8,2,3,1,4};//input memory block size;change as per your requirement
int i,j,n,bs,cnt=0,m=0,temp;
printf("\nEnter:\n1:First Fit\n2:Best Fit\n3:Worst Fit\n4:Exit\nYour Choice:");
scanf("%d",&n);
printf("\n*************************\n");
for(i=0;i<10;i++)
printf("|%d| ",arr[i]);
printf("\n*************************\n");
while(n!=4)
{
if(n==1)
{
printf("\nEnter the block size:");
scanf("%d",&bs);
cnt=0;
while(++cnt!=10)
{
if(arr[cnt-1]!=999 && arr[cnt-1]>=bs)
{
break;
}
}
if(cnt<10){
if(arr[cnt-1]>bs){
printf("\nSize of block allocated=%d",bs);
arr[cnt-1]-=bs;}
else{
printf("\nSize of block allocated=%d",arr[cnt-1]);
arr[cnt-1]=999;}
}
else
printf("\nNo memory blocks available!!\n");
printf("\n*************************\n");
for(i=0;i<10;i++)
printf("|%d| ",arr[i]);
printf("\n*************************\n");
}
else if(n==2)
{
printf("\nEnter the block size:");
scanf("%d",&bs);
cnt=0;
m=0;
while(++cnt!=11)
{
if(arr[cnt-1]!=999 && arr[cnt-1]>=bs)
{
if(m++==0)
temp=cnt-1;
else if(arr[temp]>arr[cnt-1])
temp=cnt-1;
}
}
if(m!=0){
if(arr[temp]>bs){
printf("\nSize of block allocated=%d",bs);
arr[temp]-=bs;}
else{
printf("\nSize of block allocated=%d",arr[temp]);
arr[temp]=999;}
}
else
printf("\nNo free block available!\n");
printf("\n*************************\n");
for(i=0;i<10;i++)
printf("|%d| ",arr[i]);
printf("\n*************************\n");
}
else if(n==3)
{
printf("\nEnter the block size:");
scanf("%d",&bs);
cnt=0;
m=0;
while(++cnt!=11)
{
if(arr[cnt-1]!=999 && arr[cnt-1]>=bs)
{
if(m++==0)
temp=cnt-1;
else if(arr[temp]<arr[cnt-1])
temp=cnt-1;
}
}
if(m!=0){
if(arr[temp]>bs){
printf("\nSize of block allocated=%d",bs);
arr[temp]-=bs;}
else{
printf("\nSize of block allocated=%d",arr[temp]);
arr[temp]=999;}
}
else
printf("\nNo free block available!\n");
printf("\n*************************\n");
for(i=0;i<10;i++)
printf("|%d| ",arr[i]);
printf("\n*************************\n");
}
printf("\nEnter:\n1:First Fit\n2:Best Fit\n3:Worst Fit\n4:Exit\nYour Choice:");
scanf("%d",&n);
}
return 0;
}
int main()
{
int arr[10]={5,6,7,4,9,8,2,3,1,4};//input memory block size;change as per your requirement
int i,j,n,bs,cnt=0,m=0,temp;
printf("\nEnter:\n1:First Fit\n2:Best Fit\n3:Worst Fit\n4:Exit\nYour Choice:");
scanf("%d",&n);
printf("\n*************************\n");
for(i=0;i<10;i++)
printf("|%d| ",arr[i]);
printf("\n*************************\n");
while(n!=4)
{
if(n==1)
{
printf("\nEnter the block size:");
scanf("%d",&bs);
cnt=0;
while(++cnt!=10)
{
if(arr[cnt-1]!=999 && arr[cnt-1]>=bs)
{
break;
}
}
if(cnt<10){
if(arr[cnt-1]>bs){
printf("\nSize of block allocated=%d",bs);
arr[cnt-1]-=bs;}
else{
printf("\nSize of block allocated=%d",arr[cnt-1]);
arr[cnt-1]=999;}
}
else
printf("\nNo memory blocks available!!\n");
printf("\n*************************\n");
for(i=0;i<10;i++)
printf("|%d| ",arr[i]);
printf("\n*************************\n");
}
else if(n==2)
{
printf("\nEnter the block size:");
scanf("%d",&bs);
cnt=0;
m=0;
while(++cnt!=11)
{
if(arr[cnt-1]!=999 && arr[cnt-1]>=bs)
{
if(m++==0)
temp=cnt-1;
else if(arr[temp]>arr[cnt-1])
temp=cnt-1;
}
}
if(m!=0){
if(arr[temp]>bs){
printf("\nSize of block allocated=%d",bs);
arr[temp]-=bs;}
else{
printf("\nSize of block allocated=%d",arr[temp]);
arr[temp]=999;}
}
else
printf("\nNo free block available!\n");
printf("\n*************************\n");
for(i=0;i<10;i++)
printf("|%d| ",arr[i]);
printf("\n*************************\n");
}
else if(n==3)
{
printf("\nEnter the block size:");
scanf("%d",&bs);
cnt=0;
m=0;
while(++cnt!=11)
{
if(arr[cnt-1]!=999 && arr[cnt-1]>=bs)
{
if(m++==0)
temp=cnt-1;
else if(arr[temp]<arr[cnt-1])
temp=cnt-1;
}
}
if(m!=0){
if(arr[temp]>bs){
printf("\nSize of block allocated=%d",bs);
arr[temp]-=bs;}
else{
printf("\nSize of block allocated=%d",arr[temp]);
arr[temp]=999;}
}
else
printf("\nNo free block available!\n");
printf("\n*************************\n");
for(i=0;i<10;i++)
printf("|%d| ",arr[i]);
printf("\n*************************\n");
}
printf("\nEnter:\n1:First Fit\n2:Best Fit\n3:Worst Fit\n4:Exit\nYour Choice:");
scanf("%d",&n);
}
return 0;
}
No comments:
Post a Comment