OS Lab
MEMORY MANAGEMENT
OBJECTIVE: Write C/C++ programs to simulate the Fixed partitioning and Variable partitioning
memory management techniques.
DESCRIPTION:
(a) Fixed partitioning is one of the old memory management techniques in which the main memory is
divided into fixed-size partitions and each job is assigned a partition of equal or greater size. There
are two alternatives for fixed partitioning: (i) unequal size partitions, and (ii) equal size partitions.
This technique may suffer with the problem of internal fragmentation – a wasted space when a job
is placed in a partition of size greater than that of job.
(b) Dynamic partitioning is the memory management technique in which each job gets just the amount
of memory it needs. That is, the partitioning of memory is dynamic and changes as jobs enter and
leave the system. This technique makes efficient use of memory but may suffer with external
fragmentation – gaps occurred between the occupied portions of memory.
Fixed partitioning (with unequal partitions) Fixed partitioning (with equal-size partitions)
Dynamic partitioning
OS Lab
FIXED PARTITIONING WITH EQUAL-SIZE PARTITIONS/BLOCKS
C/C++ PROGRAM FOR FIXED PARTITIONING:
// Program for Fixed Partitioning (with equal-size partitions or blocks)
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int ms, bs, nob, ef, n, mp[10], tif=0;
int i, p=0;
cout << "Enter the total memory available (in Bytes): ";
cin >> ms; // ms = memory size
cout << "Enter the block size (in Bytes): ";
cin >> bs; // bs = block (or partition) size
nob = ms/bs; // nob = number of blocks (partitions)
ef = ms – nob*bs; // ef = external fragmentation
cout << "\nHow many processes are there? ";
cin >> n; // n = number of processes
for(i=0;i<n;i++)
{
cout << "Enter memory required for process " <<i+1<< " (in Bytes): ";
cin >> mp[i]; // mp = memory for process
}
cout << "\nNo. of Blocks (partitions) available in memory = " << nob;
cout << "\n\nPROCESS\tMEMORY_REQUIRED\t ALLOCATED\tINTERNAL_FRAGMENTATION";
for(i=0;i<n && p<nob;i++)
{
cout << "\n " << i+1 << "\t\t" << mp[i];
if(mp[i] > bs)
cout << "\t\tNO\t\t---";
else
{
cout << "\t\tYES\t" << bs-mp[i];
tif = tif + bs-mp[i]; // tif = total internal fragmentation
p++;
}
}
if(i<n)
cout << "\nMemory Full, Remaining Processes cannot be accommodated";
cout << "\n\nTotal Internal Fragmentation is " << tif;
cout << "\nTotal External Fragmentation is " << ef;
return 0;
}
OS Lab
DYNAMIC PARTITIONING
C/C++ PROGRAM FOR DYNAMIC PARTITIONING:
// Program for Dynamic Partitioning
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int ms, mp[10], i, temp, n=0;
char ch = 'y';
cout << "\nEnter the total memory available (in Bytes): ";
cin >> ms; // (total) memory size
temp=ms;
for(i=0;ch=='y';i++,n++)
{
cout << "\nEnter memory required for process "<<i+1<<" (in Bytes): ";
cin >> mp[i]; // memory for process
if(mp[i]<=temp)
{
cout << "\nMemory is allocated for Process " << i+1;
temp = temp - mp[i];
}
else
{
cout << "\nMemory is Full";
break;
}
cout << "\nDo you want to continue? (y/n) ";
cin >> ch;
}
cout << "\n\nTotal Memory Available = " << ms;
cout << "\n\n\tPROCESS\tMEMORY_ALLOCATED";
for(i=0;i<n;i++)
cout << "\n \t" << i+1 << "\t" << mp[i];
cout << "\n\nTotal Memory Allocated is " << ms-temp;
cout << "\nTotal External Fragmentation is " << temp;
return 0;
}
MEMORY MANAGEMENT-II
OBJECTIVE: Write C/C++ programs to simulate the following contiguous memory allocation
techniques/placement algorithms: (a) First-fit (b) Best-fit (c) Worst-fit
DESCRIPTION:
• One of the simplest methods for memory allocation is to divide memory into several
fixed-sized partitions. Each partition may contain exactly one process.
• In this multiple-partition method, when a partition is free, a process is selected from
the input queue and is loaded into the free partition.
• When the process terminates, the partition becomes available for another process.
• The operating system keeps a table indicating which parts of memory are available
and which are occupied. When a process arrives and needs memory, a memory section
large enough for this process is provided.
• When it is time to load (or swap) a process into main memory, and if there is more
than one free block of memory of sufficient size, then the operating system must
decide which free block to allocate.
◦ Best-fit strategy chooses the block that is closest in size to the request.
◦ First-fit chooses the first available block that is large enough.
◦ Worst-fit chooses the largest available block.
(before) (after)
Example Memory configuration before and after allocation of 16 MB block
C/C++ PROGRAM FOR MEMORY MANAGEMENT SCHEME: FIRST FIT
// Program for First Fit
#include<stdio.h>
#include<iostream>
using namespace std;
#define max 25
int main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
cout << "\n\tMemory Management Scheme -- FIRST FIT";
cout << "\n\t=====================================\n";
cout << "\nEnter the number of blocks: ";
cin >> nb; // nb = number of blocks
cout << "Enter the number of files: ";
cin >> nf; // nf = number of files
cout << "\nEnter the size of each block (in Bytes):-\n";
for(i=1;i<=nb;i++)
{
cout << "Block " << i << ": ";
cin >> b[i]; // blocks
}
cout << "\nEnter the size of each file (in Bytes):-\n";
for(i=1;i<=nf;i++)
{
cout << "File " << i << ": ";
cin >> f[i]; // files
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp; // storing internal fragmentation
bf[ff[i]]=1;
}
cout << "\nFile_no.\tFile_size\tBlock_no\tBlock_size\tFragement";
for(i=1;i<=nf;i++)
cout << "\n" << i << "\t\t" << f[i] << "\t\t" << ff[i] << "\t\t" <<
b[ff[i]] << "\t\t" << frag[i];
return 0;
}
C/C++ PROGRAM FOR MEMORY MANAGEMENT SCHEME: BEST FIT
// Program for Best Fit
#include<stdio.h>
#include<iostream>
using namespace std;
#define max 25
int main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
cout << "\n\tMemory Management Scheme -- BEST FIT";
cout << "\n\t====================================\n";
cout << "\nEnter the number of blocks: ";
cin >> nb;
cout << "Enter the number of files: ";
cin >> nf;
cout << "\nEnter the size of each block (in Bytes):-\n";
for(i=1;i<=nb;i++)
{
cout << "Block " << i << ": ";
cin >> b[i];
}
cout << "\nEnter the size of each file (in Bytes):-\n";
for(i=1;i<=nf;i++)
{
cout << "File " << i << ": ";
cin >> f[i];
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
cout << "\nFile_no.\tFile_size\tBlock_no\tBlock_size\tFragement";
for(i=1;i<=nf;i++)
cout << "\n" << i << "\t\t" << f[i] << "\t\t" << ff[i] << "\t\t" <<
b[ff[i]] << "\t\t" << frag[i];
return 0;
}
C/C++ PROGRAM FOR MEMORY MANAGEMENT SCHEME: WORST FIT
// Program for Worst Fit
#include<stdio.h>
#include<iostream>
using namespace std;
#define max 25
int main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
cout << "\n\tMemory Management Scheme -- WORST FIT";
cout << "\n\t=====================================\n";
cout << "\nEnter the number of blocks: ";
cin >> nb;
cout << "Enter the number of files: ";
cin >> nf;
cout << "\nEnter the size of each block (in Bytes):-\n";
for(i=1;i<=nb;i++)
{
cout << "Block " << i << ": ";
cin >> b[i];
}
cout << "\nEnter the size of each file (in Bytes):-\n";
for(i=1;i<=nf;i++)
{
cout << "File " << i << ": ";
cin >> f[i];
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
cout << "\nFile_no.\tFile_size\tBlock_no.\tBlock_size\tFragement";
for(i=1;i<=nf;i++)
cout << "\n" << i << "\t\t" << f[i] << "\t\t" << ff[i] << "\t\t" <<
b[ff[i]] << "\t\t" << frag[i];
return 0;
}