Professional Documents
Culture Documents
MEMORY MANAGEMENT
Shreya Sivakumar
20BCE1794
The program's goal is to build the First-fit, Best-fit, and Worst-fit algorithms,
analyse internal and external fragmentations, and determine which approach is
the most efficient in terms of memory use.
#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];
int total=0;
cout<<"\nBlock no.\tsize\t\tprocess no.\t\tInternal Fragmentation";
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<<"-";
total+=bsize[i];
}
cout<<"\nExternal Fragmentation: "<<total;
return 0;
}
OUTPUT:
b) BEST FIT CODE:
#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];
cin>>b[i];
}
cout<<"\nEnter the size of the processes :\n";
for(i=1;i<=np;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;
}
int total=0;
total+=p[i];
cout<<"\nProcess_no\tsize\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];
cout<<"\nExternal Fragmentation: "<<total<<"\n";
return 0;
}
OUTPUT:
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
int memsize;
int allocp=-1;
int pos;
int allocSize;
}m[200];
bool posSort(node a,node b){
return a.pos < b.pos;
}
bool memSort(node a,node b){
return a.memsize > b.memsize;
}
int main(){
int nm,np,choice, i, j, p[200];
cout<<"Enter number of blocks: ";
cin>>nm;
cout<<"Enter block size: \n";
for(i=0;i<nm;i++){
cin>>m[i].memsize;
m[i].pos=i;
}
cout<<"Enter number of processes: ";
cin>>np;
cout<<"Enter process size:\n";
for(i=0;i<np;i++){
cin>>p[i];
}
cout<<"\n\n";
sort(m,m+nm,memSort);
int globalFlag=0;
for(i=0;i<np;i++){
int flag=0;
for(j=0;j<nm;j++){
if(p[i]<=m[j].memsize && m[j].allocp==-1){
m[j].allocp=i;
m[j].allocSize=p[i];
flag=1;
break;
}
}
sort(m,m+nm,posSort);
cout<<"\n";
int intFrag=0,extFrag=0;
cout<<"Memory Blocks\t\t";
for(i=0;i<nm;i++){
cout<<m[i].memsize<<"\t";
}
cout<<"\n";
cout<<"Process Allocation\t";
for(i=0;i<nm;i++){
if(m[i].allocp!=-1){
cout<<"P"<<m[i].allocp+1<<"\t";
}
else{
cout<<"-\t";
}
}
cout<<"\n";
cout<<"Internal Frag.\t";
for(i=0;i<nm;i++){
if(m[i].allocp!=-1){
cout<<m[i].memsize-m[i].allocSize<<"\t";
intFrag+=m[i].memsize-m[i].allocSize;
}
else{
extFrag+=m[i].memsize;
cout<<"-\t";
}
}
cout<<"\n";
if(globalFlag==1)
cout<<"\n";
else{
cout<<"Available Memory: "<<extFrag<<"\n";
}
cout<<"Total External Fragmentation: "<<extFrag<<"\n";
return 0;
}
}
OUTPUT:
Between the first, best-fit, and worst-fit, first-fit is the easiest and takes the least
amount of time to implement.
First-fit, on the other hand, is unable to allocate memory to all processes. On
the other side, best-fit and worst-fit select the best and worst partitions to match
the process, respectively. Best-fit is favoured, and all processes are allocated in
memory, because worst-fit leaves a huge amount of wasted space.
#include <bits/stdc++.h>
using namespace std;
void fifo_page_replacement(void)
{
queue<int> Q;
int page_faults=0;
}
cout<<"\nTotal Page Faults: "<<page_faults;
return;
}
int main()
{
cout<<"Number of Frames: ";
cin>>frame_size;
fifo_page_replacement();
return 0;
}
OUTPUT:
LRU REPLACEMENT:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m,i,j,k,hit=0;
cout<<"Enter number of frames: ";
cin>>n;
cout<<"Enter number of processes: ";
cin>>m;
vector<int> p(m);
vector<int> hi(m);
cout<<"Enter processes:\n ";
for(i=0;i<m;i++){
cin>>p[i];
}
vector<vector<int>> a(n);
for(i=0;i<n;i++){
a[i]=vector<int>(m,-1);
}
map <int, int> mp;
for(i=0;i<m;i++){
vector<pair<int,int>> c;
for(auto q: mp){
c.push_back({q.second,q.first});
}
sort(c.begin(),c.end());
bool hasrun=false;
for(j=0;j<n;j++){
if(a[j][i]==p[i]){
hit++;
hi[i]=1;
mp[p[i]]=1;
hasrun=true;
break;
}
if(a[j][i]==-1){
for(k=i;k<m;k++)
a[j][k]=p[i];
mp[p[i]]++;
hasrun=true;
break;
}
}
if(j==n||hasrun==false){
for(j=0;j<n;j++){
if(a[j][i]==c[c.size()-1].second){
mp.erase(a[j][i]);
for(k=i;k<m;k++)
a[j][k]=p[i];
mp[p[i]]++;
break;
}
}
}
for(auto q:mp){
if(q.first!=p[i]){
mp[q.first]++;
}
}
}
cout<<"\n";
cout<<"Page Fault "<<m-hit<<'\n';
return 0;
}
OUTPUT: