You are on page 1of 11

CSE2005 OS LAB L33+34

MEMORY MANAGEMENT

Shreya Sivakumar
20BCE1794

1. Write a C program to implement the First-fit, Best-fit, and Worst-fit


algorithm for memory allocation. For example, given memory
partitions of 100K, 500K, 200K, 300K, and 600K (in order), how
would each of the First-fit, Best-fit, and Worst-fit algorithm place
processes of 212K, 417K, 112K, and 426K (in order)? Which
algorithm makes the most efficient use of memory? Print the internal
and external fragmentation as well.

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.

a) FIRST FIT CODE:

#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;
}

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];

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++)
{

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:

c) WORST FIT CODE:

#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.

2. Write a C program to implement FIFO and LRU page replacement


algorithms and print the number of page faults for n number of
reference pages and m number of free frames.
We will implement the FIFO and LRU page replacement algorithms, as
well as report the number of page faults found in each.

a) FIFO REPLACEMENT CODE:

#include <bits/stdc++.h>
using namespace std;

const int N=100005;


int n;
int frame_size;
int pages[N];
int mark[N];

void fifo_page_replacement(void)
{
queue<int> Q;
int page_faults=0;

for(int i=0; i<n; i++)


{
if(mark[pages[i]]==true)
{
cout<<"";
}
else
{
Q.push(pages[i]);
mark[pages[i]]=true;
if(Q.size()>frame_size)
{
int p= Q.front();
mark[p]=false;
Q.pop();
}
page_faults++;
}

}
cout<<"\nTotal Page Faults: "<<page_faults;
return;
}

int main()
{
cout<<"Number of Frames: ";
cin>>frame_size;

cout<<"Page Reference Stream Length: ";


cin>>n;

cout<<"Page Reference Stream:\n";


for(int i=0; i<n; i++)
cin>>pages[i];

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:

You might also like