Header Ads Widget

Responsive Advertisement

Latest

6/recent/ticker-posts

Program for First Fit , Worst Fit , Best Fit Memory Allocation Algorithm

Program for First Fit  , Worst Fit , Best Fit Memory Allocation Algorithm

  

C++  Program for Best fit Algorithm

 
#include<iostream>

using namespace std;

int main()
{
    int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
    static int barray[20],parray[20];
    cout<<"\n\t\t\tMemory Management Scheme - Best Fit";
    cout<<"\nEnter the number of blocks:";
    cin>>nb;
    cout<<"Enter the number of processes:";
    cin>>np;
    
    cout<<"\nEnter the size of the blocks:-\n";
    for(i=1;i<=nb;i++)
    {
        cout<<"Block no."<<i<<":";
        cin>>b[i];
    }
    
    cout<<"\nEnter the size of the processes :-\n";
    for(i=1;i<=np;i++)
    {
        cout<<"Process no. "<<i<<":";
        cin>>p[i];
    }
    
    for(i=1;i<=np;i++)
    {
        for(j=1;j<=nb;j++)
        {
            if(barray[j]!=1)
            {
                temp=b[j]-p[i];
                if(temp>=0)
                    if(lowest>temp)
                    {
                        parray[i]=j;
                        lowest=temp;
                    }
            }
        }
        
        fragment[i]=lowest;
        barray[parray[i]]=1;
        lowest=10000;
    }
    
    cout<<"\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment";
    for(i=1;i<=np && parray[i]!=0;i++)
        cout<<"\n"<<i<<"\t\t"<<p[i]<<"\t\t"<<parray[i]<<"\t\t"<<b[parray[i]]<<"\t\t" <<fragment[i];
    
    return 0;
}
 
OUTPUT :

Memory management scheme - Best fit output
Out put for Best fit Memory Allocation Algorithm


C++  Program for First fit algorithm


#include<iostream>

using namespace std;

int main()
{
    int bsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j;

    for(i = 0; i < 10; i++)
    {
        flags[i] = 0;
        allocation[i] = -1;
    }
    
    cout<<"Enter no. of blocks: ";
    cin>>bno;
    
    cout<<"\nEnter size of each block: ";
    for(i = 0; i < bno; i++)
        cin>>bsize[i];

    cout<<"\nEnter no. of processes: ";
    cin>>pno;
    
    cout<<"\nEnter size of each process: ";
    for(i = 0; i < pno; i++)
        cin>>psize[i];
    for(i = 0; i < pno; i++)         //allocation as per first fit
        for(j = 0; j < bno; j++)
            if(flags[j] == 0 && bsize[j] >= psize[i])
            {
                allocation[j] = i;
                flags[j] = 1;
                break;
            }
    
    //display allocation details
    cout<<"\nBlock no.\tsize\t\tprocess no.\t\tsize";
    for(i = 0; i < bno; i++)
    {
        cout<<"\n"<< i+1<<"\t\t"<<bsize[i]<<"\t\t";
        if(flags[i] == 1)
            cout<<allocation[i]+1<<"\t\t\t"<<psize[allocation[i]];
        else
            cout<<"Not allocated";
    }
    
    return 0;

OUTPUT :

Memory management scheme - First fit output
Out put for First fit Memory Allocation Algorithm


C++  Program for Worst fit Algorithm


#include <iostream>
 
using namespace std;
 
int main()
{
    int nBlocks,nProcess,blockSize[20],processSize[20];
     
    cout<<" Enter the number of blocks: "; cin>>nBlocks;
    cout<<" Enter the number of processes: "; cin>>nProcess;
     
    cout<<" Enter the size of "<<nBlocks<<" blocks: ";
    for(int i=0;i<nBlocks;i++) cin>>blockSize[i];
       
    cout<<" Enter the size of "<<nProcess<<" processes: ";
    for(int i=0;i<nProcess;i++) cin>>processSize[i];
     

      
      for(int i=0;i<nProcess;i++)
       {
           int max = blockSize[0];
           int pos = 0;
           
           for(int j=0;j<nBlocks;j++)
            if(max < blockSize[j]) { max = blockSize[j]; pos = j; } if(max >= processSize[i])
            {
                cout<<"\nProcess "<<i+1<<" is allocated to block "<<pos+1;
                blockSize[pos] = blockSize[pos]-processSize[i];
            }
            else{
            cout<<"\nProcess "<<i+1<<" can't be allocated";
                
            }
       }

     return 0;
}
 
OUTPUT :

Memory management scheme - Worst fit output
Out put for Worst fit Memory Allocation Algorithm

Post a Comment

5 Comments

  1. Emmm i want to ask about my education that i am recently clear my inter now what course should i do next i also get ideas from the https://qanda.typicalstudent.org but need your opinions here !!
    Thanks !!

    ReplyDelete
  2. You did a great job. I am really enjoyed your post. Thanks for this one. Data Science Training in Pune

    ReplyDelete
  3. You are awesome bro,saved my life with this

    ReplyDelete