#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int p[10],np,b[10],nb,ch,c[10],d[10],alloc[10],flag[10],i,j;
cout<<"\nEnter the no of process:";
cin>>np;
cout<<"\nEnter the no of blocks:";
cin>>nb;
cout<<"\nEnter the size of each process:";
for(i=0;i<np;i++)
{
cout<<"\nProcess "<<i;
cin>>p[i];
}
cout<<"\nEnter the block sizes:";
for(j=0;j<nb;j++)
{
cout<<"\nBlock "<<j;
cin>>b[j];
c[j]=b[j];
d[j]=b[j];
}
if(np<=nb)
{
cout<<"\n1.First fit";
cout<<"\n2.Best fit";
cout<<"\n3.Worst fit";
do
{
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nFirst Fit\n";
for(i=0;i<np;i++)
{
for(j=0;j<nb;j++)
{
if(p[i]<=b[j])
{
alloc[j]=p[i];
cout<<"\n\nAlloc["<<alloc[j]<<"]";
cout<<"\n\nProcess "<<i<<" of size "<<p[i]<<" is allocated in block: "<<j<<" of size:"<<b[j];
flag[i]=0,b[j]=0;
break;
}
else flag[i]=1;
}
}
for(i=0;i<np;i++)
{
if(flag[i]!=0)
cout<<"\n\nProcess "<<i<<" of size "<<p[i]<<" is not allocated";
}
break;
case 2: cout<<"\nBest Fit\n";
for(i=0;i<nb;i++)
{
for(j=i+1;j<nb;j++)
{
if(c[i]>c[j])
{
int temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
cout<<"\nAfter sorting block sizes:";
for(i=0;i<nb;i++)
cout<<"\nBlock "<<i<<":"<<c[i];
for(i=0;i<np;i++)
{
for(j=0;j<nb;j++)
{
if(p[i]<=c[j])
{
alloc[j]=p[i];
cout<<"\n\nAlloc["<<alloc[j]<<"]";
cout<<"\n\nProcess "<<i<<" of size "<<p[i]<<" is allocated in block: "<<j<<" of size:"<<c[j];
flag[i]=0,c[j]=0;
break;
}
else flag[i]=1;
}
}
for(i=0;i<np;i++)
{
if(flag[i]!=0)
cout<<"\n\nProcess "<<i<<" of size "<<p[i]<<" is not allocated";
}
break;
case 3: cout<<"\nWorst Fit\n";
for(i=0;i<nb;i++)
{
for(j=i+1;j<nb;j++)
{
if(d[i]<d[j])
{
int temp=d[i];
d[i]=d[j];
d[j]=temp;
}
}
}
cout<<"\nAfter sorting block sizes:";
for(i=0;i<nb;i++)
cout<<"\nBlock "<<i<<":"<<d[i];
for(i=0;i<np;i++)
{
for(j=0;j<nb;j++)
{
if(p[i]<=d[j])
{
alloc[j]=p[i];
cout<<"\n\nAlloc["<<alloc[j]<<"]";
cout<<"\n\nProcess "<<i<<" of size "<<p[i]<<" is allocated in block: "<<j<<" of size:"<<d[j];
flag[i]=0,d[j]=0;
break;
}
else flag[i]=1;
}
}
for(i=0;i<np;i++)
{
if(flag[i]!=0)
cout<<"\n\nProcess "<<i<<" of size "<<p[i]<<" is not allocated";
}
break;
default:cout<<"Invalid Choice!";
break;
}
}
while(ch<=3);
getch();
}
}
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int p[10],np,b[10],nb,ch,c[10],d[10],alloc[10],flag[10],i,j;
cout<<"\nEnter the no of process:";
cin>>np;
cout<<"\nEnter the no of blocks:";
cin>>nb;
cout<<"\nEnter the size of each process:";
for(i=0;i<np;i++)
{
cout<<"\nProcess "<<i;
cin>>p[i];
}
cout<<"\nEnter the block sizes:";
for(j=0;j<nb;j++)
{
cout<<"\nBlock "<<j;
cin>>b[j];
c[j]=b[j];
d[j]=b[j];
}
if(np<=nb)
{
cout<<"\n1.First fit";
cout<<"\n2.Best fit";
cout<<"\n3.Worst fit";
do
{
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nFirst Fit\n";
for(i=0;i<np;i++)
{
for(j=0;j<nb;j++)
{
if(p[i]<=b[j])
{
alloc[j]=p[i];
cout<<"\n\nAlloc["<<alloc[j]<<"]";
cout<<"\n\nProcess "<<i<<" of size "<<p[i]<<" is allocated in block: "<<j<<" of size:"<<b[j];
flag[i]=0,b[j]=0;
break;
}
else flag[i]=1;
}
}
for(i=0;i<np;i++)
{
if(flag[i]!=0)
cout<<"\n\nProcess "<<i<<" of size "<<p[i]<<" is not allocated";
}
break;
case 2: cout<<"\nBest Fit\n";
for(i=0;i<nb;i++)
{
for(j=i+1;j<nb;j++)
{
if(c[i]>c[j])
{
int temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
cout<<"\nAfter sorting block sizes:";
for(i=0;i<nb;i++)
cout<<"\nBlock "<<i<<":"<<c[i];
for(i=0;i<np;i++)
{
for(j=0;j<nb;j++)
{
if(p[i]<=c[j])
{
alloc[j]=p[i];
cout<<"\n\nAlloc["<<alloc[j]<<"]";
cout<<"\n\nProcess "<<i<<" of size "<<p[i]<<" is allocated in block: "<<j<<" of size:"<<c[j];
flag[i]=0,c[j]=0;
break;
}
else flag[i]=1;
}
}
for(i=0;i<np;i++)
{
if(flag[i]!=0)
cout<<"\n\nProcess "<<i<<" of size "<<p[i]<<" is not allocated";
}
break;
case 3: cout<<"\nWorst Fit\n";
for(i=0;i<nb;i++)
{
for(j=i+1;j<nb;j++)
{
if(d[i]<d[j])
{
int temp=d[i];
d[i]=d[j];
d[j]=temp;
}
}
}
cout<<"\nAfter sorting block sizes:";
for(i=0;i<nb;i++)
cout<<"\nBlock "<<i<<":"<<d[i];
for(i=0;i<np;i++)
{
for(j=0;j<nb;j++)
{
if(p[i]<=d[j])
{
alloc[j]=p[i];
cout<<"\n\nAlloc["<<alloc[j]<<"]";
cout<<"\n\nProcess "<<i<<" of size "<<p[i]<<" is allocated in block: "<<j<<" of size:"<<d[j];
flag[i]=0,d[j]=0;
break;
}
else flag[i]=1;
}
}
for(i=0;i<np;i++)
{
if(flag[i]!=0)
cout<<"\n\nProcess "<<i<<" of size "<<p[i]<<" is not allocated";
}
break;
default:cout<<"Invalid Choice!";
break;
}
}
while(ch<=3);
getch();
}
}
Comments
Post a Comment