Вы находитесь на странице: 1из 5

Digital assignment

Faculty:Dr srimathi C
Slot:
Name:Ashutosh Reddy
Reg No: 18BCE0330
Date:27/09/2019

First Fit:

Code:
#include<iostream>
using namespace std;
int main()
{
int n,m,i,ifr=0,ef=0;
cout<<"Enter the no of processes : ";
cin>>n;
int size[n],array[n],j,k=0,f=0;
cout<<"Enter the size of each process : ";
for(i=0;i<n;i++)
cin>>size[i];
cout<<"Enter the size of memory array : ";
cin>>m;
int memo[m];
cout<<"Enter the memory sizes : ";
for(i=0;i<m;i++)
cin>>memo[i];
for(i=0;i<n;i++)
{
int count=0;
for(j=0;j<m;j++)
{
if(memo[j]>=size[i])
{
count++;
memo[j]-=size[i];
ifr+=memo[j];
array[f++]=j+1;
break;
}
}
if(count==0)
ef+=size[i];
}
cout<<"Internal Fragmentation : "<<ifr<<endl;
cout<<"External Fragmentation : "<<ef<<endl;
cout<<"Memory Blocks Accessed in Order : "<<endl;
for(i=0;i<f;i++)
{
if(i==f-1)
cout<<array[i];
else
cout<<array[i]<<"->";
}
return 0;
}

Output:

Best Fit:

Code:

#include<iostream>
using namespace std;
int main()
{
int n,m,i,ifr=0,ef=0;
cout<<"Enter the no of processes : ";
cin>>n;
int size[n],array[n],j,k=0,f=0;
cout<<"Enter the size of each process : ";
for(i=0;i<n;i++)
cin>>size[i];
cout<<"Enter the size of memory array : ";
cin>>m;
int memo[m];
cout<<"Enter the memory sizes : ";
for(i=0;i<m;i++)
cin>>memo[i];
int place;
for(i=0;i<n;i++)
{
int count=0;
for(j=0;j<m;j++)
{
if(memo[j]>=size[i])
{
count++;
place=j;
for(k=j+1;k<m;k++)
{
if(memo[k]>=size[i] && memo[k]<memo[place])
place=k;
}
break;
}

}
if(count==0)
ef+=size[i];
else
{
memo[place]-=size[i];
ifr+=memo[place];
array[f++]=place+1;

}
}
cout<<"Internal Fragmentation : "<<ifr<<endl;
cout<<"External Fragmentation : "<<ef<<endl;
cout<<"Memory Blocks Accessed in Order : "<<endl;
for(i=0;i<f;i++)
{
if(i==f-1)
cout<<array[i];
else
cout<<array[i]<<"->";
}
return 0;
}

Output:

Worst Fit:

Code:

#include<iostream>
using namespace std;
int main()
{
int n,m,i,ifr=0,ef=0;
cout<<"Enter the no of processes : ";
cin>>n;
int size[n],array[n],j,k=0,f=0;
cout<<"Enter the size of each process : ";
for(i=0;i<n;i++)
cin>>size[i];
cout<<"Enter the size of memory array : ";
cin>>m;
int memo[m];
cout<<"Enter the memory sizes : ";
for(i=0;i<m;i++)
cin>>memo[i];
int place;
for(i=0;i<n;i++)
{
int count=0;
for(j=0;j<m;j++)
{
if(memo[j]>=size[i])
{
count++;
place=j;
for(k=j+1;k<m;k++)
{
if(memo[k]>=size[i] && memo[k]>memo[place])
place=k;
}
break;
}

}
if(count==0)
ef+=size[i];
else
{
memo[place]-=size[i];
ifr+=memo[place];
array[f++]=place+1;
}
}
cout<<"Internal Fragmentation : "<<ifr<<endl;
cout<<"External Fragmentation : "<<ef<<endl;
cout<<"Memory Blocks Accessed in Order : "<<endl;
for(i=0;i<f;i++)
{
if(i==f-1)
cout<<array[i];
else
cout<<array[i]<<"->";
}
return 0;
}

Output:

Вам также может понравиться