Print Friendly and PDF
#include<conio.h>
#include<windows.h>
#include<iostream>
using namespace std;
int main()
{
   int c;
   system("Color FC");
   cout<<"\n\t\t     Choose your desired option"<<endl;
   cout<<"\t\t\t1 ->First Fit"<<endl;
   cout<<"\t\t\t2 ->Best Fit "<<endl;
   cout<<"\t\t\t3 ->Worst Fit"<<endl;
   cout<<"\t\t\t4 ->Next Fit "<<endl;
   cout<<"\t\t\tenter choice:\t";
   cin>>c;
   if(c==4)
  { 
    int p,m;  
    cout<<"Enter number of processes : ";
    cin>>p;
    cout<<"Enter number of Memory blocks : ";
    cin>>m;
    int parr[p],marr[m],i;
    for(i=0;i<p;i++)
    {
    cout<<"Enter size of process "<<(i+1)<<" : ";
    cin>>parr[i];      
    }
    for(i=0;i<m;i++)
    {
    cout<<"Enter size of memory "<<(i+1)<<" ";
    cin>>marr[i];      
     }
     int j=0;
     for(i=0;i<p;i++)
     {
     cout<<" search  "<< i<<" "<< j;
      for(;;j=(j+1)%m)
      {
      if(marr[j]>=parr[i])
      {
       marr[j]-=parr[i];
       cout<<"Allocating process to memory";
       cout<<"\n Size remaining in it after allocation ";
       cout<<(i+1)<<" "<<(j+1)<<" "<<marr[j];   
       cout<<j<<(j+1)%m;
        break;            
        }  
        }    
        if(j==m)
        {
        cout<<"Not enough memory for process "<<i;
        break;
        }        
        }
         getch();
       }        
       else if(c==1 || c==2 || c==3)
       {
       int i,j,k,n,l,m[10],p[10],po[20],flag,z,y,temp,temp1;
       cout<<"enter memory partition:\t";
       cin>>n;
       cout<<"\nenter memory size for\n";
       for(i=1;i<=n;i++)
       {
       cout<<"\npartition "<<i<<" :\t";
       cin>>m[i];
       po[i]=i;       
       }
          cout<<"\nenter process:\t";
          cin>>j;
          cout<<"\nenter memory size for\n";
          for(i=1;i<=j;i++)
          {
              cout<<"\nprocess "<<i<<" :\t";
              cin>>p[i];                 
          }        
          switch(c)
          {
             case 1:
             for(i=1;i<=j;i++)
    {
    flag=1;
    for(k=1;k<=n;k++)
    {
   if(p[i]<=m[k])
   {
   cout<<"\nprocess "<<i<<" whose memory size is "<<p[i];
   cout<<"KB allocated at memory partition : "<<po[k]<<endl;;
   if(p[i]<m[k])
   {
   cout<<"internal fragmentation is : "<<(m[k]-p[i]);
   }
   else
   {
   cout<<"external fragmentation is : "<<(p[i]-m[k]);
    }
    m[k]=m[k]-p[i];
    break;           
    }
    else
    {
     flag++;  
     }
     }     
    if(flag>n)
     {
     cout<<"\nprocess "<<i<<" whose memory size is "<<p[i];
     cout<<"KB can't be allocated";       
     }           
     }
     break;
     case 2:
     for(y=1;y<=n;y++)
     {
     for(z=y;z<=n;z++)
     {
     if(m[y]>m[z])
     {
      temp=m[y];
      m[y]=m[z];
      m[z]=temp;
      temp1=po[y]; 
       po[y]=po[z];
       po[z]=temp1;            
       }                  
       }              
       }
       for(i=1;i<=j;i++)
       {
       flag=1;
       for(k=1;k<=n;k++)
       {
      if(p[i]<=m[k])
      {
      cout<<"\nprocess "<<i<<" whose memory size is "<<p[i];
     cout<<"KB allocated at memory partition : "<<po[k]<<endl;
     if(p[i]<m[k])
        {
      cout<<"internal fragmentation is : "<<(m[k]-p[i]);
        }
       else
         {
   cout<<"external fragmentation is : "<<(p[i]-m[k]);
    }
    m[k]=m[k]-p[i];
   break;           
   }
   else
  {
  flag++;  
  }
  }   
  if(flag>n)
  {
  cout<<"\nprocess "<<i<<" whose memory size is "<<p[i];
  cout<<"KB can't be allocated";       
  }           
  }
  break;
  case 3:
  for(y=1;y<=n;y++)
  {
  for(z=y;z<=n;z++)
  {
  if(m[y]<m[z])
  {
  temp=m[y];
  m[y]=m[z];
  m[z]=temp;
  temp1=po[y]; 
  po[y]=po[z];
  po[z]=temp1;            
  }                 
  }              
  }
  for(i=1;i<=j;i++)
 {
 flag=1;
  for(k=1;k<=n;k++)
  {
  if(p[i]<=m[k])
 {
 cout<<"\nprocess "<<i<<" whose memory size is "<<p[i];
 cout<<"KB allocated at memory partition : "<<po[k]<<endl;
 if(p[i]<m[k])
 {
 cout<<"internal fragmentation is : "<<(m[k]-p[i]);
 }
 else
 {
 cout<<"external fragmentation is : "<<(p[i]-m[k]);
 }
 m[k]=m[k]-p[i];
 break;           
 else
{
flag++;  
}
}   
if(flag>n)
{
cout<<"\nprocess "<<i<<" whose memory size is "<<p[i];
cout<<"KB can't be allocated";       
}          
}
break;
}  
getch();
}
}
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: